Slipszenko Web Development
Beginners PHP Part 3
Author: Edd Slipszenko. Category: PHP. Date: 2007-10-18. Comments: 10.

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 Code Box
<?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 Code Box
<?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!

Comments
Comment from: Guest - Wilson Gunanithi
Date: 2007-11-26.
Yes.. It was very useful and helpful for Beginners like me.. I hope you will be providing some more useful chapters here.. Beginners are waiting for your described chapters.. OK bye.. and take care.. with PHP greetings.. Kind Regards, Wilson..J
Comment from: Guest - Rye
Date: 2007-11-27.
Yes and thank you for the tutorial! It is very helpful for me as a noob in the field of PHP. I can't wait to dig deeper into it with your tutorials!
Comment from: Guest - Aurelio
Date: 2007-12-04.
Thanks. This helped me go back and understand that I had overlooked switch statements in my earlier learning. It made them very easy to understand.
Comment from: Guest - Khan (Kohat)
Date: 2007-12-06.
This is really very good tutorial for beginners. I like this type of tutorials. Your work is very good. I appreciate you.
Comment from: Guest - shabeer
Date: 2007-12-07.
I is very nice tutorial so i am expecting more tutorials like these.....
Comment from: Guest - tess
Date: 2007-12-07.
good luck happy programming
Comment from: Guest - scott
Date: 2007-12-07.
Fabulous!
Comment from: Guest - Scot Kamins
Date: 2007-12-13.
Very useful beginning. I am psyched that I can learn this language.
Comment from: Hesham
Date: 2008-04-08.
waiting for more of that Mr.Edd Slipszenko it really helped us
Comment from: Hesham
Date: 2008-04-08.
waiting for more of that Mr.Edd Slipszenko it really helped us

© Slipszenko Web Development