Learn How to Program PHP Style

Introduction: Learn How to Program PHP Style

Tutorial: PHP Tutorial - the Basics
Author: laconix / inevitable_chaos
Difficulty: Easy
Description: Just the bare essentials to get you started in PHP.
Prerequisites: Basic html knowledge, a web server with PHP installed.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: The Basics

BASICS:

Okay to get started create a basic html page like such:
<html><body></body></html>
Okay you're familiar with this no doubt, now to add in the PHP.
<body><?phpecho "Hello World!";?></body></html>
All this simple code does is echo Hello World onto the page.
The 2 most used commands to output information in PHP are print and echo.

Step 2: Variables

VARIABLES:
Have your code set up like before-hand.
<body><?php?></body></html>

To define a variable in PHP a $ precedes the variable name. For example.
<body><?php$hw = "Hello World!";?></body></html>
Now a variable by itself doesn't do anything really, you need to do something with it!
<body><?php$hw = "Hello World!";echo $hw;?></body></html>
All this bit of code does is store Hello World! in a variable called $hw, then echos "Hello World!" onto the page.
If all went well it should look like this: Hello World

Step 3: Statements

Statements:
Okay now we're getting a little more difficult!
Start off with a basic PHP model.
<body><?php?></body></html>
Now add in code to make the variable $pie and $number, and store the string "I like pie!" and 1 in them respectively.
If you got it right it should look like such:
<body><?php$pie = "I like pie!";$number = 1;?></body></html>
Now for the slightly more difficult part, an IF statement.
<body><?php$pie = "I like pie!";$number = 1;echo "Who likes pie?";if ($number != 1)    echo "You didn&apost store the right number in the variable $number!";else    echo "<br>" . $pie; ?></body></html>
Okay I should now explain the basic operators in PHP.
"!=" - does not equal
"==" - does equal
">" - greater than
">=" - greater than or equal to
"<" - smaller than
"<=" - smaller than or equal to

To separate things in an echo us a period "."

The basic IF statement is structured as such:
    do thiselse    do this

If all went well it should look like this: Statements

You have just learnt the basics of PHP, go and play around with your new learnt skill.

Copyright laconix - http://deadestsystem.info

Be the First to Share

    Recommendations

    • LED Strip Speed Challenge

      LED Strip Speed Challenge
    • Sculpting Challenge

      Sculpting Challenge
    • Clocks Contest

      Clocks Contest

    15 Discussions

    0
    ReCreate
    ReCreate

    Reply 10 years ago on Step 1

    Maybe because he forgot a space between the <?php and echo

    0
    alfonso
    alfonso

    12 years ago on Introduction

    I'm new to php and most of the web site stuff, so could you tell me what you mean when you say "a web server with PHP installed."

    0
    coolguy
    coolguy

    Reply 12 years ago on Introduction

    You may already know, but a webserver is what holds the website files and handles all the requests. You could open a regular html file in firefox or iexplorer but if you wanted to use javascript php asp xml sql etc... the site has to be viewed through a webserver. If you use an internet host to store your website files, they already have everything setup for you. If you want to view javascript and other languages beside html by reading the files directly off your computer you will need to setup up a webserver (such as apache or iis) and to view php pages on it you'll need to add the php.ini module to it. The easiest way to start up is to just get an online host and upload your files to it to get a basic understanding and as you move along eventually you'll want to setup a webserver. Note: Free hosted sites like geocities don't support things like sql databases or php code, some don't even support javascript. if you really are just getting started goto www.w3schools.com. They have awesome tutorials that cover a lot of whats out there.

    0
    rabbitkillrun
    rabbitkillrun

    Reply 11 years ago on Introduction

    not true... javascript and other similar languages that are proccessed use-side can be run from your won computer... it is only server-side languages that need a webserver with it installed...

    0
    inevitable_chaos
    inevitable_chaos

    Reply 12 years ago on Introduction

    Well you can have a very basic webserver with nothing but the ability to show html pages to people, and let the browse listings of files.. Or you can have a very extensible one that has support for languages such as PHP, or perhaps it may have a MySQL database. It is very uncommon, very very uncommon, to have a webserver nowadays that only supports html. Most good free webhosts will support PHP to an extent.

    0
    Uru Wolf
    Uru Wolf

    12 years ago on Introduction

    This is a nice little instructable but you don't realy cover much on how to run the php files, ect. I might make one that goes into deeper debth, maybe look at some functions and databse intergration.

    0
    Matticus IV
    Matticus IV

    12 years ago on Introduction

    Is it possible to open a php file without using the internet? (normally I run them by going to (my IP address)/filename.php)

    0
    inevitable_chaos
    inevitable_chaos

    13 years ago

    Any comments/corrections are greatly appreciated. :D

    0
    LasVegas
    LasVegas

    Reply 13 years ago

    A little simplistic. I'd recommend demonstrating how to interact with the user as well, (IE: Input a number...) and perhaps how to run a basic loop (while).

    0
    inevitable_chaos
    inevitable_chaos

    Reply 13 years ago

    Yeah, i was hoping to keep it really basic to start off with. As not to scare anyone off learning it.

    0
    Spencer
    Spencer

    Reply 13 years ago

    good instructable, are you going to follow up with any more?

    0
    randomname20
    randomname20

    13 years ago

    Just something most self taught people over look.

    == is equal to
    === is equal to and of the same type

    so a difference would be say:
    $true = TRUE;
    $a = 1;

    if($a === $true){
    echo $a." = 1 and is BOL TRUE";
    }else{
    echo $a." = 1 and is NOT a bol";
    }