Go Back   GotGames.com.au > GotGames Off Topic Discussion > Programming

Reply
 
LinkBack Thread Tools
Old 03-05-2007   #1 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default Chaos' PHP Tutorial - Part 3: Conditional Statements.

Chaos' PHP Tutorial - Part 3: Conditional Statements.

Well, as promised here is the third installment of the epic series of PHP tutorials! This time we will be focusing on Conditional Statements. The previous tutorials contain some knowledge that is needed in this one, so it is advised that you read them (if you already haven't) before attempting to understand this one. This tutorial will most likely be more complicated for beginners than the last one, but bear with it and soon you'll understand!

Conditional Statements
Conditional statements are used to tell the script what to do when certain conditions are met. There are a few different types of conditional statements, I will list a few.

Code:
* If statement
* Else statement
* Elseif statement
* While statement
If statement

First we'll start with the two most used and basic conditional statements; the if and else statement. The if statement is used in PHP just as it could be used in regular conversation:

PHP Code:
if <something happens>
then <something else happens
The actual syntax for the if statement is:

PHP Code:
if (condition/expression) {
statement;

If you remember back to the previous tutorial, I gave an example which would look similar to this:

PHP Code:
$x 5
$y 10;

$x == $y (5 equals to 10False)
$x != $y (5 is not equal to 10True)
$x $y (5 is less than 10True)
$x $y (5 is greater than 10False)
$x <= $y (5 is less than or equal to 10True)
$x >= $y (5 is greater than or equal to 10False
We are going to replicate this but we will do so using the if statement.

PHP Code:
$x 5;
$y 10;

if (
$x $y
{
   echo 
"$x was less than $y!";

Let's take a look at this code. First, we assigned the values 5 and 10 to the variables $x and $y. We then created an if statement, with the conditions or expressions of $x < $y ($x less than $y). Obviously 5 is less than 10, so the condition is true!
The curly brackets { and } are much like the delimiters of PHP; they tell the script to execute everything between them if the conditions that were given in the expression are true.

We now know what happens when the conditions are true, what about if they are false? Normally if the conditions of the if statement are false, the statement to be executed will just be ignored. However, we can tell the script what to do in the case of it being false!

The Else Statement
The else statement is basically the alternative which is executed if the conditions of the if statement are wrong. Its syntax is as follows:

PHP Code:
else {
statement;

Simple, huh? Now we need to add the magical else statement into our script.

PHP Code:
$x 5// Assigned values to x and y.
$y 10;

if (
$x $y
{
   echo 
"$x was less than $y!";
}

else
{
   echo 
"$x was greater than $y!";

Okay. Now what we have is the script saying "if x is less than y... echo '$x was less than $y!', ELSE... echo '$x was greater than $y!'"
In this case, it is the former of the two as $x is less than $y; therefore, the message that "$x was less than $y!" would be displayed. If we changed $x to 10 and $y to 5, the results would change.

PHP Code:
$x 10// Assigned values to x and y.
$y 5;

if (
$x $y
{
   echo 
"$x was less than $y!";
}

else
{
   echo 
"$x was greater than $y!";

In this case, the parser would check if x is less than y -- of course... now it isn't! The parser would then ignore the command to echo and look for the else statement. Seeing as there is no alternative at present, it would run the command in the else statement.
This would result in the message of "$x was greater than $y!" being printed to the screen.

That's all good and well, but what if we wanted to add another variable?


PHP Code:
$x 5;
$y 10;
$z 5
Now we have three variables: $x, $y and $z. The if and else statements just wouldn't cut it if we wanted to compare each variable. We could use the if statement for $x and $y again and the else statement incase the condition isn't true. What about $z?

The Elseif Statement
The Elseif statement basically acts as another if statement, allowing you to give two different conditions for the parser to evaluate. Its syntax is as follows:

PHP Code:
elseif (condition or expression)
{
statement;

Let's add it to our script!

PHP Code:
$x 5
$y 10;
$z 5;

if (
$x $y
{
   echo 
"$x was less than $y!";
}

elseif (
$x $y)
{
   echo 
"$x was greater than $y!";
}

elseif (
$x <= $z)
{
   echo 
"$x was less than or equal to $z!";
}

else 
{
   echo 
"I don't know!";

Alright, this is probably the longest PHP script you've seen... so let's go through it slowly. First it says if x is less than y, then echo the message. Secondly it says elseif x is greater than y, echo the message.
After that it says elseif x is less than or equal to z (in this case x and z are equal) then echo the message. Lastly it says else echo "I don't know!" The else is there for the possibility of y being greater than z and etc.

The While Statement
The while statement is the simplest form of a loop in PHP. Basically, it tells PHP to repeatedly execute the statement as long as the given expression remains true. The syntax for it is:

PHP Code:
while (expression
{
statement;

The while statement is very useful for displaying MySQL data, but that's beyond the scope of this current tutorial. We will use it in a much simpler example.

PHP Code:
$i 1;

while (
$i <= 5
{
    echo 
$i;
    
$i++;

Okay. At first, the script tells PHP to assign the integer of 1 to the variable $i. It then has a while loop. The while loop says "while $i is less than or equal to 5" and then proceeds to echo the value of $i (at first it would be 1.)
It then tells PHP to increment (add) to the value of $i. After $i++ is run, $i would = 2. Since it is a loop, the script will be executed again and again until
$i equals 5. It would also print to screen the numbers 1 to 5.

That pretty much sums up this tutorial on conditional statements. I won't tell you what the next tutorial is about because it will be a nice suprise. Good luck, have fun and remember to PM me if you need help!

--Chaos.
chX is offline   Reply With Quote
Old 04-05-2007   #2 (permalink)
Monster Member
 
Join Date: Jan 2007
Location: Brisbane, Australia
Age: 19
Posts: 3,501
Rep Power: 6
Default

Nicely done - have you got all of these pre-prepared? lol
__________________
Jayson
Jayso is online now   Reply With Quote
Old 04-05-2007   #3 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default

I wrote all of them in the past 3 days. Two on Wednesday and one last night. I write them in notepad first so that I can get everything right before posting them up.
chX is offline   Reply With Quote
Old 04-05-2007   #4 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

There is also the ternary Statement:

PHP Code:
expression true false
Now how this works is, expression is evaluated first, if it is true the first statement is executed (in my example `true`) if not the second (`false` in my example).

This can be a handy nugget say:

$var = isset($_GET['var']) ? parseInput($_GET['var']) : $varDefault;
sets $var to GET['var'] iff it is set, or varDefault if not.

(iff = if and only if btw)
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature.
Khaless is offline   Reply With Quote
Old 04-05-2007   #5 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

Theres no switch in PHP?

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 04-05-2007   #6 (permalink)
Participating Member
 
blindside's Avatar
 
Join Date: Jun 2006
Posts: 231
Rep Power: 3
Default

Yer, there is.
blindside is offline   Reply With Quote
Old 04-05-2007   #7 (permalink)
Monster Member
 
lumi's Avatar
 
Join Date: Aug 2006
Location: Melbourne
Age: 19
Posts: 3,406
Rep Power: 6
Default

Good tut, I'm understanding them all fine keep em coming if you will
__________________

Play .OGG
lumi is offline   Reply With Quote
Old 04-05-2007   #8 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default

Quote:
Originally Posted by chemicalNova View Post
Theres no switch in PHP?

chem

There is, but I didn't include it in this tutorial. I might edit it in later if people want it or I feel a need to (didn't want to overload people with information.)

Last edited by chX; 04-05-2007 at 10:34 PM..
chX is offline   Reply With Quote
Old 04-05-2007   #9 (permalink)
GotGames Ninja Admin
 
Twelve-60's Avatar
 
Join Date: May 2006
Location: NSW, Australia
Age: 20
Posts: 2,282
Rep Power: 5
Send a message via MSN to Twelve-60
Default

PHP Code:
switch ($variable) {
    case 
"foo":
        echo 
'$variable = foo';
        break;
    case 
"bar":
        echo 
'$variable = bar';
        break;
    default:
        echo 
'$variable != foo or bar';



- Twelve-60
__________________
Twelve-60 is online now   Reply With Quote
Old 04-05-2007   #10 (permalink)
Contributing Member
 
Join Date: Jun 2006
Posts: 607
Rep Power: 3
Default

Following is C code.
PHP Code:
switch (count 8)  /* count > 0 assumed */
 
{
   case 
0:        do {  *to = *from++;
   case 
7:              *to = *from++;
   case 
6:              *to = *from++;
   case 
5:              *to = *from++;
   case 
4:              *to = *from++;
   case 
3:              *to = *from++;
   case 
2:              *to = *from++;
   case 
1:              *to = *from++;
                     } while ((
count -= 8) > 0);
 } 
http://en.wikipedia.org/wiki/Duff's_device
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature.
Khaless is offline   Reply With Quote
Old 04-05-2007   #11 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default

Quote:
Originally Posted by Twelve-60 View Post
PHP Code:
switch ($variable) {
    case 
"foo":
        echo 
'$variable = foo';
        break;
    case 
"bar":
        echo 
'$variable = bar';
        break;
    default:
        echo 
'$variable != foo or bar';



- Twelve-60
Yep. I'm planning on teaching them switch when I show them how to use just one index.php page and include all the other pages into it rather than having to redirect to a rankings.php, roster.php page.

Using index.php?page= which will then use the switch statement to include a certain file dependent on the expression they added after the =.

But you know what I'm on about.

Last edited by chX; 04-05-2007 at 10:33 PM..
chX is offline   Reply With Quote
Old 04-05-2007   #12 (permalink)
Pro Member
 
chemicalNova's Avatar
 
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
Default

lol Khaless. I still have trouble seeing what that does every single time..

chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots.
-
chemicalNova is offline   Reply With Quote
Old 11-05-2007   #13 (permalink)
Participating Member
 
dazvid's Avatar
 
Join Date: Jun 2006
Location: Melbourne
Age: 21
Posts: 268
Rep Power: 3
Default

You are going into so much detail, something that is lacking in most basic tutorials. Good work.
dazvid is offline   Reply With Quote
Old 11-05-2007   #14 (permalink)
Pro Member
 
empske's Avatar
 
Join Date: Jan 2007
Location: ZULU
Age: 17
Posts: 6,719
Rep Power: 0
Default

I don't understand any of that.

But nice to see you guys do.
__________________

odin : atleast i didnt join gotgames in 2009

C:\Documents and Settings\Admin\Desktop\DdudFACE.png
empske is offline   Reply With Quote
Old 12-05-2007   #15 (permalink)
chX
GotGames Moderator
 
chX's Avatar
 
Join Date: Jan 2007
Location: Adelaide
Age: 17
Posts: 3,760
Rep Power: 5
Send a message via MSN to chX
Default

Quote:
Originally Posted by dazvid View Post
You are going