Skip to main content

PHP if Statements

By SamK
0
0 recommends
Topic(s)

Conditional statements are used to execute different actions based on varying conditions.

PHP Conditional Statements

When writing code, you frequently need to perform different actions based on various conditions. You can use conditional statements to achieve this.

In PHP, we have the following conditional statements:

  • if statement runs a block of code when the condition is true.
  • if...else statement runs one block of code if the condition is true, and another if it's false.
  • if...elseif...else statement handles multiple conditions, executing different blocks of code depending on which condition is true.

PHP - The if Statement

The if statement runs a block of code only when a given condition is evaluated as true.

Syntax

<?php
if (condition) {
  // code to be executed if condition is true;
}
?>

If statements typically contain conditions that compare two values, as shown below.

// Example usage
<?php
if (4 > 3) { 
  echo "Wishing you a wonderful day!";
}
?>

/*
Output:
Wishing you a wonderful day!
*/

We can also use variables within the if statement:

// Example usage
<?php
$t = 10;
if ($t < 18) {
  echo "Hope you have an amazing day ahead!";
}
?>

/*
Output:
Hope you have an amazing day ahead!
*/

Comparison Operators

To compare two values, we use comparison operators.

Here are the PHP comparison operators for use in if statements:

  • Equal: == : Returns true if the values are equal    
  • Identical: === : Returns true if the values and data types are identical 
  • Not equal: != : Returns true if the values are not equal    
  • Not equal: <> : Returns true if the values are not equal    
  • Not identical: !== : Returns true if the values or data types are not identical  
  • Greater than: > : Returns true if the first value is greater than the second value
  • Less than: < : Returns true if the first value is less than the second value    
  • Greater than or equal to: >= : Returns true if the first value is greater than, or equal to, the second value    
  • Less than or equal to: <= : Returns true if the first value is less than, or equal to, the second value

Logical Operators

To check multiple conditions, we can use logical operators such as the && operator:

// Example usage
<?php
$a = 100;
$b = 44;
$c = 300;
if ($a > $b && $a < $c ) {
  echo "Both conditions are true";
}
?>

/*
Output:
Both conditions are true
*/

Here are the PHP logical operators for use in if statements:

  • And: and : True if both conditions are true    
  • And: && : True if both conditions are true    
  • Or: or : True if either condition is true   
  • Or: || : True if either condition is true 
  • Xor: xor : True if either condition is true, but not both   
  • Not: ! : True if condition is not true

We can compare as many conditions as we want in a single if statement.

// Example usage
<?php
$b = 4;
if ($b == 2 || $b == 3 || $b == 4 || $b == 5) {
  echo "$b is a number between 2 and 5";
}
?>

/*
Output:
4 is a number between 2 and 5
*/

PHP - The if...else Statement

The if...else statement executes a block of code if a specified condition is true, and another block of code if the condition is false.

Syntax

<?php
if (condition) {
  // code to be executed if condition is true;
} else {
  // code to be executed if condition is false;
}
?>
// Example usage
<?php
$t = 12;
if ($t > "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

/*
Output:
Have a good night!
*/

PHP - The if...elseif...else Statement

The if...elseif...else statement executes different codes for more than two conditions.

Syntax

<?php
if (condition) {
  // code to be executed if this condition is true;
} elseif (condition) {
  // code to be executed if first condition is false and this condition is true;
} else {
  // code to be executed if all conditions are false;
}
?>
// Example usage
<?php
$t = 12;
if ($t < "10") {
  echo "Have a good morning!";
} elseif ($t < "20") {
  echo "Have a good day!";
} else {
  echo "Have a good night!";
}
?>

/*
Output:
Have a good day!
*/

Shorthand If

To write shorter code, you can write if statements on one line.

// Example usage
<?php
$a = 6;
if ($a < 10) $b = "Hello";
echo $b
?>

/*
Output:
Hello
*/

Short Hand If...Else

if...else statement can indeed be written in one line, but the syntax is a bit different.

// Example usage
<?php
$a = 13;
$b = $a < 10 ? "Hey" : "What's up";
echo $b;
?>

/*
Output:
What's up
*/

PHP Nested if Statement

You can nest one if statement inside another, allowing you to check additional conditions within the first. This is called nested if statements.

// Example usage
<?php
$a = 12;
if ($a > 9) {
  echo "Above 9";
  if ($a > 18) {
    echo " and also above 18";
  } else {
    echo " but not above 18";
  }
}
?>

/*
Output:
Above 9 but not above 18
*/

Questions & Answers