back || home || next

Tutorial 3 : Statements and Loops

The if Statement

The if statement evaluates an expression between parentheses. If this expression results in a true value, a block of code is executed. Otherwise, the block is skipped entirely. This enables scripts to make decisions based on any number of factors.

if ( expression )

{

// code to execute if the expression evaluates to true

}

Example

<?php

$num = 1;

If ($num1==1){

Print “Your number is one”;

}

If ($num1 !=1){

Print “Your number is not one”;

}

?>

Using the else Clause with the if Statement

When working with the if statement, you will often want to define an alternative block of code that should be executed if the expression you are testing evaluates to false. You can do this by adding else to the if statement followed by a further block of code:

if ( expression )

{

// code to execute if the expression evaluates to true

}

else

{

// code to execute in all other cases

}

Example

<?php

$num = 1;

If ($num1==1){

Print “Your number is one”;

}else{

Print “Your number is not one”;

}

?>

Using the elseif Clause with the if Statement

You can use an if-elseif-else construct to test multiple expressions before offering a default block of code:

if ( expression )

{

// code to execute if the expression evaluates to true

}

elseif ( another expression )

{

// code to execute if the previous expression failed

// and this one evaluates to true

else

{

// code to execute in all other cases

}

Example

<?php

$num = 1;

If ($num1==1){

print “Your number is one”;

}

elseif($num1==0){

print “Your number is zero”;

}

else{

print “Not zero or one”;

}

?>

The switch Statement

The switch statement is an alternative way of changing program flow according to the evaluation of an expression. There are some key differences between the switch and if statements. Using the if statement in conjunction with elseif, you may evaluate multiple expressions. switch evaluates only one expression, executing different code according to the result of that expression, as long as the expression

evaluates to a simple type (a number, a string, or a boolean). The result of an expression evaluated as part of an if statement is read as either true or false. The expression of a switch statement yields a result that is tested against any number of values.

switch ( expression )

{

case result1:

// execute this if expression results in result1

break;

case result2:

// execute this if expression results in result2

break;

default:

// execute this if no break statement

// has been encountered hitherto

}

The switch statement's expression is often simply a variable. Within the switch statement's block of code, you find a number of case statements. Each of these tests a value against the result of the switch statement's expression. If these are equivalent, then the code after the case statement is executed. The break statement ends execution of the switch statement altogether. If this is left out, the next case statement's expression is evaluated. If the optional default statement is reached, its code is executed. 

Caution

Don't forget to include a break statement at the end of any code that will be executed as part of a case statement. Without break, the program flow will continue to the next case statement and ultimately

to the default statement. In most cases, this will not be the behavior that you will be expecting.

Using the ? Operator

The ? or ternary operator is similar to the if statement but returns a value derived from one of two expressions separated by a colon. Which expression is used to generate the value returned depends on the result of a test expression:
 

( expression ) ?returned_if_expression_is_true:returned_if_expression_is _false;

If the test expression evaluates to true, the result of the second expression is returned; otherwise, the value of the third expression is returned. 

Loops

So far you've looked at decisions that a script can make about what code to execute. Scripts can also decide how many times to execute a block of code. Loop statements are designed to enable you to achieve repetitive tasks. Almost without exception, a loop continues to operate until a condition is achieved, or you explicitly choose to exit the loop.

The while Statement

The while statement looks similar in structure to a basic if statement: 

while ( expression )

{

// do something

}


As long as a while statement's expression evaluates to true, the code block is executed over and over again. Within the block, you usually change something that affects the while statement's expression; otherwise, your loop continues indefinitely.


The do..while Statement

A do...while statement looks a little like a while statement turned on its head. The essential difference between the two is that the code block is executed before the truth test and not after it:

do {

// code to be executed

}

while ( expression );

Note

The test expression of a do...while statement should always end with a semicolon.

This statement might be useful if you want the code block to be executed at least once even if the while expression evaluates to false.

The for Statement

You cannot achieve anything with a for statement that you cannot do with a while statement. On the other hand, the for statement is often a neater and safer way of achieving the same effect. Earlier, Listing 5.6 initialized a variable outside the while statement. The while statement then tested the variable in its expression. The variable was incremented within the code block. The for statement allows you to achieve this on a single line. This allows for more compact code and makes it less likely that you forget to increment a counter variable, thereby creating an infinite loop. 

for ( variable assignment; test expression; variable increment )

{

// code to be executed

}

Each of the expressions within the parentheses of the for statement is separated by semicolons. Usually, the first expression initializes a counter variable, the second expression is the test condition for the loop, and the third expression increments the counter.

Breaking Out of Loops with the break Statement

Both while and for statements incorporate a built-in test expression with which you can end a loop. The break statement, though, enables you to break out of a loop according to additional tests. This can provide a safeguard against error.

Skipping an Iteration with the continue Statement

The continue statement ends execution of the current iteration but doesn't cause the loop as a whole to end. Instead, the next iteration is immediately begun.

 back || home || next