PHP Conditional Statements


Conditional statements like if, else, and elseif in PHP allow you to control the flow of your program based on specific conditions. These are fundamental concepts that help in decision-making during the execution of code. This article covers frequently asked interview questions about if, else, and elseif statements in PHP, along with concise answers.


What is an if statement in PHP?

Answer:
An if statement is a conditional control structure that executes a block of code if the specified condition is true.

$age = 18;
if ($age >= 18) {
   echo "You are eligible to vote.";
}
In this example, the block of code inside the if statement runs only if the $age variable is greater than or equal to 18.

How do you write an if-else statement in PHP?

Answer:
An if-else statement allows you to execute one block of code if the condition is true and another block if the condition is false.

$age = 16;
if ($age >= 18) {
   echo "You are eligible to vote.";
} else {
   echo "You are not eligible to vote.";
}
In this example, the else block will execute if the if condition is false.

What is an elseif statement in PHP, and how does it work?

Answer:
The elseif statement is used to test multiple conditions. If the first if condition is false, PHP checks the elseif condition, and if that is true, it executes the associated block of code.

$score = 75;
if ($score >= 90) {
   echo "Grade: A";
} elseif ($score >= 80) {
   echo "Grade: B";
} elseif ($score >= 70) {
   echo "Grade: C";
} else {
   echo "Grade: F";
}
In this example, different blocks of code are executed depending on the value of $score.

Can you have multiple elseif conditions in PHP?

Answer:
Yes, you can have multiple elseif conditions to check various conditions in sequence. PHP evaluates each condition from top to bottom, and once a condition is met, the corresponding block is executed, and the remaining conditions are ignored.

$number = 5;
if ($number > 10) {
   echo "Greater than 10";
} elseif ($number > 5) {
   echo "Greater than 5";
} elseif ($number == 5) {
   echo "Equal to 5"; // This block executes
} else {
   echo "Less than 5";
}

What is the difference between else if and elseif in PHP?

Answer:

  • else if: You can write else if as two separate words, and it functions similarly to elseif. However, this creates a new block of if nested inside the else block.
  • elseif: A single word is a more compact form and is preferred for clarity and readability.

Both work, but elseif is considered best practice for cleaner code.

// Both are valid:
if ($x > 10) {
   // do something
} elseif ($x > 5) {
   // do something
}
// Or using else if:
if ($x > 10) {
   // do something
} else if ($x > 5) {
   // do something
}

Can an if statement exist without an else or elseif?

Answer:
Yes, an if statement can exist without an accompanying else or elseif. The else and elseif blocks are optional and only needed if you want to handle alternative conditions.

if ($number > 0) {
   echo "Positive number";
}
In this example, there is only an if statement. If the condition is false, no other code will execute.

What is the difference between if and switch in PHP?

Answer:
The if statement is more flexible and can handle complex conditions, while switch is more efficient when dealing with multiple possible values of the same variable.

Example of if:

if ($day == 'Monday') {
   echo "It's Monday";
} elseif ($day == 'Tuesday') {
   echo "It's Tuesday";
} else {
   echo "It's another day";
}

Example of switch:

switch ($day) {
   case 'Monday':
      echo "It's Monday";
      break;
   case 'Tuesday':
      echo "It's Tuesday";
      break;
   default:
      echo "It's another day";
}

Use switch when you have a single variable and multiple possible values.


How can you nest if-else statements in PHP?

Answer:
You can nest if-else statements within one another to check for more specific conditions. Nested if statements are useful when multiple layers of conditions need to be checked.

$age = 20;
$hasID = true;
if ($age >= 18) {
   if ($hasID) {
      echo "Access granted";
   } else {
      echo "ID required";
   }
} else {
   echo "Access denied";
}

In this example, the second if block is nested within the first to check both the age and the presence of an ID.


How do you write shorthand for an if-else statement in PHP?

Answer:
You can use the ternary operator (? :) as shorthand for a simple if-else statement. It takes three operands: a condition, a result if the condition is true, and a result if the condition is false.

$age = 18;
$status = ($age >= 18) ? "Adult" : "Minor";
echo $status; // Output: Adult

How do you combine multiple conditions in an if statement?

Answer:
You can combine multiple conditions in an if statement using logical operators like && (AND) and || (OR).

$age = 18;
$hasID = true;
if ($age >= 18 && $hasID) {
   echo "Access granted";
} else {
   echo "Access denied";
}

In this example, the condition checks if both $age >= 18 and $hasID are true.


Can an else statement exist without an if?

Answer:
No, an else statement cannot exist on its own without a preceding if statement. The else block is dependent on the if block to handle the case when the condition in the if statement is false.


What happens if there is no else and the if condition is false?

Answer:
If there is no else block and the if condition evaluates to false, the script simply skips over the if block and continues executing the code that follows.


How do you use elseif for checking ranges of values?

Answer:
elseif can be used to check if a value falls within different ranges of numbers or conditions.

$temperature = 30;
if ($temperature > 40) {
   echo "Very Hot";
} elseif ($temperature > 30) {
   echo "Hot";
} elseif ($temperature > 20) {
   echo "Warm";
} else {
   echo "Cold";
}
Ads