Advertisement:

Author Topic: PHP Lesson 07 - control structure, if statement  (Read 55171 times)

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
PHP Lesson 07 - control structure, if statement
« on: August 31, 2003, 08:33:17 PM »
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: [Select]
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

Code: [Select]
$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

Code: [Select]
$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:

Code: [Select]
$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:

Code: [Select]
<?
$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.
« Last Edit: March 29, 2004, 05:04:33 AM by [Unknown] »

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re:PHP Lesson 7
« Reply #1 on: September 01, 2003, 12:10:07 AM »
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]

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
Re:PHP Lesson 7
« Reply #2 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.

Code: [Select]
$red = 'red';
$box = $red;
$red = 'blue';

echo $box; //will print red

while

Code: [Select]
$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.

Offline Overseer

  • Sr. Member
  • ****
  • Posts: 746
  • Gender: Male
    • dubcc.com - West Coast Connection Forum
Re:PHP Lesson 7
« Reply #3 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 :)

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
Re:PHP Lesson 7
« Reply #4 on: September 05, 2003, 08:37:13 AM »
I'm pretty sure it doesn't even exit, you'll have to redeclare the variable so it isn't referenced to other variables.

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re:PHP Lesson 7
« Reply #5 on: September 05, 2003, 09:16:18 PM »
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]

Offline catqueen

  • Newbie
  • *
  • Posts: 0
  • Gender: Male
  • To code or not to code
Re:PHP Lesson 7
« Reply #6 on: September 08, 2003, 11:35:51 AM »
Does PHP have a version of the select case/switch statement?

In C++ it would be
Code: [Select]
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.

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re:PHP Lesson 7
« Reply #7 on: September 08, 2003, 12:58:57 PM »
Yes, and it has exactly the same syntax.

-[Unknown]

Offline Overseer

  • Sr. Member
  • ****
  • Posts: 746
  • Gender: Male
    • dubcc.com - West Coast Connection Forum
Re:PHP Lesson 7
« Reply #8 on: September 08, 2003, 02:52:33 PM »
ahh cool.. i had wondered that.. i'd seen lots of if /elseif/elseif.. and thought.. ewww  ;D

Offline catqueen

  • Newbie
  • *
  • Posts: 0
  • Gender: Male
  • To code or not to code
Re:PHP Lesson 7
« Reply #9 on: September 08, 2003, 07:17:08 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.

Offline Seph|roth

  • Full Member
  • ***
  • Posts: 432
  • Gender: Male
  • Final Fantasy VII: Advent Children
    • Ultimate Weapon Fantasy Gaming Network
Re:PHP Lesson 7
« Reply #10 on: September 08, 2003, 07:40:44 PM »
catqueen, how come your post count states "0"? ;) weird....

Offline catqueen

  • Newbie
  • *
  • Posts: 0
  • Gender: Male
  • To code or not to code
Re:PHP Lesson 7
« Reply #11 on: September 08, 2003, 07:58:51 PM »
catqueen, how come your post count states "0"? ;) weird....
I hadn't even noticed. That is weird.

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re:PHP Lesson 7
« Reply #12 on: September 08, 2003, 09:42:24 PM »
This board was set to have posts not count.

-[Unknown]

Offline Joey Smith™

  • SMF Friend
  • SMF Hero
  • *
  • Posts: 6,356
  • Gender: Male
Re: PHP Lesson 07 - control structure, if statement
« Reply #13 on: November 23, 2008, 11:51:57 PM »
[Unknown] made a very good point.  With assignment, variables themselves aren't referenced, instead their values are copied.

Code: [Select]
$red = 'red';
$box = $red;
$red = 'blue';

echo $box; //will print red

while

Code: [Select]
$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?