subscribe: Posts | Comments

Control Statements

0 comments


The PHP control structures are the same as those of C, with some added.
Conditional

The structure of conditionals is the same as in C:
if ($ username == "Wildfred") (

) Elseif ($ username == "Winifred") (

Else ()

)

Switch


switch ($ user) (
case "Wildfred:
break;
case "Winifred"
break;
default:
)
The expression of selected group of case has to be scalar (not array or object).

Loops

Loops can be used in the instructions break and continue to exit the current
loop or to advance to the next iteration.

For

for ($ i = 0; i <40; i + +) (
Echo ( " $ i is $ i <BR>");
)

While

$ bDoExit = 0;
while (! $ bDoExit) (
Echo ( "ITER <BR>);
bDoExit = $ 1;
)
do (
Echo ( "ITER <BR>);
bDoExit = $ 1;
) While (! $ BDoExit);

list, each (arrays)

There are two functions that together we can easily iterate through all the
elements of an array:
$ arrApellidos = array ( ‘Pepe’ => "Doe", "Paco" => "Gomez");
while (list ($ strNombre, $ strApellidos) = each ($ arrApellidos)) (
Echo ( "$ name $ strNombre is strApellidos. <BR>");
)
All arrays have a counter (accessible via the functions current reset next and
prev and the role each is responsible for return current and call next With the
list and assign the key element to the variable $strNombre $strApellidos until
there is no element each returns null).

Array_Walk

Array_Walk is a function that takes as parameters an array and a function and
applies the function to each element of the array:
dumpit function ($ elem) (
Echo ( "$ elem <BR>);
)
$ arr = array ( "Elem1, Elem2, Elem3");
Array_Walk ($ arr, "dumpit");