PHP Control Structures
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.
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:
while loop iterates through a block of code as long as a specified condition is true.
do-while loop first executes a block of code once, in every case, then iterates through that block of code as long asa specified condition is true.
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!:
include & 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.
The include statement includes and evaluates a file.
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:
This approach will prevent the included file from polluting your current scope with changed or added variables. include & require can also be used to assign values to a variable when returned something by file.
include1.php file :
index.php file :
for loops are typically used when you have a piece of code which you want to repeat a given number of times.
foreach is a construct, which enables you to iterate over arrays and objects easily.
To use foreach loop with an object, it has to implement Iterator interface.
When you iterate over associative arrays:
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.
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":