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";}
The ternary operator as shorthand syntax for if-else
The ternary operator(opens new window) 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.
while
while loop iterates through a block of code as long as a specified condition is true.
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(opens new window).
if($a==42):echo"The answer to life, the universe and everything is 42.";endif;
Multiple elseif statements using short-syntax:
if($a==5):echo"a equals 5";elseif($a==6):echo"a equals 6";else:echo"a is neither 5 nor 6";endif;
To enable strict type mode, the declare statement is used with the strict_types declaration:
declare(strict_types=1);
include & require
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.
Be careful with this approach, since it is considered a code smell(opens new window), 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:
include & require can also be used to assign values to a variable when returned something by file.
Example :
include1.php file :
<?php$a="This is to be returned";return$a;?>
index.php file :
$value = include 'include1.php';
// Here, $value = "This is to be returned"
return
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.
functionreturnEndsFunctions(){echo'This is executed';return;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
for loops are typically used when you have a piece of code which you want to repeat a given number of times.
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
The if construct allows for conditional execution of code fragments.
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";break;case"green":case"blue":echo"the colour is green or blue";break;case"yellow":echo"the colour is yellow";// note missing break, the next block will also be executedcase"black":echo"the colour is black";break;default:echo"the colour is something else";break;}
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";break;case($i>100):echo"more than 100";break;case($i>1000):echo"more than 1000";break;}