Welcome to S-Design
The home of your bespoke, professional, fast-loading, interactive, database-driven web presence.
Menu
Merry Christmas Everyone
PHP TUTORIALS


PHP Basics

PHP is a HyperText PreProcessor, which means it processes the HyperText(HTML) BEFORE it gets sent to the browser.

But what does this really mean?
Usually when you click a link, your browser sends a request to the server... "can I have xxx page please", and the server grabs the page and sends it back to your browser.
However if the page you requested is a PHP page (has .php as it's file extension) then the server first sends the page through the PHP parser, any PHP commands in the page are executed/followed, and the results sent back to your browser.

Now like me when I first started learning PHP... you may still be thinking "so what?" & "I don't get it". Well, lets start with a simple example.


PHP code is placed inside your normal HTML page, but saved with a .php file extension instead of .html. All PHP code is placed between the PHP start and end tags, like this:
<?PHP
Your PHP code goes here
?>

If you look at most PHP tutorial sites, they'll probably show you this test code, and ask you to run it on a PHP enabled server:
<html>
<title>My First PHP Page</title>
<head>
</head>
<body>
<?PHP
echo "Hello. This text was written by PHP";
?>
</body>
</html>

If your web server is PHP enabled, the above code will produce a webpage with nothing on it but the text "Hello. This text was written by PHP".
If your web server is not PHP enabled, you'll get some kind of error... either the webpage will physically print out the "<?PHP echo "Hello. This text was written by PHP"; ?>" part, or you'll get a blank page, or your browser might attempt to download the PHP page. Either way, you'll know whether PHP is working or not!

OK, so we know that the text inside the PHP code was written out, but wouldn't it have been easier to just write that text in the HTML and get the same results? The simple answer is yes, which is why that kind of example is completely useless to someone who's new to PHP.

Let's try this instead:
<html>
<title>My Second PHP Page</title>
<head>
</head>
<body>
<a href="phptest.php?link=1">Link 1</a>
<br>
<a href="phptest.php?link=2">Link 2</a>
<br>
<?PHP
$link = $_GET['link'];
if($link == "1") {
echo "You clicked on Link 1";
}
else if($link == "2") {
echo "You clicked on Link 2";
}
else {
echo "You haven't clicked a link";
}
?>
</body>
</html>

Assuming we saved the above code as phptest.php, running that page in a browser will produce a webpage containing 2 links, both linking to the same page that the links themselves appear on, together with some text "You haven't clicked a link".

Both links also contain a variable reference called link. One = "1" and the other = "2". When you click on one of these links, you're asking the server to grab the page phptest.php, but are also providing the script within that page access to the values of the variable reference.

The first line of the PHP code GETs the value of link from the url, and assigns it to a PHP variable $link.
So $link will equal either "1" or "2" or nothing.

The code then checks to see if the variable $link equals "1", and if so writes out the text "You clicked on Link 1", and stops processing.

If $link doesn't equal "1", then the code carries on and checks to see if $link equals "2", and if so writes out the text "You clicked on Link 2", and stops processing.

If $link hasn't been matched to "1" or "2", the PHP code writes out the text "You haven't clicked on a link".

Hopefully you'll be starting to see what PHP can do. It can change the content of a page based upon what your visitors do. Now you could have created 2 links to 2 different pages, and then created those 2 different pages, but with PHP you can do it all with one simple page which is easier to keep track of.

Don't worry if you didn't fully understand the PHP code above... we'll get to that in the following chapters, but hopefully you'll be a little wiser as to why you might want to use PHP.

Lets move on and explore PHP Variables.


Title