Archived Boards and Threads... > Parham's PHP Tutorials

PHP Lesson 07 - control structure, if statement

(1/3) > >>

Parham:
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:


--- Code: ---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
}

--- End code ---

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


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

--- End code ---

"$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


--- Code: ---$name1 = 'Parham';
$name2 = 'Joe';
$name3 = 'Parham';

--- End code ---

"$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:


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

--- End code ---

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:


--- Code: ---<?
$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";
?>

--- End code ---

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.


--- Code: ---$red = 'red';
$box = $red;
$red = 'blue';

echo $box; //will print red

--- End code ---

while


--- Code: ---$red = 'red';
$box = &$red;
$red = 'blue';

echo $box; //will print blue

--- End code ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version