Web Tutorialz - Basic PHP Tutorial

Web Tutorialz

"The home of Tutorialz
on the net"

PHP Tutorials


Basic PHP

Written by Blake Simpson on September 5th, 2010


The first tutorial I am going to write on this website will be about the basics of PHP. Just to save throwing any inexperienced people in at the deep end. More experienced readers should just bear with me, or maybe just refresh your memory's by reading on.

First of all you need to know where to write PHP. PHP can be written anywhere on any page with a ".php" extension. All it has to do is be enclosed in php tags. Like so:

<?php

PHP code here

?>

So simple enough so far. But now for some code put in those tags. How about making text appear?
Watch this!

<?php

echo 'Hello World';

?>

That's all you need. This will make "Hello World" appear on the screen. You could also do these for the same effect or with p tags.

echo <p>.'Hello World'.</p>;

echo "Hello World";

echo "<p>Hello World</p>";

Note the full stop acts as a concat operator e.g. adds on to a string. Also you can use double quotes. Just don't mix 2 kinds together! If you want to use the 2 of the same kind of quote togeather you must escape it with a '\' which I will show soon.

Now for some variables. variables are accessed using the $ sign followed by the variable name. shown like this:

<?php

$message = 'Hello World';

or

$message = "Hello World";

Then display it

echo $message;

?>

If you wanted to echo a variable within a sentence this can be done as long as you echo the sentence using double quotes and only double quotes. e.g.

echo "My message is: $message";

On to if statments. These are fairly simple so i'll be brief. Note also to compare a variable, or see what it contains you use a double equals sign not a single one.

<?php

if($message == 'Hello World') {
echo $message;
} else {
echo "message variable is not \"Hello World\", It is $message";
}

?>

Here you see a statement where if the message is one thing it displays it. "Otherwise it says variable is not "Hello World", It is" then whatever the variable contains. Notice the quotes within quotes escaped with \'s

And that's it. They are some of the basics of PHP. That should be enough to take in for the now though. Make sure to check back soon for more advanced tutorials too!

Thank you for reading. You are reader number 543.

^Back to Top^