PHP Basics: Control Structures


Control structures are very important to writing an effective PHP application. Control structures are necessary in most PHP scripts even if they are small scripts. This is like the difference between bad directions on how to cook a meal in a recipe, such as a recipe that has how much of each ingrediant but not what order to mix them or how long to cook the meal. Below are all important control structures for PHP.

if – Simply put this is used to compare two values and if the argument is true it will continue to complete the task its told to do.

$value2) {
echo “value1 is greater than value2”;
}
?>

else – is usually combined with if statements to provide a task for the code to do if the argument is false.

$value2) {
echo “value1 is greater than value2”;
} else {
echo “value1 is less than value2”;
}
?>

elseif/else if – also is combined with an if for if you want to provide another argument to analyze to see if it is true.

do-while – The only difference from the while statement is that the argument is verified at the end of the nested statements instead of the beginning.

0);
?>

for – for loops can be the hardest ones to get the hang of but once you know how to use them they can come in very handy.

for ($i = 1; $i <= 10; $i++) { echo $i; } $i = 1 to assign the initial value, $i <= 10; to tell it how long to run the for loop and $i++ to increment the value of $i by one after each run of the loop. foreach - is great for working with arrays and can come in handy for working with arrays of data that is pulled from a database.

break – break ends execution of the current for, foreach, while, do-while or switch structure.

continue – continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration.

switch – is a lot like a series of if statements.

switch ($i) {
case 0:
echo “i equals 0”;
break;
case 1:
echo “i equals 1”;
break;
case 2:
echo “i equals 2”;
break;
}

declare – The declare construct is used to set execution directives for a block of code.
return – is important to use within functions. If called from within a function, the return() statement immediately ends execution of the current function, and returns its argument as the value of the function call.

require – require() is identical to include() except upon failure it will also produce a fatal E_COMPILE_ERROR level error.

include – The include() statement includes and evaluates the specified file. One common use for this is to include the connection strings file to run in the document.

require_once – The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

include_once – The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again.

goto – The goto operator can be used to jump to another section in the program.


Thank you for reading. PHPinformation.org, your home for quality PHP Information.

Hi, Stranger! Leave Your Comment...

You must be logged in to post a comment.

Powered by WP Robot