Manjit Baishya
My Coding Roadmap

My Coding Roadmap

Day 15: If Statements in PHP

Day 15: If Statements in PHP

Manjit Baishya's photo
Manjit Baishya
·Sep 9, 2022·

4 min read

If you are familiar with other programming languages, you have most certainly come across conditional statements and you clearly know If is one such conditional statement. Even if you are not familiar with this stuff, I will clearly guide you through it.

Conditional statements are like smart statements that give the source code the power of making its own decision. Our code itself can choose a path out of the many provided to it, based on the data available to it! Don't get too confused, just have a look at the sentences below:

If it rains
    I will not go out
-------------------------------
If I am hungry
    I will eat
Else
    I will not eat

These are called conditional statements. If the condition is met, the first proposed statement gets executed, or else nothing gets executed or the other statement(if available) gets executed. Now, after taking a good look at the example provided above, please read the paragraph above for a better understanding.

Now, if statements in PHP look something like this:

<?php

  //a variable to store the boolean data
  $male = true;

  //if true then execute the following
  if($male)
  {
    //execute the following if true
    echo "You are male.";
  }

 ?>

Output>>>

You are male.

The data provided inside the if condition is boolean in nature, i.e. either true or false. If the data provided inside the if statement has the boolean value true, then the statements inside them will get execute or else nothiung happens!

<?php

  //a variable to store the boolean data
  $male = false;

  //if true then execute the following
  if($male)
  {
    //execute the following if true
    echo "You are male.";
  }

 ?>

Output>>>


Now, what if we put up a condition like "if not this, then that"; how are we supposed to do that? Else is a keyword that is occasionally paired with If in order to provide another option if the condition first provided is not met. Have a look by yourslf:

<?php 

  //variable to store the boolean value
  $male = false;

  //checking conditionality here
  if($male)
  {
    //execute this if bollean value is true
    echo "You are male.";
  }
  else 
  {
    //execute this if bollean value is false
    echo "You are not male.";
  }

 ?>

Output>>>

You are not male.

Now, the next question is, what is we have more than just two options? Well, we can perform those type of actions too!

Along with If and Else statements, we have another one called the Elseif statement which can be used when we required more than two conditions.

<?php

  //variable to store the boolean value
  $grade = "B";

  //checking conditionality here
  if($grade == "A")
  {
    //execute this if this condition s true
    echo "Your grade is A+";
  }
  elseif($grade == "B")
  {
    //execute this if this condition s true
    echo "Your grade is B";
  }
  else
  {
    //if none matches, execute this
    echo "You failed the exam!!!";
  }

 ?>

Output>>>

Your grade is B.

Till now, we have worked a single condition but multiple options, what if we require multiple conditions to be checked at once! Well, we can do that with the AND(&&) operator and the OR(||) operator. A simple example will clear that up.

<?php

  //considering height as first variable
  $height = 'tall';

  //considering gender as second variable
  $gender = 'male';

  if($height == 'tall' && $gender == 'male')
  {
    echo "You are a tall male.";
  }
  else
  {
    echo "You are not a tall male.";
  }

 ?>

Output>>>

You are a tall male.

Now, with all what we have done till now, we can design the following program.

<?php

  //considering height as first variable
  $tall = true;

  //considering gender as second variable
  $male = false;

  //check for the condition
  if($tall && $male)
  {
    //print this if the condition is true
    echo "You are a tall male.";
  }
  // !$height menas if $height is not true
  elseif(!$tall && $male)
  {
    echo "You are a short male.";
  }
  elseif($tall && !$male)
  {
    echo "You are tall but not male";
  }
  else {
    echo "You are not male";
  }

 ?>

Output>>>

You are not a tall male.

That is all for today!

Thank You.

 
Share this