From now on we will give more “dynamic” because our scripts from various structures indicate that action be carried out in each case, we will also further the possibility of a same action many times with just one line Code.
IF is a control structure used to make decisions as a condition is met (or more) or not. Its basic structure is as follows:
if (condition / s) (
action to perform;
action to perform if they are not complied with;
Here is an example to understand it better basic:
if ($ age> = 18) (
echo “You can not buy beer because you do not have 18 years”;
We can even make conditional more comprehensive as the following case:
if (($ age> = 18 )&&($ money> 0)) (
You can buy beer because you have 18 and your money is greater than 0;
echo “Or does not mince or do not have the 18”;
Takes different decisions depending on various states of the variable.Su syntax is as follows:
switch (expression) (
sentence to run when the expression is worth value1
sentence to run when the expression is worth value2
sentence to run when the expression is worth value3
judgement to be run by default when you are not complied with any of the above conditions
The for loop is used to repeat the same operation a certain number of times. His syntax is as follows:
for (initialization; condition; update) (
sentence to run while the condition
The for loop is composed of 3 parts:
— Restarting: It runs only when you start first bucle.En this part is often put the variable that count the number of times it repeats the loop.
— Status: It will assess the condition that each time you start your bucle.Esta condition it determines the length of the loop.
— Update: It serves to indicate the changes that we want to implement the variables each time you run the loop.
An example of its use would be as follows:
for ($ i = 1; i <= 10; i + +) (
echo “The current number is.” $ i;
This would write all the numbers contained between 0 and 10.
While loop and do while
This loop is used when we want to repeat the execution of judgments about an indefinite number of times. His syntax is as follows:
while (condition) (
sentence to run
To better understand the use of while we serve the following example:
while ($ color! = “red”) (
color = dame a color;
This is an example of what can be done while. In this case if the color red is not tell us that we introduce a color.
While … Loop OJ
This loop is used when we do not know how many times you will run a loop but what is clear is that if we at least once whether to run the accion.Su syntax is as follows:
) while (condition)
BREAK and continuity
Used to stop the loop and stop interpreting the code that follows after the break
It serves to return to the beginning of the loop from anywhere in the loop.