Which PHP keyword is used to execute a block of code when an `if` condition is false?
A
elseif B
else C
endif D
break
Analysis & Theory
`else` is used when the `if` condition is false.
What is the output of this code?
```
<?php
$x = 8;
if ($x > 10) {
echo "Greater";
} else {
echo "Smaller or Equal";
}
?>
```
A
Greater B
Smaller or Equal C
8 D
Error
Analysis & Theory
Since `$x` is not greater than 10, the `else` block runs, outputting `Smaller or Equal`.
Which is correct PHP syntax for an `if...else` statement?
A
if (condition) { } elseif { } B
if (condition) { } else { } C
if condition { } else { } D
if (condition) then { } else { }
Analysis & Theory
Correct syntax is `if (condition) { } else { }`.
What is the output?
```
<?php
$age = 18;
if ($age >= 18) {
echo "Adult";
} else {
echo "Minor";
}
?>
```
A
Adult B
Minor C
18 D
Error
Analysis & Theory
Since `$age` is 18 (which meets the condition `$age >= 18`), it prints `Adult`.
If the `if` condition is true, which block runs?
A
The else block B
The if block C
Both blocks D
Neither block
Analysis & Theory
When the `if` condition is true, only the `if` block runs.
What happens if both `if` and `else` are false?
A
Error B
The else block runs C
Nothing runs D
The if block runs
Analysis & Theory
`else` does not have a condition. It always runs if `if` is false.
What is the output?
```
<?php
$number = -3;
if ($number > 0) {
echo "Positive";
} else {
echo "Negative";
}
?>
```
A
Positive B
Negative C
-3 D
Error
Analysis & Theory
`$number` is -3, which is not greater than 0, so the `else` block runs, outputting `Negative`.
Can the `else` block exist without an `if` block?
A
Yes B
No C
Sometimes D
Depends on the PHP version
Analysis & Theory
`else` must always follow an `if`. It cannot exist alone.
What is the output?
```
<?php
$marks = 75;
if ($marks >= 50) {
echo "Pass";
} else {
echo "Fail";
}
?>
```
A
Pass B
Fail C
75 D
Error
Analysis & Theory
`$marks` is 75 which is greater than or equal to 50, so it outputs `Pass`.
How many times does the `else` block execute if the `if` condition is false?
A
0 B
1 C
Depends on the condition D
Error
Analysis & Theory
If the `if` condition is false, the `else` block executes once.