Popular Blog Posts on The Project Shrink

25 Sure-fire Ways To Motivate Your Team Members

23 Powerful Tips for Working in Multi-Cultural Teams

PHP Conditional Structures

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

}

  • Pete
    Switch conditionals will generally execute faster than if/else conditionals.

    The reason for this is that a switch will only evaluate the given expression once. While you may not notice a performance increase for basic evaluations (e.g. true or false), there may be a significant performance increase when evaluating more complex expressions (e.g. string comparisons, mathematical calculations).

    So when you are evaluating an expression that could have several possible values or outcomes, a switch is probably a better choice than an if/else.

    For your pages, Nick, using something like:

    switch ($page_layout) {
    case "main": break;
    case "about": break;
    case "contact": break;
    }

    would probably be better than:

    if ($page_layout == "main) { }
    else if ($page_layout == "about") { }
    else if ($page_layout == "contact") { }


    And on a personal level, I often find that switches help with code readability.
  • Thank you :)
  • Does PHP parse slower in if/than statements as opposed to a switch statement? My new site design relies on the page to change the layout, and I'm not sure if/then's are really effecient with optimizing load time.
  • Someone
    can i insert if statements inside a switch statement?


    Yes, sir.
  • Guest
    can i insert if statements inside a switch statement?
blog comments powered by Disqus