The manual Page
Version française
   
index | glossary | news | downloads | links ]
  PHP
PHP basics
syntax
controle structures
MySQL
Oracle
source code
 
news
glossary
links
downloads
 
credits
contact
 
 
search
 
last update
19/02/2003
Valid HTML 4.0!
Valid CSS!
Hit-Parade
Mesurez votre audience


  Control structures in PHP

Introduction

Like any other programing language, PHP controls the progress of a program with control structures: conditional tests, loops, jumps...

PHP uses most well-known control structures and they are very similar to those used by the C language.

Conditonal structures

if

if (expr1) {
	code to be executed if expr1 is true
}
elseif (expr2) {
	code to be executed if expr2 is true
}
...
else {
	code to be executed only if none of the previous expressions are true
}

expr1 and expr2 are expressions that must return a Boolean value. It can be a simple test like "a == b" or a value returned by a function like this: if (! is_string($var)) ...

We can use as many elseif as needed. Obviously, elseif and else are not mandatory, their uses depend on what we want to test.

If the code to be executed in a statement only has one line, it is possible to get rid of braces ("{" and "}").

switch

This structure is an alternative solution to if when there are a lot of elseif statements:

switch (expr1) {
	case expr2:
		code to be executed if expr1=expr2
		break;
	case expr3:
		code to be executed if expr1=expr3
		break;
	...
	default:
		code to be executed if none of the previous expression
		match expr1
		break;
}

The value of expr1 is successively compared to expr2, expr3... until one of them matches expr1. If there is no match, the code refered by default is executed (default code to be executed). expr1, expr2... can be anything (integers, strings, results of a function call...), as long as "expr1 == expr2" has a meaning.

break is here to indicate the end of a case statement. If one forgets to put a break, the next cae if executed!

(expr)?value1:value2

It may look weird, but this expression is really useful in PHP.

This structure must be used directly in expressions. It is used to make simple tests and get a result directly usable in expressions. Here is an example:

echo "$nbr page".(($nbr>1)?"s":"")." found.";

This is supposed to display the number of pages found after a query, but in a correctly constructed sentence (i.e. "3 pages found" or "1 page found"). Explanation: expr is first evaluated. If it is true, then value1 is returned (a "s" in this example), or else value2 is returned (an empty string in this example). It is very simple and efficient to avoid heavy if structures. Of course, value1 and value2 may be anything.

Loops

for

It is certainy the most common structure, but it is also the most complicated:

for (start_expr; cond_expr; iter_expr) {
	code to be executed at each iteration
}

This for loop needs 3 expressions:

  1. The first one (start_expr) is the start expression; it evaluated only once, when the loop sequence starts. We usually use it to initialize a loop counter (something like "$i = 0").
  2. The second one is a conditional expression (for example: i < 10) that controls the loop iterations. This expression is evaluated every time at the very beginning of each iteration (before executing the code of the loop); if it is true, a new iteration is started and the code of the loop is executed. If it is false, the loop sequence is over, and the program continues with the code that follows the for loop.
  3. The third expression is the iteration expression. It is evaluated at the end of each iteration. Typically, we use this expression to increment the loop counter initialized in start_expr.

Here is an example of a simple counter (from 1 to 10):

for ($i = 1; $i < 11; $i++) {
// We display the value if the counter $i
	echo "$i\n";
}

The break and continue instructions may be used respectively to leave immediately a loop sequence (the program continues with the code that follows the for statement), or to finish immediately the current iteration but we do not leave the loop (the program continues with the evaluation of the iteration expression).

Like for if, if the code of the loop has only one line, we may ommit braces ("{" and "}").

while

It is the second structure used to build loops. It is easier to use than for, since their it requires only one expression:

while (expr) {
	code to be executed as long as expr is true
}

Its working is quite simple: at each iteration, expr is evaluated. If the result is true the code of the loop is executed before performing another evalutation of expr and so on. As soon as an evaluation is false, the loop is interrupted and the program continues with the code that follows the while statement.

Here is the same counter as the one given for the for structure:

$i = 1;
while ($i < 11) {
// We display the value if the counter $i
	echo "$i\n";
	$i++;
}

One must be very careful when writting while loops, because there is a risk to write infinite loops... It is then recommended to always check increments and test expressions when writting such a loop.

As the for statement, it is possible to use break and continue to get special behaviours. break will immediately stop a loop, but it is also possible to ask PHP to stop the loop only after n iterations with the break n; instruction. continue is used to immediately start a new iteration without waiting for the set of instructions to be completely executed. continue n; can be used to get this behaviour only for the iteration number n.

As usual, for a single-lined instruction set, we can ommit braces.

do/while

do {
	code to be executed
} while (expr);

This structure is variant of the while structure: expr is evaluated only at the end of an iteration (and not the beginning), that is to say the instruction set is executed at least once.

break and continue have the same uses as in a while statement. It is possible to get rid of braces if there is only one instruction line.

printable format printable format



Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences.