Before your start this tutorial, unless you have a good understanding of php variables and if statements, please go back and read part 1 and/or 2 of this tutoial. If you have already done that or already understand these topics, please read on.
In the last tutorial we looked at the use of if statements, whilst they're very useful, it begin's to become annoying when we've got loads of elseif's. A better alternative is to use a "switch" statement. Here is an example of one:
<?php
switch($_GET['page'])
{
case "news": //So the URL will be: index.php?page=news
echo "It's the news page!";
break;
case "contact":
echo "It's the contact page!";
break;
case "info":
echo "It's the info page!";
break;
default: //So the URL will be: index.php OR index.php?page=(anything that isn't news, contact or info)
echo "Home Page!";
break;
}
?>
First we execute the switch statement and then open the curly braces {}'s to contain all the diferent options. Each option is then started with a "case" statement, and then the value of your option, in this case the GET variale of "page", is encased within quotation marks. Php will then run all the code between that and the finishing point which is marked with a "break;". As you can see in the code example above we can also have "default:" instead or "case:", this can be used to process code as a default, (as you might have quessed from the name), if the value doesn't equal any of the options. Hope this has been useful to you thus far!
Now let's look at one of the most useful, things possibly with php, SSI (Server side includes). There are two functions: require() and include(). First I shall example how they are used and then I shall and then I shall discuss the diferences between them.
<?php
require("includes/example.php");
include("includes/example.php");
?>
As you can see, they both work in very similar ways. All you do is call the function and tell it where the file is! Simple :P
The only difference between the two really is that if require() fails to find the file, the entire page won't load. Whereas if include() fails to find the file you told me to, it will simply display an error message.
As always, I hope that you have found this tutorial useful. This is the last of the beginners php tutorials. I'm going to be making some more tutorial's for php, on slightly more specific subject's such, as email and mysql interaction with php. Bye!