PHP Tutorial
PHP conditional statements - if and switch statements

? ? ? ? ? ?

PHP Conditional Statements

When writing our code, we may want to run some code only if some conditions are true. To control the flow of how is our code run we can use conditional statement.

The conditional statements are: 

  • if - used to run a block of code when our condition will be fulfilled (will be true);
  • if ... else - used when we want to run some code if our condition is true and (else) another block of code in case our condition will be false;
  • if ... elseif ... else - used if after checking our first condition and we get false, we want to add another condition that will be checked in order to run some code;
  • switch - used to run mainly one block of code from multiple ones, based on the value of a variable.

if statement

The if statement will check a condition we set and based on that it will choose to run or not a block of code. We put the block of code, that will have to be decided if it will be runned or not, inside two brackets, like this '{ /*code to be runned...*/ }'.

<?php

$price = 20;
$delivery_type = 'standard';

if($price > 15)
{
   $delivery_type = 'free';
};

echo $delivery_type;
/*
it will output 'free' since the variable's $price value was above 15 
*/

if ... else statement

The if ... else statement uses the if statement but also add a block of code that will be executed in case our condition will be false.

Think about it more like a default option if our condition will not be evaluated as true. We will want some code to be run if our condition will be returned as true, but otherwise run the code given in the else statement.

<?php

$age = 14;
$ticket_price = 20;

if($age <= 12)
{
   echo 'You have 50% discount movie price.';

}
else
{
   echo 'You have regular movie price.';
}

/*
 It will output 'You have regular movie price.' since the variable $age is above 12
*/

if ... elseif ... else statement

The if ... elseif ... else statements gives you the possibility to run multiple block code for multiple conditions. You can add 'elseif' statements as many as you need, but you need to start your first condition, on the top, with the 'if' statement. 

The 'else' statement is optional, you can use it if none of the conditions above have return true, to run a default block of code.

<?php

$day_number = 3;

//check day number;
if($day_number === 1)
{
   echo 'Have a great Monday!';
}
elseif($day_number === 2)
{
   echo 'Have a great Tuesday!';
}
elseif($day_number === 3)
{
   echo 'Have a great Wednesday!';
}
elseif($day_number === 6 || $day_number === 7)
{
   echo 'Have a great weekend!';
}
else
{
   echo 'Have a great day!';
}

/*
will output 'Have a great Wednesday!', since $day_number is 3
*/

//check day number again;
if($day_number >= 1 && $day_number <=5)
{
   echo 'Have a great day!';
}
elseif($day_number === 6 && $day_number === 7)
{
   echo 'Have a great weekend!';
}

/*
will output 'Have a great day!', since $day_number is 3
*/

switch statement

The switch statement runs a specific block of code from multiple ones, based on a single expression (that usually is a variable). It works quite similar with multiple 'if' statements, stacked together, checking a single expression.

<?php

//set pressed button
$button_colour = 'red';
$flavour = '';

//pick the flavour
switch ($button_colour)
{
   case 'brown':
      $flavour = 'chocolate';
      break;

   case 'pink':
      $flavour = 'strawberry';
      break;

   case 'red':
      $flavour = 'cherry';
      break;

   case 'yellow':
      $flavour = 'mango';
      break;
   
   default:
      $flavour = 'vanilla';
}

//show picked flavour
echo 'You chose '.$flavour.' ice-cream.';

/*
will output 'You chose cherry ice-cream', since our button colour was red
this expression would be similar with the one bellow
*/

if($button_colour == 'brown')
{
      $flavour = 'chocolate';
}
elseif($button_colour == 'pink')
{
      $flavour = 'strawberry';
}
elseif($button_colour == 'red')
{
      $flavour = 'cherry';
}
elseif($button_colour == 'yellow')
{
      $flavour = 'mango';
}
else
{
      $flavour = 'vanilla';
}

//show picked flavour
echo 'You chose '.$flavour.' ice-cream.';

As visible from the example above, when dealing with many similar repetitive expressions, switch can give a cleaner alternative to the if statement.

break statement

Switch statement works by checking our expression against the value of the each case.

So, it starts by checking the value of our first case, if it matches the value than runs it's block of code, than it moves to the second case and checks it's value, it's maches again than it runs also it's code and than moves to the third case and so on. 

It we want to exit the switch statement once it maches a case, we need to use the break statement. Break statement instructs the execution to exit the switch structure and move forward.

<?php

$day_number = 6;

switch($day_number)
{
   case 1:
   case 2:
   case 3:
   case 4:
   case 5:
      echo 'Today is a working day.';
      break;

   case 6:
   case 7: 
      echo 'Today is weekend.';
      break;

   default:
      echo 'Today is an interesting day';
}

/*

it will output 'Today is weekend'
all the cases that have the value between 1 and 5 will output 'Today is a working day' since they don't have a break statement, same with those that have the value between 6 and 7 will output 'Today is a working day'

*/

default statement

After evaluating the values of all the cases an switch statement may not mach any value, if we want to have block to be ran if there's nothing matched we use the default statement, as the default case. Comparing to the if ... elseif ... else conditional structure , it's the equivalent of the else statement.

<?php

$animal = '';

switch($animal)
{
   case 'dogs':
      echo 'I love dogs.';
         break;
   case 'cats':
      echo 'I love cats.';
         break;
   case 'fish':
      echo 'I love fish.';
         break;

   default:
      echo 'I love all the animals.';
}


/*

will run the default statement as none of the above ones had values to match the expression

*/

What are conditional statements used for?

What will the output of the following code?

<?php

$movie_name = 'Star Wars';
$valid_movie_name = false;

if($movie_name != '')
{
   $valid_movie_name = true;
};

var_dump($valid_movie_name);

What will be the output of the following code?

<?

$weekday = 4;
$regular_price = 100;

$popcorn_included = true;
$ticket_price = 0;

//check if we have weekend discount
if($weekday === 6 || $weekday == 7)
{
   $ticket_price = $regular_price;
}
else 
{
   $ticket_price = $regular_price * 0.8;
}

//check if popcorn included
if($popcorn_included)
{
  $ticket_price += 10; 
}

echo $ticket_price;

What is the output if the following code?

<?

$time = 20;

if($time < 12)
{
   echo 'It\s morning.';
}
elseif($time >= 12)
{
   echo 'It\'s about lunch time!';
}
elseif($time > 19)
{
   echo 'It\'s evening.';
}

What will be the output of the following script?

<?php

switch(!true)
{
    case !!0:
        echo 'A';
        
    case 1 == '1':
        echo 'B';

    case false || !false:
        echo 'C';
        break;
      
    case true && !false:
        echo 'D';
        break;
      
    case true !== 1:
        echo 'E';
      
}

When will 'default' statement be ran in a 'switch' statement?