Instructables
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.
 
Remove these adsRemove these ads by Signing Up

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't 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
smidge1476 years ago
doesn't even work.
Maybe because he forgot a space between the <?php and echo
alfonso7 years ago
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."
coolguy alfonso7 years ago
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.
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...
inevitable_chaos (author)  alfonso7 years ago
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.
Uru Wolf6 years ago
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.
Matticus IV7 years ago
Is it possible to open a php file without using the internet? (normally I run them by going to (my IP address)/filename.php)
inevitable_chaos (author) 8 years ago
Any comments/corrections are greatly appreciated. :D
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).
inevitable_chaos (author)  LasVegas8 years ago
Yeah, i was hoping to keep it really basic to start off with. As not to scare anyone off learning it.
good instructable, are you going to follow up with any more?
inevitable_chaos (author)  Spencer8 years ago
Yep, hopefully soon.
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";
}