PHP if statement

Register here to save your progress

? ? ? ?

Intro

When writing our code, we may want run some specific code only in some conditions. 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.

What are conditional statements used for?

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 
*/

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);

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
*/

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;

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
*/

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.';
}