Tags

, , ,

wp1//embedr.flickr.com/assets/client-code.js
Flow control is the point where you start making decisions in your code. Next, you execute further code depending on those decisions.
In human language, “if” is a conjunction. Here, in programming, it is a statement.

If(expression){
statement
}

Here, expression is a php expression that evaluates TRUE or FALSE value. If your expression is TRUE, the statement that follows is executed. If it’s FALSE, it’s not executed.
Before moving forward, I’d suggest you to consult any beginner’s PHP book where you can learn about operators – conditional and logical, both. You also need to know about operator precedence.
Let us see one quick example.

//if there is two possibilities
$low = 10;
$high = 30;
$temp = 20;

if ($temp $high){
echo 'Stay inside';
}
else{
echo 'Nice temperature out.';
}

Here we have checked the temperature outside. We have assigned a value (20) to the $temp variable. It is neither lower than the low temperature (10) nor it is higher than the high temperature (30). So our output is obvious. The php expression is FALSE. So the output will be “Nice temperature out.”
Now, we can also write the same code in a different way. It is called ‘Ternary Operator’. This is a built-in operator that acts like an if statement. However, it has an unusual form.

object-oriented-design-patterns-in-php7 for web

object-oriented-design-patterns-in-php7 for web


Here is the example where we are trying to test the same code through ternary operator. I give the full code, so that you can compare it with the if statement.


//if there are two possibilities
$low = 10;
$high = 30;
$temp = 20;

if ($temp $high){
echo 'Stay inside';
}
else{
echo 'Nice temperature out.';
}

echo '
';

//we can also write it using ternary operator
$ter = ($temp $high) ? "Stay in" : "Go out";
echo $ter;

In the ternary operator, we put the expression part within first bracket. After that, we have used a question mark. We keep two possibilities. In addition, to differentiate two possibilities we put a colon between them.

I’m a Full-Time writer. If you find these articles helpful please consider buying any of my books.
You can buy from either Leanpub or
Amazon or from my publisher Apress.

Now, consider a third possibility. To solve that problem, we introduce “elseif” between “if and else”.

if ($temp > $high)
{
echo "Don’t go out.";
}
elseif ($temp < $low)
{
echo " Don’t go out.";
}
else
{
echo " It is good outside.";
}

You can continue using “elseif” as long as you wish. You can test many possibilities.
To test many possibilities, you can use “switch” statement.


// Example of switch statement

$variable = 45;

switch ($variable) {
case 5:
case 10:
case 14:
echo 'Too cold.';
break;
case 15:
case 20:
case 22:
echo 'Moderate.';
break;
case 30:
case 35:
case 40:
echo 'Too hot.';
break;

default:
echo 'If all cases fail, this will be the final value: it is not good out.';
break;
}

Here a temperature is given and we test that temperature against many possibilities. We can write as many “cases” as possible, it is just like “elseif”.
Now comes the best part of flow control.
Loop.
You know that computers are great at doing repetitive tasks. Moreover, we are happy knowing that, because we don’t like doing repetitive tasks. That makes computer strong and popular. Like other programming languages, php also comes with standard loops that help computer doing the repetitive tasks easily.
Here is an example of “for loop”.

//$index = 1;

for ($index = 0; $index < 3; $index++) {

if ($index == 0){continue;}
echo "1/$index : " . 1/$index . "
";
//$index++;

}

While loop statement is slightly different in nature and you should handle it carefully. As a beginner, one may make a mess in the code if he or she cannot write while loop properly.
It is very good in calculating few typical applications like gross salaries of 200 different persons. Or you may want to take a list of highest and lowest temperatures of a definite month or a year.
Although the while statement is simple, people may make mistake. It happens because while loop executes the statement repeatedly as long as the condition is true.

$num=1;
while ($num <=10)
{
echo "Increment Number : $num
";>
$num = $num + 1;
}

Forgetfully if you wrote the opposite way, that is, if you wanted to check whether $num is greater than 10, suddenly there would be a lot of confusion. The infinite loop traps the code in such cases and it continues until the end.
On the other hand, foreach loop statement works only on arrays. It is easy to handle. For manipulating and working on array, it is extremely essential.
We write it in this way:
foreach ($array as $key => $value) {

}
Suppose we want to find total numbers of a given array. The code will be like this:
$array = [10, 25, 123654, 7854];
foreach ($array as $value) {

//the total of the numbers
$TotalOfTheNumbers = $TotalOfTheNumbers + $value;

}
Sometimes, you can ignore the key for the numbers as the key starts from 0. In the above code, the values are important.