Become Master Yoda in PHP (Part-3)

Hello, guys welcome back to becoming Master Yoda in PHP. In today’s lesson, we will learn about decision making and loops in PHP. So, let’s get started-

3.1 PHP Decision making and Loops

Conditional Statements:

Conditional Statements or Decision-making statements in PHP decide the direction of the flow of program execution. There are two types of conditional statements: a) if/else, b) switch.

A. If-else Statements

  1. if statement in PHP

if the statement is the simplest. It is used to decide whether a certain block of statements will be executed or not i.e. if a certain condition is true then a block of statements will be executed otherwise not. For Example:

$t =10;
if ($t < 20) {
               echo “Hello World!”;  

if statement

3.3 Output of if statement

2. if-else in PHP

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t do anything. But what if we want to do something else if the condition is false. Then else statement is used. For Example:

$t =100;
if ($t < 20) {
                                             echo “Hello World!”;
               echo “Welcome Back!”;

if- else statement

3.4 Output of if -else statement

3. Nested if-else in PHP

What if there are multiple conditions that we need to apply to execute some block of statements, there nested if-else is used, for this elseif statement is used for multiple conditions. For Example:

$t= 100;
echo “less than 10”;}
echo “less than 50”;}
echo “less than 120”;}
echo “Don’t know the result!!”;}

Nested if-else condition

3.5 Output of nested if-else statement

That all about if-else statements, now let’s talk about switch statements:

B. Switch statements

Switch case statements are a substitute for long if statements that compare a variable to several integral values. The switch() is used for the execution of the switch statement. For Example:

$op = “+”;
switch ($op) {
  case “+”:
    echo $a+$b;
  case “-“:
    echo $a-$b;
  case “*”:
    echo $a*$b;
  case “/”:
    echo $a/$b;
    echo “Can’t figure out!!”;

Switch statement

3.6 Output of switch statement

Here, you can see the break statements being executed again and again. So, let’s understand the break statement: “When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop”.

If we don’t use the break statement in the above program, then all the statements will be printed, as given below:

3.7 Output without break in switch statement

Loops in PHP :

Loops in PHP come into use when we need to repeatedly execute a block of statements.

A. For loop in PHP

The for statement is usually used when we know how many times do, we need to execute a statement or a block of statements. For Example:

for ($i = 0; $i <= 10; $i++) {
               echo “Number : $i \n”;

for loop

3.9 Output of for loop

B. While loop in PHP

The while statement in PHP will be executed as long as a test expression is true. For Example:

$i = 0;
while($i <= 10) {
               echo ” $i \n”;

While loop

3.10 Output of while loop

C. Do While loop in PHP

The do while repeats itself as long as the specified condition is true. It will execute at least once irrespective of test condition. For Example:

$i = 0;
do {
               echo “Hello World <br>”;
} while ($i < 5);

Do- while loop

3.11 Output of do-while loop

D. Foreach loop in PHP

The foreach loop works on arrays, and is usually used through each key/value pair in an array. For Example:

$hsl = array(“Helix”, “Smart”, “Labs”);
foreach ($hsl as $value) {
               echo $value.” “;

foreach loop

3.12Output of foreach loop

That’s all for today, will see you in the next lecture till then Happy Learning!!!

Leave a Comment

Your email address will not be published. Required fields are marked *