PHP If-else-elseif and Switch-case
Categories - PHP Tags - PHP   Maniruzzaman Akash   1 week ago   45   0

PHP If-else-elseif and Switch-case

What is if in programming language.

if is a very important feature of any programming language. It’s used to execute conditional statement based on any condition. In PHP, here is the structure of an if-statement.

if (condition or any expression that returns boolean(true or 1 or false or 0)) {
   // This will be executed, if condition equal to true
}

Example of an if-statement:

<?php
 $age = 28;
 
 if ($age > 18) {
	 echo "You are an Adult !";
 }
?>

Output:

You are an Adult !

if-else

If we want to do something if the first condition does not met, then we can use else like so -

if (condition) {
	// This will be executed, if condition equal to true
} else {
	// This will be executed, if condition is false
}

Example of an if-else statement:

<?php
 $age = 15;
 
 if ($age > 18) {
	 echo "You are an Adult !";
 } else {
    echo "You are a child !";
 }
?>

Output:

You are a child !

if-elseif-else

If we want to check multiple condition, not only a single condition check, then we can use elseif like so -

if (condition) {
	// This will be executed, if condition equal to true
} elseif (other_condition) {
	// This will be executed, if first condition is false and this condition is true
} else {
	// This will be executed, if all above condition is false
}

Example of an if-elseif-else statement:

<?php
 $age = 25;
 
 if ($age >= 50) {
	 echo "You are Old !";
 }  elseif ($age >= 18) {
	 echo "You are an Adult !";
 } else {
    echo "You are a child !";
 }
?>

Output:

You are an Adult !

Switch-case

In PHP or any programming language, we can use switch-case instead of if-else. It can do same work like if-else. Main benifits and purpose of switch-case is we can do operations on same variable multiple times.

Let’s see how switch-case works -

switch (variable) {
  case value1:
    // execute when variable=value1;
    break;

  case value2:
    // execute when variable=value2;
    break;

  case value3:
    // execute when variable=value3;
    break;

  default:
    // execute when nothing matches
	break;
}

If there is multiple case for one condition, then we can write also like this -

case value1:
case value2:
    // execute when variable=value1 and variable=value2;
    break;

Example of switch-case: Check if a character is Vowel or consonent with the help of switch-case and if-else.

<?php

$character = 'a';
$vowel = false;

switch($character) {
	case 'a':
	case 'A':
    	$vowel = true;
    	break;

	case 'e':
	case 'E':
    	$vowel = true;
    	break;
	
	case 'i':
	case 'I':
    	$vowel = true;
    	break;
	
	case 'o':
	case 'O':
    	$vowel = true;
    	break;
	
	case 'u':
	case 'U':
    	$vowel = true;
    	break;

	default:
    	$vowel = false;
		break;
}

if ($vowel === true) {
	echo "The character is a Vowel";
} else {
	echo "The character is a consonent";
}

Output:

The character is a Vowel

More example related to if-else

  1. Simple PHP program to make age teller
  2. Simple PHP program to welcome daytime
  3. Simple PHP program to check even number
  4. Simple PHP program to check odd number
Previous
PHP Operators Part 2 - Comparison Operator, Logical Operator, Conditional Operator
Next
PHP Math Functions - pi, abs, sqrt, min, max, rand, round, floor, ceil, pow, is_nan, is_numeric