Control Structures in PHP

Control Structures


if structure


The if Statement


    Conditional structures are used to control which statements get executed. They are composed of three fundamental elements:

    if statements;

    elseif statements; and

    else statements.


Conditionals in PHP are structured similarly to those found in C++ and Java. The structure begins with an if clause, which is composed of the word "if" followed by a true/false statement in parentheses ( ). The subsequent code will be contained in a block, denoted by curly braces { }. Sometimes the braces are omitted, and only one line will follow the if statement. elseif and else clauses sometimes occur after the if clause, to test for different statements.


    The if clause says "If this statement is true, I want the program to execute the following statements. If it is false, then ignore these statements." In technical terms, it works like this: When an if statement is encountered, the true/false statement in parentheses is evaluated. If the statement is found to be true, the subsequent block of code contained in curly braces is executed. However, if the statement is found to be false, the program skips those lines and executes the next non-blank line.


    Following the if clause are two optional clauses: else and elseif. The elseif clause says "If the last statement was false, let's see if this statement is true. If it is, execute the following code. If it isn't, then skip it." elseif statements are only evaluated when the preceding if statement comes out to be false. Otherwise they are skipped. Other than that, the elseif clause works just like a regular if clause. If it is true, its block is executed, if not, its block is skipped.


    Finally, the else clause serves as a "catch-all" for an if statement. Essentially the else statement says "If all of the preceding tests fail, then execute this code."


    Example 1

    <?php
    $num1= 1;
    $num2= 2;
    if($num1 == $num2) {
    	echo "$num1 is equal to $bar.";
    } elseif ($num1 > $num2) {
    	 echo "$num1 is greater than $bar.";
    } else {
    			echo "$num2 is less than $bar.";
    		}
    ?<
    

Switch Cases


    How They Work

    Here's an example of a simple game where a user enters a $user_command and different functions are run as a resul

    	if($user_command == "n")
    	{
    	 go_north();
             }
    	else if($user_command == "e")
    	{
    	 go_east();
             }
    	else if($user_command == "s")
    	{
    	 go_south();
    	 }
    	 else if($user_command == "w")
    	{
    	 go_west();
             }
    	else
    	{
    	 do_something_else();
    	}
    

    Clearly, there's a lot of repeated code here. The switch case structure allows you to avoid this redundant code. It allows programmers to repeatedly compare the value of a certain variable to a list of possible values and execute code based on the result. This is the syntax for a switch case statement, compared to the same code written using if statements:

    if statement style switch case style

     if($firstvariable == 'comparison1'
    || $firstvariable == 'comparison2')
    {
       doSomething();
       doSomethingElse();
    }
    else if ($firstvariable == 'comparison3')
    {
     doAThirdThing();
    }
    else
    {
     launchMissiles();
    }
    

    // Look at how much switch case saves you!

    switch($firstvariable) { case 'comparison1': case 'comparison2': doSomething(); doSomethingElse(); break; case 'comparison3': doAThirdThing(); break; default: launchMissiles(); break; } The switch case style will save you from retyping $firstvariable, and make your code look cleaner (especially if that code is a long chain of simple if statements). Returning to our zork sample program, we have:Original Code Switch-Case Code if($user_command == "n") { go_north(); } else if($user_command == "e") { go_east(); } else if($user_command == "s") { go_south(); } else if($user_command == "w") { go_west(); } else { do_something_else(); } switch($user_command) { case 'n': go_north(); break; case 'e': go_east(); break; case 's': go_south(); break; case 'w': go_west(); break; default: do_something_else(); break; }

Syntax:


  • switch($var) { case [value]: [code] break; case [value]: [code] break; . .. ... .... default: [code] break; } In this example. $var is the first variable to be compared. This variable is then compared against each case statement from the top down, until it finds a match. At that point, the code will excecute until a break statement is reached (which will allow you to leave the case statement entirely). Important Warning about Using Switch Case Statements!!!! Don't forget to use break when you mean break! If you forget, you might run functions you don't intend to. However, there are circumstances where leaving breaks out can be useful. Consider this example: switch ($n) { case 0: case 1: case 2: //only executes if $n is 0, 1 or 2 doSomethingForNumbers2OrSmaller(); break; case 3: //only executes if $n is 3 doSomethingForNumber3(); default: //only executes if $n is 3 or above doSomethingForNumbers3OrBigger(); break; } This kind of coding is sometimes frowned upon, since it's not always as clear to see what the code is meant to do. Also, consider commenting case statements that aren't supposed to have a break; statement before the next case, so when others look at your code, they know not to add a break.

Did you like our works?

We are known for Website Development and Website Designing, along with Android iOS application development in Mumbai, India. Please write us what you think, we would like to hear it from you

1