Slipszenko Web Development
Beginners PHP Part 2
Category: PHP. Date: 2007-10-16. Comments: 5.

Before your start this tutorial, unless you have a basic understanding of php variables, please go back and read part 1 of this tutoial found here. If you have already done that or already understand variables please read on.

The last tutorial didn't really tell us much that would make the site dynamic as such. But now I think we're ready to go onto something a lot more interesting and a lot more useful, "if" statements:

Php Code Box
<?php
if(== 1)
    {
    echo 
"One Does Equal One!";
    }
?>

This code is the basic design for an "if" statement, first we simply start the "if" function which then checks what's whichin the brackets "()" to see if it's true or false. In this case, we're checking to see if 1 is equivalent to 1. If so we run the code within the curly brace's "{}". So basicly this statement says that if 1 equals 1 then we display "One Does Equal One!". The "==" is a comparison operater. There are several other comparison operators, here are a few of the most useful ones:

Comprison Operator Meaning Usage
== Is Equal To To check if the criteria are equal to each other.
!= Is Not Equal To To check if the criteria are not equal to each other.
< OR > Is bigger than OR is smaller than. To check if the criteria is bigger than the other OR to check if the criteria is smaller than the other
=< OR => Is bigger than or equal to OR is smaller than or equal to. To check if the criteria is bigger than or equal to the other OR to check if the criteria is smaller than or equal to the other

You don't have to use set amounts as the "if" statements criteria, you can use variables (It all starts to link in, see). So we can do this:

Php Code Box
<?php
$page 
$_GET['page'];
if(
$page == "news")
    {
    echo 
"This is the news page!";
    }
?>

Also, php "if" statements can be extended by adding "else" and "elseif" statements like this:

Php Code Box
<?php
$page 
$_GET['page'];
if(
$page == "news")
    {
    echo 
"This is the news page!";
    }
elseif(
$page == "home")
    {
    echo 
"This is the home page!";
    }
else
    {
    echo 
"This is not the news or the home page!";
    }
?>

This code first checks to see if the variable page, taken from the query string on the URL has the value "news", if it is it displays "This is the news page!", if not it checks to see if the $page variable has the value of home and if so displays "This is the home page!", if it isn't then it displays: "This is not the news or the home page!".

If you have any problems with this leave a comment and I shall try to ammend this tutorial to help you out. That's all for now, but I shall be uploading part 3 soon. To be the first to find out when, subscribe to the rss feed.

EDIT: Part 3 Now Available: Here

Comments

Comments are temporarily unavailable as I'm working on various different updates, will be back very soon!


2007 - 2008 © Slipszenko Web Development