PHP TutorialCompile PHP ExtensionsContributing to the PHP CoreContributing to the PHP ManualCreate PDF files in PHPInstalling a PHP environment on WindowsPHP Alternative Syntax for Control StructuresPHP APCuPHP Array iterationPHP ArraysPHP Asynchronous programmingPHP Autoloading PrimerPHP BC Math (Binary Calculator)PHP Built in serverPHP CachePHP Classes and ObjectsPHP ClosurePHP Coding ConventionsPHP Command Line Interface (CLI)PHP CommentsPHP Common ErrorsPHP Compilation of Errors and WarningsPHP Composer Dependency ManagerPHP ConstantsPHP Control StructuresPHP CookiesPHP CryptographyPHP DateTime ClassPHP DebuggingPHP Dependency InjectionPHP Design PatternsPHP Docker deploymentPHP Exception Handling and Error ReportingPHP Executing Upon an ArrayPHP File handlingPHP Filters & Filter FunctionsPHP Functional ProgrammingPHP FunctionsPHP GeneratorsPHP Headers ManipulationPHP How to break down an URLPHP How to Detect Client IP AddressPHP HTTP AuthenticationPHP Image Processing with GDPHP ImagickPHP IMAPPHP Installing on Linux/Unix EnvironmentsPHP JSONPHP LocalizationPHP LoopsPHP Machine learningPHP Magic ConstantsPHP Magic MethodsPHP Manipulating an ArrayPHP mongo-phpPHP Multi Threading ExtensionPHP MultiprocessingPHP MySQLiPHP MySQLi affected rows returns 0 when it should return a positive integerPHP NamespacesPHP Object SerializationPHP OperatorsPHP Output BufferingPHP Outputting the Value of a VariablePHP Parsing HTMLPHP Password Hashing FunctionsPHP PDOPHP PerformancePHP PHPDocPHP Processing Multiple Arrays TogetherPHP PSRPHP Reading Request DataPHP RecipesPHP ReferencesPHP ReflectionPHP Regular Expressions (regexp/PCRE)PHP Secure Remeber MePHP SecurityPHP Sending EmailPHP SerializationPHP SessionsPHP SimpleXMLPHP SOAP ClientPHP SOAP ServerPHP SocketsPHP SPL data structuresPHP SQLite3PHP StreamsPHP String formattingPHP String Parsing

PHP Control Structures

From WikiOD

if else[edit | edit source]

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 greater than b";

PHP Manual - Control Structures - Else

The ternary operator as shorthand syntax for if-else

The ternary operator evaluates something based on a condition being true or not. It is a comparison operator and often used to express a simple if-else condition in a shorter form. It allows to quickly test a condition and often replaces a multi-line if statement, making your code more compact.

This is the example from above using a ternary expression and variable values: $a=1; $b=2;

echo ($a > $b) ? "a is greater than b" : "a is NOT greater than b";

Outputs: a is NOT greater than b.

Alternative syntax for control structures[edit | edit source]

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;, respectively. For individual examples, see the topic on alternative syntax for control structures.

if ($a == 42):
    echo "The answer to life, the universe and everything is 42.";

Multiple elseif statements using short-syntax:

if ($a == 5):
    echo "a equals 5";
elseif ($a == 6):
    echo "a equals 6";
    echo "a is neither 5 nor 6";

PHP Manual - Control Structures - Alternative Syntax

do-while[edit | edit source]

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 {
    echo $i;
} while ($i < 10);

Output: `12345678910`

For detailed information, see the Loops topic.

goto[edit | edit source]

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!:

goto MyLabel;
echo 'This text will be skipped, because of the jump.';

echo 'Hello World!';

declare[edit | edit source]

declare is used to set an execution directive for a block of code.

The following directives are recognized:

For instance, set ticks to 1:


To enable strict type mode, the declare statement is used with the strict_types declaration:


while[edit | edit source]

while loop iterates through a block of code as long as a specified condition is true.

$i = 1;
while ($i < 10) {
    echo $i;

Output: 123456789

For detailed information, see the Loops topic.

include & require[edit | edit source]

require[edit | edit source]

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 - Require

include[edit | edit source]

The include statement includes and evaluates a file.


$a = 'Hello World!';


include 'variables.php';
echo $a;
// Output: `Hello World!`

Be careful with this approach, since it is considered a code smell, because the included file is altering amount and content of the defined variables in the given scope.

You can also include file, which returns a value. This is extremely useful for handling configuration arrays:


return [
    'dbname' => 'my db',
    'user' => 'admin',
    'pass' => 'password',


$config = include 'configuration.php';

This approach will prevent the included file from polluting your current scope with changed or added variables.

PHP Manual - Control Structures - Include

include & require can also be used to assign values to a variable when returned something by file.

Example :

include1.php file :

    $a = "This is to be returned";

    return $a;

index.php file :

    $value = include 'include1.php';
   // Here, $value = "This is to be returned"

return[edit | edit source]

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';
   echo 'This is not executed.';

When you run returnEndsFunctions(); you'll get the output This is executed;

When return is called from within a function with and argument, the execution of the current function will end and the value of the argument will be returned to the calling function.

for[edit | edit source]

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[edit | edit source]

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:

$array = ['color'=>'red']; 

foreach($array as $key => $value){
    echo $key . ': ' . $value; 

Outputs: color: red

For detailed information, see the Loops topic.

if elseif else[edit | edit source]


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 than b", "a is equal to b" or "a is smaller than b":

if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} else {
    echo "a is smaller than b";

Several elseif statements

You can use multiple elseif statements within the same if statement:

if ($a == 1) {
    echo "a is One";
} elseif ($a == 2) {
    echo "a is Two";
} elseif ($a == 3) {
    echo "a is Three";
} else {
    echo "a is not One, not Two nor Three";

if[edit | edit source]

The if construct allows for conditional execution of code fragments.

if ($a > $b) {
  echo "a is bigger than b";

PHP Manual - Control Structures - If

switch[edit | edit source]

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 block is executed. If no matching case statement is found, the code in the default block is executed, if it exists.

Each block of code in a case or default statement should end with the break statement. This stops the execution of the switch structure and continues code execution immediately afterwards. If the break statement is omitted, the next case statement's code is executed, even if there is no match. This can cause unexpected code execution if the break statement is forgotten, but can also be useful where multiple case statements need to share the same code.

switch ($colour) {
case "red":
    echo "the colour is red";
case "green":
case "blue":
    echo "the colour is green or blue";
case "yellow":
    echo "the colour is yellow";
    // note missing break, the next block will also be executed
case "black":
    echo "the colour is black";
    echo "the colour is something else";

In addition to testing fixed values, the construct can also be coerced to test dynamic statements by providing a boolean value to the switch statement and any expression to the case statement. Keep in mind the first matching value is used, so the following code will output "more than 100":

$i = 1048;
switch (true) {
case ($i > 0):
    echo "more than 0";
case ($i > 100):
    echo "more than 100";
case ($i > 1000):
    echo "more than 1000";

For possible issues with loose typing while using the switch construct, see Switch Surprises