Programming in PHP - Control Structure (part 1)
Posted 02/03/2015 at 00:44 by manulaiko3.0
Copy and paste from my website: 
In the other chapters we made PHP to execute lines 1 by 1, but what about if PHP could choose what to execute? This is possible thanks to the control structures.
Control structures bassically let’s you change the flow of the code when a condition is true or false.
There are 4 control structures you can use:
This is the simplest control structure available, and really powerfull. A simple if statement looks like this:
if (condition) do something… The first component of the control structure is the keyword if, this means that when you see if it should be followed by the condition between parenthesis, the condition can be a boolean variable or a comparation (remember why I told you that comparations are important?). The last part of the if is the line (or lines) that PHP will execute if and only if (that’s the reason of its name) the condition is true, if the lines to execute are more than 1 they must be surrounded by braces, let’s take a look:
<?php $var1 = 3; $var2 = 5; $var3 = “user”; if($var1 >= $var2) echo “The condition is true!”; echo “This line will be executed even if the condition is false!”; if($var3 == “user”) { echo “var3 is user”; echo “This line will be executed only if the condition is true”; } It’s really easy to understand right? Take a look at this:
if (condition) do something… else do more something… As you can see it’s the same as the standard if structure but with the keyword else at the end of the structure, followed by the line (or lines) to execute if the condition is false, let’s see an example with PHP:
<?php $var1 = “user”; if($var1 == “user”) { echo “var1 is user”; echo “Logged in!”; } else { echo “var1 isn’t user”; echo “Please register”; } Well, that’s all about if … else structure!
Switch
The if … else structure lets us choose 2 options based in a condition, but, what about if we need to choose more options? The solution is the switch structure!
This is your problem:
switch (variable) { case 1: do something… break; […] case n: do something… break; default do something break; } The first thing is the keyword switch followed by a variable. Then, the body of the structure, contains case blocks, this is the same as a comparation, for example:
if (variable == 1) do things in case block 1 else if(variable == 2) do things in case block 2 […] else if (variable == n) do things in case block n else do things in default block The default block will be executed if the variable wasn’t found in any of the case blocks and can be omited, this means that if the variable wasn’t found in any of the case blocks nothing will happen, but now let’s try to solve our problem:
<?php $points = 0; $mark = 7; switch($mark) { case 0: $points = $points – 10; break; case 1: $points = $points – 4; break; case 2: $points = $points – 3; break; case 3: $points = $points – 2; break; case 4: $points = $points – 1; break; case 5: $points = $points + 0; //we can omit this block since it makes no change break; case 6: $points = $points + 1; break; case 7: $points = $points + 2; break; case 8: $points = $points + 3; break; case 9: $points = $points + 4; break; case 10: $points = $points + 10; break; } echo $points; //prints 2 And that’s all about switch structure and control structure untill next chapter!
See you!!

In the other chapters we made PHP to execute lines 1 by 1, but what about if PHP could choose what to execute? This is possible thanks to the control structures.
Control structures bassically let’s you change the flow of the code when a condition is true or false.
There are 4 control structures you can use:
- If, If…else
- Switch
- For, Foreach
- While, Do…while
This is the simplest control structure available, and really powerfull. A simple if statement looks like this:
if (condition) do something… The first component of the control structure is the keyword if, this means that when you see if it should be followed by the condition between parenthesis, the condition can be a boolean variable or a comparation (remember why I told you that comparations are important?). The last part of the if is the line (or lines) that PHP will execute if and only if (that’s the reason of its name) the condition is true, if the lines to execute are more than 1 they must be surrounded by braces, let’s take a look:
<?php $var1 = 3; $var2 = 5; $var3 = “user”; if($var1 >= $var2) echo “The condition is true!”; echo “This line will be executed even if the condition is false!”; if($var3 == “user”) { echo “var3 is user”; echo “This line will be executed only if the condition is true”; } It’s really easy to understand right? Take a look at this:
You are coding a login system for your website and you need to check if the user is registered.Afortunally if keyword comes with an opposite keyword: else, and when they’re used together the sky turns blue (if it wasn’t blue before), this is how it looks:
If the user is registered it will be redirected to home page, if it isn’t it will be redirected to register page.
if (condition) do something… else do more something… As you can see it’s the same as the standard if structure but with the keyword else at the end of the structure, followed by the line (or lines) to execute if the condition is false, let’s see an example with PHP:
<?php $var1 = “user”; if($var1 == “user”) { echo “var1 is user”; echo “Logged in!”; } else { echo “var1 isn’t user”; echo “Please register”; } Well, that’s all about if … else structure!
Switch
The if … else structure lets us choose 2 options based in a condition, but, what about if we need to choose more options? The solution is the switch structure!
This is your problem:
You want to have the control of your marks and for that you’ve though in a “game” that consists in this:This is our problem, and we will solve it, but first take a look at the syntax of the switch structure:
You start with 0 points, this amount of points will increase acord with your marks in the latest exams.
Thinking that your exam marks goes from 0 to 10 you have this table:
Mark Points
0 -10
1 -4
2 -3
3 -2
4 -1
5 0
6 +1
7 +2
8 +3
9 +4
10 +10
switch (variable) { case 1: do something… break; […] case n: do something… break; default do something break; } The first thing is the keyword switch followed by a variable. Then, the body of the structure, contains case blocks, this is the same as a comparation, for example:
if (variable == 1) do things in case block 1 else if(variable == 2) do things in case block 2 […] else if (variable == n) do things in case block n else do things in default block The default block will be executed if the variable wasn’t found in any of the case blocks and can be omited, this means that if the variable wasn’t found in any of the case blocks nothing will happen, but now let’s try to solve our problem:
<?php $points = 0; $mark = 7; switch($mark) { case 0: $points = $points – 10; break; case 1: $points = $points – 4; break; case 2: $points = $points – 3; break; case 3: $points = $points – 2; break; case 4: $points = $points – 1; break; case 5: $points = $points + 0; //we can omit this block since it makes no change break; case 6: $points = $points + 1; break; case 7: $points = $points + 2; break; case 8: $points = $points + 3; break; case 9: $points = $points + 4; break; case 10: $points = $points + 10; break; } echo $points; //prints 2 And that’s all about switch structure and control structure untill next chapter!
See you!!
Total Comments 0






