Tutorial by Examples

PHP provides an alternative syntax for some control structures: if, while, for, foreach, and switch. When compared to the normal syntax, the difference is, that the opening brace is replaced by a colon (:) and the closing brace is replaced by endif;, endwhile;, endfor;, endforeach;, or endswitch;, ...
while loop iterates through a block of code as long as a specified condition is true. $i = 1; while ($i < 10) { echo $i; $i++; } Output: 123456789 For detailed information, see the Loops topic.
do-while loop first executes a block of code once, in every case, then iterates through that block of code as long as a specified condition is true. $i = 0; do { $i++; echo $i; } while ($i < 10); Output: `12345678910` For detailed information, see the Loops topic.
The goto operator allows to jump to another section in the program. It's available since PHP 5.3. The goto instruction is a goto followed by the desired target label: goto MyLabel;. The target of the jump is specified by a label followed by a colon: MyLabel:. This example will print Hello World!...
declare is used to set an execution directive for a block of code. The following directives are recognized: ticks encoding strict_types For instance, set ticks to 1: declare(ticks=1); To enable strict type mode, the declare statement is used with the strict_types declaration: declare(s...
The if statement in the example above allows to execute a code fragment, when the condition is met. When you want to execute a code fragment, when the condition is not met you extend the if with an else. if ($a > $b) { echo "a is greater than b"; } else { echo "a is NOT gre...
require require is similar to include, except that it will produce a fatal E_COMPILE_ERROR level error on failure. When the require fails, it will halt the script. When the include fails, it will not halt the script and only emit E_WARNING. require 'file.php'; PHP Manual - Control Structures - ...
The return statement returns the program control to the calling function. When return is called from within a function, the execution of the current function will end. function returnEndsFunctions() { echo 'This is executed'; return; echo 'This is not executed.'; } When you run re...

for

for loops are typically used when you have a piece of code which you want to repeat a given number of times. for ($i = 1; $i < 10; $i++) { echo $i; } Outputs: 123456789 For detailed information, see the Loops topic.
foreach is a construct, which enables you to iterate over arrays and objects easily. $array = [1, 2, 3]; foreach ($array as $value) { echo $value; } Outputs: 123. To use foreach loop with an object, it has to implement Iterator interface. When you iterate over associative arrays: $arra...
elseif elseif combines if and else. The if statement is extended to execute a different statement in case the original if expression is not met. But, the alternative expression is only executed, when the elseif conditional expression is met. The following code displays either "a is bigger tha...

if

The if construct allows for conditional execution of code fragments. if ($a > $b) { echo "a is bigger than b"; } PHP Manual - Control Structures - If
The switch structure performs the same function as a series of if statements, but can do the job in fewer lines of code. The value to be tested, as defined in the switch statement, is compared for equality with the values in each of the case statements until a match is found and the code in that blo...

Page 1 of 1