So far, our files have been executed line by line. Due to that, we have been getting notices on some scenarios, such as when the array does not contain what we are looking for. Would it not be nice if we could choose which lines to execute? Control structures to the rescue!
A control structure is like a traffic diversion sign. It directs the execution flow depending on some predefined conditions. There are different control structures, but we can categorize them in conditionals and loops. A conditional allows us to choose whether to execute a statement or not. A loop executes a statement as many times as you need. Let's take a look at each one of them.
A conditional evaluates a Boolean expression, that is, something that returns a value. If the expression is true
, it will execute everything inside its block of code. A block of code is a group of statements enclosed by {}
. Let's see how it works:
<?php echo "Before the conditional."; if (4 > 3) { echo...