PHP: Control Structures for Absolute Beginners

PHP has support various conditional statement that allow developer to manipulate data easily. Following is the control structure that supported by PHP:

If, Else, Else If

If you are come from other programming background,  then this is sound familiar to you. Same as other languages, the “If, Else, Else If” is the most common conditional statement that you will use throughout your project. The usage will be as shown as below:

[php]
<?php
$number = 5;
if($number == 4) {
echo "This value is equal to 4";
} else if($number == 3) {
echo "This value is equal to 3!";
} else {
echo "This value is not equal to 3 or 4";
}
?>
[/php]

For

For, is a looping statement that accept 3 expression, which is first expression is the default expression, second expression beginning of each iteration of the loop as long as the expression is evaluates to TRUE, last expression is evaluated at the end of each iteration. The usage is:
[php]
<?php
for($i=0; $i<3; ++$i) {
/*print output here*/
}
?>
[/php]

Foreach

Foreach, normally is use to deal with array. It allow you to go through every single element of an array such as:
[php]
<?php
$values= array("Value 1", "Value 2","Value 3", "Value 4", "Value 5");
foreach($values as $value) {
/*print output here*/
}
?>
[/php]

While, do-while

While, is a looping that repeat a block of code continuously for as long as a condition is TRUE.
[php]
<?php
$i = 0;
while($i<5) {
echo $i;
++$i;
}
?>
[/php]
The output is: 0 1 2 3 4

The do-while loop is a variant of while loop. This loop will execute the block of code ONCE, and then it will repeat the loop as long as the specified condition is true. For example:
[php]
<?php
$i = 1;
do {
echo $i;
++$i;
} while($i<0);
[/php]
The output is 1.
The do-while loop will always be executed at least once, even if the condition is false, because the statements are executed before the condition is tested.

Switch, Break

Switch, is a conditional statement that use to handle different condition; Break is use to stop the script to continue to run on the certain condition. Break has been use together with other control structure as well, such as while.

[php]
<?php
$day = date(‘w’);
switch($day)
{
case ‘0’:
echo "Sunday!";
break;
case ‘1’:
echo "Monday!";
break;
case ‘2’:
echo "Tuesday!";
break;
case ‘3’:
echo "Wednesday!";
break;
case ‘4’:
echo "Thursday!";
break;
case ‘5’:
echo "Thanks God!, Today is Friday!";
break;
case ‘6’:
echo "Saturday!";
break;
default:
echo "Sorry, I do not know what day is today.";
break;
}
?>
[/php]
The code above shows that, we use PHP default function called date() to return the days to us, and we check it using switch control structure. Besides, every case in switch must be ended with break control structure, to tell the interpreter to stop running the code, once the condition is met.

Continue

Continue, same like break statement, it often used with looping, such as for, do-while and etc. It is extremely useful if you want a action to be performed when certain criteria is met in a loop, such as:

[php]
<?php
$i=0
for ($i=0;$i<=5;$i++)
{
if ($i==3)
{
continue;
}
echo $1;
}
?>
[/php]
The output will be: 0,1,2,4,5

The loop will skip and continue with next value if $i is equal to 3.

Return

The usage of return statement is similar to other programming language, 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.

include, include_once, require, require_once

PHP allow developer to load external script file using include, include_once, require, require_once. The include_once and require_once is different from include and require, and it is recommended for better performance, because it first will check whether the script file is already been loaded before.


Posted

in

by

Advertisement