PHP 5 Power Programming

CONTROL STRUCTURES
PHP supports a variety of the most common control structures available in other programming languages. They can be basically divided into two groups: conditional control structures and loop control structures. The conditional control structures affect the flow of the program and execute or skip certain code according to certain criteria, whereas loop control structures execute certain code an arbitrary number of times according to specified criteria.
 Conditional Control Structures
Conditional control structures are crucial in allowing your program to take different execution paths based on decisions it makes at runtime. PHP supports both the if and switch conditional control structures.
 if Statements
if statements are the most common conditional constructs, and they exist in most programming languages. The expression in the if statement is referred to as the truth expression. If the truth expression evaluates to true, the statement or statement list following it are executed; otherwise, they’re not. You can add an else branch to an if statement to execute code only if all the truth expressions in the if statement evaluated to false:
if ($var >= 50) {
print ‘$var is in range’;
}
else { print ‘$var is invalid’;
}
Notice the braces that delimit the statements following if and else, which make these statements a statement block. In this particular case, you can omit the braces because both blocks contain only one statement in them. It is good practice to write these braces even if they’re not syntactically required. Doing so improves readability, and it’s easier to add more statements to the if block later (for example, during debugging). The elseif construct can be used to conduct a series of conditional checks and only execute the code following the first condition that is met. For example:
if ($num < 0)
{ print ‘$num is negative’;
}
elseif ($num == 0)
{
print ‘$num is zero’;
}
elseif ($num > 0)
{
print ‘$num is positive’;
}
Control Structures 
The last elseif could be substituted with an else because, if $num is not negative and not zero, it must be positive.
Note: It’s common practice by PHP developers to use C-style else if notation instead of elseif.
Both styles of the if construct behave in the same way. While the statement style is probably more readable and convenient for use inside PHP code blocks, the statement list style extends readability when used to conditionally display HTML blocks. Here’s an alternative way to implement the previous example using HTML blocks instead of print:<!–?php if ($num &lt; 0): ?–>
<h1><span style= »font-size: 12pt; »>$num is negative</span></h1>
<h1><span style= »font-size: 12pt; »>$num is zero</span></h1>
<h1><span style= »font-size: 12pt; »>$num is positive</span></h1>
<!–?php endif; ?–>
As you can see, HTML blocks can be used just like any other statement. Here, only one of the HTML blocks are displayed, depending on the value of $num.
Note: No variable substitution is performed in the HTML blocks. They are always printed as is.
switch Statements
You can use the switch construct to elegantly replace certain lengthy if/ elseif constructs. It is given an expression and compares it to all possible case expressions listed in its body. When there’s a successful match, the following code is executed, ignoring any further case lines (execution does not stop when the next case is reached). The match is done internally using the regular equality operator (==), not the identical operator (===). You can use the break statement to end execution and skip to the code following the switch construct.
Statement Statement List switch (expr){ case expr: statement list case expr: statement list … default: statement list } switch (expr): case expr: statement list case expr: statement list … default: statement list endswitch;

LIRE AUSSI :  Les sessions et cookies en PHP

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *