আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

PHP Control Structure part 2: What is the if-elseif statement in PHP?

What is the if / elseif statement in PHP?

PHP or any other programming language else if-elseif is going to check all condition are gradually between multiple conditions and the first matching condition is to execute in the program. After matching the first condition it does not check the next condition. Let’s say a little easier, suppose you will go to the shop to buy apples, oranges, grapes or bananas, your first choice is apple in this case, if you do not get the apple, you will come with orange, bring the grapes if the orange is not available, and if there is no grapes you will go then come with bananas. This image of real life is resolved with PHP and any Programming Language else if / elseif statement. Notice the following image:

PHP else if Condition

How does the elseIf statement work in PHP?

  • If any condition is false between if and else then else if / elseif is used.
  • If two many logical elseif function in program and one can match then execute inside the block.
  • It is good to know another thing, if multiple else if / elseif logical condition is match then compiler choice the first one.

Flow Chart of elseif statement in PHP

The easy-to-understand how-to works on below flowchart:

PHP else-if flowchart

What is the syntax of elseif Statement?

if(test_expression 1)
{
here goes statements;
}
else if(test_expression 2)
{
here goes statements;
}
else if(test_expression 3)
{
here goes statements;
}
else
{
here goes statements;
}

Example 1:

<?php
$age=16;
if(($age > 0) && ($age<=15)){
print("\nyou are child");
}
elseif(($age > 15) && ($age<=25)){
print("\nyou are Young Man!");
}
elseif($age > 25 && $age<=40){
print("\nyou are Matured");
}
else
{
print("\nSorry, You are not capable to work");
}
echo "\nThis is normal flow ";

What is the switch statement on PHP?

php switch-case-statement

Switch case in PHP or any Programming Language, as like else if / elseif, the difference of if else / elseif statement gradually checks all conditions and match the first condition and execute it. And does not check the next condition. But for the switch case, jump on that condition and execute it. No other condition checks.

Flow Chart of the switch statement on PHP

Switch Statement Flowchart

What is the syntax of the switch statement?

switch ( variable )
{
case value1:
statement;
break;
case value2:
statement;
break;
default:
statement;
}

Example 1:

<?php
$action = "ADD";
switch ($action) {
case "ADD":
echo "Perform actions for adding.";
break;
case "MODIFY":
echo "Perform actions for modifying.";
break;
case "DELETE":
echo "Perform actions for deleting.";
break;
}
?>

Example 2:

<?php
$day= date('w');
//echo $day;
switch ($day) {
   case 1:
           echo "Today is Monday";
           break;
   case 2:
            echo "Today is Tuesday";
           break;
   case 3:
            echo "Today is Wednesday";
           break;
   case 4:
             echo "Today is Thursday";
           break;
   case 5:
            echo "Today is Friday";
           break;
   case 6:
            echo "Today is Saturday";
           break;
   default:
            echo "Today is Sunday";
           break;
}
?>

Is there any difference between elseif and the switch statement in PHP?

There are several differences between the elseif and switch statements in PHP:


switch VS else if / elseif
switch chase never check all condition, jump on the selected condition. No other condition will be check. Else if / elseif the statement will check all the condition and execute the first matching condition. And the next condition is no longer checked.
Switch case always check equal or not elseif / else if always check whether boolean is true or false
Switch faster than elseif elseif slower than switch case
Some cases, the switch statement is much more compact or easier. On the other hand, elseif is comparatively difficult.
switch case is essential to use break statement else if / elseif, do not use break statements.
The value of the variable supports integer data type in the switch expression. elseif / else if, the value of the variable used in an expression supports both data type integer and character.
switch statement can not check the range of multiple values ​​ elseif / else If statement can check the range of multiple values ​​
Only one expression can be used for multiple choice in switch statement. elseif / else If statement, you can use different expressions for each choice.

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

Leave a Reply

Your email address will not be published. Required fields are marked *