Приглашаем посетить
Куприн (kuprin-lit.ru)

3.2 Making Decisions

Previous Table of Contents Next

3.2 Making Decisions

With the if( ) construct, you can have statements in your program that are only run if certain conditions are true. This lets your program take different actions depending on the circumstances. For example, you can check that a user has entered valid information in a web form before letting her see sensitive data.

The if( ) construct runs a block of code if its test expression is true. This is demonstrated in Example 3-1.

Example 3-1. Making a decision with if( )
if ($logged_in) {

   print "Welcome aboard, trusted user.";

}

The if( ) construct finds the truth value of the expression inside its parentheses (the test expression). If the expression evaluates to true, then the statements inside the curly braces after the if( ) are run. If the expression isn't true, then the program continues with the statements after the curly braces. In this case, the test expression is just the variable $logged_in. If $logged_in is true (or has a value such as 5, -12.6, or Grass Carp, that evaluates to true), then Welcome aboard, trusted user. is printed.

You can have as many statements as you want in the code block inside the curly braces. However, you need to terminate each of them with a semicolon. This is the same rule that applies to code outside an if( ) statement. You don't, however, need a semicolon after the closing curly brace that encloses the code block. You also don't put a semicolon after the opening curly brace. Example 3-2 shows an if( ) clause that runs multiple statements when its test expression is true.

Example 3-2. Multiple statements in an if( ) code block
print "This is always printed.";

if ($logged_in) {

    print "Welcome aboard, trusted user.";

    print 'This is only printed if $logged_in is true.';

}

print "This is also always printed.";

To run different statements when the if( ) test expression is false, add an else clause to your if( ) statement. This is shown in Example 3-3.

Example 3-3. Using else with if( )
if ($logged_in) {

    print "Welcome aboard, trusted user.";

} else {

    print "Howdy, stranger.";

}

In Example 3-3, the first print statement is only executed when the if( ) test expression (the variable $logged_in) is true. The second print statement, inside the else clause, is only run when the test expression is false.

The if( ) and else constructs are extended further with the elseif( ) construct. You can pair one or more elseif( ) clauses with an if( ) to test multiple conditions separately. Example 3-4 demonstrates elseif( ).

Example 3-4. Using elseif( )
if ($logged_in) {

    // This runs if $logged_in is true

    print "Welcome aboard, trusted user.";

} elseif ($new_messages) {

    // This runs if $logged_in is false but $new_messages is true

    print "Dear stranger, there are new messages.";

} elseif ($emergency) {

    // This runs if $logged_in and $new_messages are false

    // But $emergency is true

    print "Stranger, there are no new messages, but there is an emergency.";

}

If the test expression for the if( ) statement is true, the PHP interpreter executes the statements inside the code block after the if( ) and ignores the elseif( ) clauses and their code blocks. If the test expression for the if( ) statement is false, then the interpreter moves on to the first elseif( ) statement and applies the same logic. If that test expression is true, then it runs the code block for that elseif( ) statement. If it is false, then the interpreter moves on to the next elseif( ).

For a given set of if( ) and elseif( ) statements, at most one of the code blocks is run: the code block of the first statement whose test expression is true. If the test expression of the if( ) statement is true, none of the elseif( ) code blocks are run, even if their test expressions are true. Once one of the if( ) or elseif( ) test expressions is true, the rest are ignored. If none of the test expressions in the if( ) and elseif( ) statements are true, then none of the code blocks are run.

You can use else with elseif( ) to include a code block that runs if none of the if( ) or elseif( ) test expressions are true. Example 3-5 adds an else to the code in Example 3-4.

Example 3-5. elseif( ) with else
if ($logged_in) {

    // This runs if $logged_in is true

    print "Welcome aboard, trusted user.";

} elseif ($new_messages) {

    // This runs if $logged_in is false but $new_messages is true

    print "Dear stranger, there are new messages.";

} elseif ($emergency) {

    // This runs if $logged_in and $new_messages are false

    // But $emergency is true

    print "Stranger, there are no new messages, but there is an emergency.";

} else {

    // This runs if $logged_in, $new_messages, and

    // $emergency are all false

    print "I don't know you, you have no messages, and there's no emergency.";

}

All of the code blocks we've used so far have been surrounded by curly braces. Strictly speaking, you don't need to put curly braces around code blocks that contain just one statement. If you leave them out, the code still executes correctly. However, reading the code can be confusing if you leave out the curly braces, so it's always a good idea to include them. The PHP interpreter doesn't care, but humans who read your programs (especially you, reviewing code a few months after you've originally written it) appreciate the clarity that the curly braces provide.

    Previous Table of Contents Next