If you like this post then please subscribe to my full feed RSS. You can also subscribe by Email. Not sure how this works?

As you've read in a previous article, a statement to PHP is like a sentence in English. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well.

One of the most important statement in many languages, PHP included, is the "if" structure. It allows the execution of code, based on different conditions. The syntax is simple:

if (expression)

statement

While the expression should be boolean, this is not a must, because, as you've read before, PHP automatically evaluates expressions according to the context in which they're used. So if "expression" evaluates to "TRUE", then PHP will execute the statement.

if ($num1 > $num2) //is "$num1" bigger than "$num2"?

print "The first number is bigger than the second number"; //if it is, do this

There are times when you want more then one statement to be executed. You don't have to insert 10 if structure for 10 statements, you just have to group all 10 statements in a statement group using curly braces:

if ($num1 > $num2)

{

print "The first number is bigger than the second number";

$bigger = $num1; //assigning the value of "$num1" to "$bigger"

}

Sometimes you may want to execute a statement if a condition is met, and another statement if the condition is not met. In order to do this, you can use "else" after the first statement, which will execute another statement if the condition resolves to "FALSE".

if ($num1 > $num2) //is "$num1" bigger than "$num2"?

{

print "The first number is bigger than the second number"; //if it is, do this

$bigger = $num1; //assigning the value of $num1 to $bigger

}

else

{

print "The second number is bigger than the first number";//if it's not, do this

$bigger = $num2; //assigning the value of $num1 to $bigger

}

Another way to extend an if statement is using "elseif". Similar to "if", "elseif" requires an expression to evaluate. On the other hand, unlike "else", "elseif" will execute a statement if the "if" expression evaluates to FALSE, and only if the "elseif" expression evaluates to TRUE.

if ($num1 > $num2) //is "$num1" bigger than "$num2"?

{

print "The first number is bigger than the second number"; //if it is, do this

$bigger = $num1; //assigning the value of $num1 to $bigger

}

elseif ($num1 == $num2) //the first expression is FALSE, so try this one

{

print "Both numbers are equal"; //got it!

}

else //if the previous if/elseif expressions are FALSE, do this…

{

print "The second number is bigger than the first number"; //the only option left

$bigger = $num2; //assigning the value of $num1 to $bigger

}

You can use several "elseif" structures in the same if statement, but remember that an "elseif" statement will only be executed if the previous expressions were all evaluated to "FALSE", and the current "elseif" expression evaluates to "TRUE". In other words, once the "if" structure evaluates an expression to "TRUE", it executes the corresponding statement, then it exits the structure.

When you want to compare the same variable or expression with many different values, and execute a different piece of code depending on which value it equals to, the "if" statement might prove to be repetitive. This is where "switch" comes in. The "switch" statement is similar to a series of if statements on the same expression.

if ($number == 1)

{ print "the number is 1"; }

if ($number == 2)

{ print "the number is 2"; }

if ($number == 3)

{ print "the number is 3"; }

So you can use a switch structure to write this:

switch ($number)

{

case 1:

print "the number is 1″;

break;

case 2:

print "the number is 2″;

break;

case 3:

print "the number is 3″;

break;

}

While it may look easy at first, the "switch" statement is more complex than you think. "switch" parses each statement and executes it. While at first, no code is executed, when a "case" statement is found with a value that matches the value of the "switch" expression, then PHP begins to execute the statements. It continues to execute statements until the end of the "switch" block, or the first time it finds a "break" statement. If you don't write a "break" statement at the end of a statement group, PHP will go on executing the statements of the following case. For example:

switch ($number)

{

case 1:

print "the number is 1″;

case 2:

print "the number is 2″;

case 3:

print "the number is 3″;

}

If "$number" holds the value "1", this piece of code will output all three messages. If "$number" is "2", it will output only the last two messages; if "$number" is "3", then it will only output the last message. Therefore, it's important to add "break" statements in order for your code to perform as intended. On the other hand, you can use this if you have different values that should output the same result – just leave the values with no corresponding statement.

The "switch" structure has his own "else", but it's called "default". "switch" will try to match all the given values, but if it doesn't find any, it can be provided a default statement, which will be executed. This should be the last statement in the "switch" structure:

switch ($number)

{

case 1:

print "the number is 1″;

break;

case 2:

print "the number is 2″;

break;

case 3:

print "the number is 3″;

break;

default: //if no other values are matched, do the following statement

print "the number is not equal to 1, 2 or 3";

}

Did You Enjoy This Page? These icons link to social bookmarking sites where readers can share and discover new web pages.
  • del.icio.us
  • MisterWong
  • BlinkList
  • Furl
  • Netscape
  • Spurl
  • StumbleUpon
If you like this post then please subscribe to my full feed RSS. You can also subscribe by Email. Not sure how this works?

One Response to “PHP Conditional Structures”

  1. can i insert if statements inside a switch statement?

    Jose

Leave a Reply