• Welcome to Simple Machines Community Forum. Please login or sign up.

PHP Lesson 07 - control structure, if statement

Started by Parham, August 31, 2003, 08:33:17 PM

Previous topic - Next topic

Parham

August 31, 2003, 08:33:17 PM Last Edit: March 29, 2004, 05:04:33 AM by [Unknown]
In this lesson I'll be discussing control structures.  What is a control structure you might ask?  Well let's begin with how PHP reads your code.  PHP reads code linearly, meaning it'll start from the first line in the program and move on as each line is successfully executed.  PHP knows to move onto the next line when it sees the semi-colon.  Control structures help you break out of this linear motion, they let you control what line PHP should process next.  To give you a basic example if you've been programming for a while, in older languages for example, the "GOTO" command was and example of a control structure.

The first control structure you need to get very acquainted with is the "if" statement.

The syntax of the "if" statement goes as follows:


if (first expression) {
//if the first expression is true
} elseif (second expression) {
//if the second expression is true
} else {
//if none of the above expressions are true
}


Control structures usually involve some sort of comparison as the expression.  This is where the comparison operators come in handy.  If you didn't already read the comparison operator's documentation located at http://www.php.net/manual/en/language.operators.comparison.php, please take a moment to read it.

Comparison operators compare values (or types) in two (or more) variables and return either "true" or "false".  Some of the common control statements you need to know about are:

"==" - equal in value
"!=" - not equal in value
">" - greater than
"<" - less than
">=" - greater than or equal to
"<=" - less than or equal to


$number1 = 12;
$number2 = 3;
$number3 = 36;
$number4 = 50;
$number5 = 3;


"$number1 == $number2" will return false
"$number1 != $number2" will return true
"$number1 == $number3" will return false
"$number2 == $number5" will return true
"$number1 > $number3" will return false
"$number3 < $number4" will return true


$name1 = 'Parham';
$name2 = 'Joe';
$name3 = 'Parham';


"$name1 == $name2" will return false
"$name1 != $name2" will return true
"$name1 == $name3" will return true

Using the above information now, we can construct our "if" statements.  "if" statements are read very much like how you'd speak regular English when asking yourself a question.  For example, "if the color of the ball is red, put the ball in the first box, otherwise put it in the second box", would be coded in the following way:


$ball = 'red';
if ($ball == 'red') {
  $box1 = $ball;
} else {
  $box2 = $ball;
}


That's a simple enough example.  Let me give you a play-by-play of it.  We declare our variable on the first line, a simple string.  The second line is where all the work is done.  If you want to read it out it'll come out like this: "if the ball is red in value".  That line returns true because the value of $ball is "red", therefore it'll execute the first block and put the $ball variable in the $box1 variable.  Now if the ball wasn't red, the $ball variable would be assigned to the $box2 variable.  Another same type of example:


<?
$ball = 'purple'; //try changing it to "yellow", "blue", "green", "purple", or any other color
if ($ball == 'red') { //if this expression returns true, run the block
  $redbox = $ball;
} elseif ($ball == 'yellow') { //if this statement returns true, run the block
  $yellowbox = $ball;
} elseif ($ball == 'blue') { //ditto
  $bluebox = $ball;
} elseif ($ball == 'green') { //ditto
  $greenbox = $ball;
} elseif ($ball == 'purple') { //ditto
  $purplebox = $ball;
} else { //run this if none of the following were run
  $colorlessbox = $ball;
}
echo "red box: $redbox\n";
echo "yellow box: $yellowbox\n";
echo "blue box: $bluebox\n";
echo "green box: $greenbox\n";
echo "purple box: $purplebox\n";
echo "colorless box: $colorlessbox\n";
?>


You can have endless amounts of "elseif" blocks in your "if" statements.  You can even combine them (something we won't really get to because it usually isn't as important).  Remember this was a very basic introduction to the "if" control structure statement, PLEASE ask questions if you have any.  I'm aware that this lesson might not have been clear.

[Unknown]

It's important to keep in mind that if you do this:

<?php
$red = 'red';
$box = $red;
$red = 'blue';
?>

The variables will NOT be both 'blue'.. $red will be 'blue' and $box will be 'red'.

This is because it only *copies* the value.  So, $a = 5; $b = $a; is the same as $a = 5; $b = 5;.  Changing the values later will have no impact at all.

-[Unknown]

Parham

[Unknown] made a very good point.  With assignment, variables themselves aren't referenced, instead their values are copied.


$red = 'red';
$box = $red;
$red = 'blue';

echo $box; //will print red


while


$red = 'red';
$box = &$red;
$red = 'blue';

echo $box; //will print blue


To pass a variable by reference (and not just copy the value), you HAVE to add an ampersand in front of the variable.  I think this discussion itself might make a very good lesson.

Overseer

what about dereferencing? is it the same as in C?

e.g.

$Red = 'red';
$pRed = &$Red;
$RedToo = *$pRed;  <-- probably wrong :)

Parham

I'm pretty sure it doesn't even exit, you'll have to redeclare the variable so it isn't referenced to other variables.

[Unknown]

Quote from: Overseer on September 05, 2003, 07:41:01 AM
what about dereferencing? is it the same as in C?

e.g.

$Red = 'red';
$pRed = &$Red;
$RedToo = *$pRed;  <-- probably wrong :)

No, it's like this:

$Red = 'red'; // $Red == 'red' -> true.
$pRed = &$Red; // $pRed == 'red' -> true - this is not a C++ reference, it's more link a shortcut.
$RedToo = $pRed; // $RedToo == 'red' -> true.
$pRed = 'blue'; // $Red == 'blue' -> true, $RedToo == 'blue' -> false.

-[Unknown]

catqueen

Does PHP have a version of the select case/switch statement?

In C++ it would be

switch(variable)
{
   case 1:
   //first selection
   break;
   case 2:
   //second selection
   break;
   default:
   //if nothing else matches
}


Usually, elseif is sufficient, but sometimes the switch comes in handy.

[Unknown]

Yes, and it has exactly the same syntax.

-[Unknown]

Overseer

ahh cool.. i had wondered that.. i'd seen lots of if /elseif/elseif.. and thought.. ewww  ;D

catqueen

Quote from: [Unknown] on September 08, 2003, 12:58:57 PM
Yes, and it has exactly the same syntax.

I thought it might. I think jscript uses the same syntax also. That's one of the good things about learning C type languages.

Seph|roth

catqueen, how come your post count states "0"? ;) weird....

catqueen

Quote from: Seph|roth on September 08, 2003, 07:40:44 PM
catqueen, how come your post count states "0"? ;) weird....
I hadn't even noticed. That is weird.

[Unknown]

This board was set to have posts not count.

-[Unknown]

Joey Smith™

Quote from: Parham on September 01, 2003, 01:57:39 AM
[Unknown] made a very good point.  With assignment, variables themselves aren't referenced, instead their values are copied.


$red = 'red';
$box = $red;
$red = 'blue';

echo $box; //will print red


while


$red = 'red';
$box = &$red;
$red = 'blue';

echo $box; //will print blue


To pass a variable by reference (and not just copy the value), you HAVE to add an ampersand in front of the variable.  I think this discussion itself might make a very good lesson.
So let me get this straight here....

In this example:
$red = 'red';
$box = $red;
$red = 'blue';

echo $box; //will print red

echo $red; //would output blue because it was reassigned in the last line right?

Advertisement: