![]()
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
IntroductionLike 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 structuresif
expr1 and expr2 are expressions that must return a Boolean value. It can be a simple
test like " 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 "}"). switchThis structure is an alternative solution to if when there are a lot of elseif statements:
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 " 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:value2It 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:
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. LoopsforIt is certainy the most common structure, but it is also the most complicated:
This for loop needs 3 expressions:
Here is an example of a simple counter (from 1 to 10):
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 "}"). whileIt is the second structure used to build loops. It is easier to use than for, since their it requires only one expression:
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:
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 As usual, for a single-lined instruction set, we can ommit braces. do/while
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. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Copyright © 2000-2002 themanualpage.org - This site is submissive to the terms of the GNU GPL and FDL licences. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||