What is the correct syntax for an if statement in PHP?
A
if {condition} then {} B
if (condition) {} C
if condition {} D
if [condition] {}
Analysis & Theory
The correct syntax is `if (condition) { // code }`.
What will the following code output?
```
<?php
$x = 10;
if ($x > 5) {
echo "Yes";
}
?>
```
A
Yes B
No C
Nothing D
Error
Analysis & Theory
`$x` is greater than `5`, so it outputs `Yes`.
What keyword is used to check another condition if the first `if` is false?
A
elseif B
else if C
ifelse D
Both A and B
Analysis & Theory
Both `elseif` and `else if` are valid in PHP for additional conditions.
Which keyword runs code when all previous `if` or `elseif` conditions are false?
A
unless B
otherwise C
else D
ifnot
Analysis & Theory
`else` runs when no previous conditions are true.
What is the output of this code?
```
<?php
$a = 3;
if ($a == 3) {
echo "Three";
} else {
echo "Not Three";
}
?>
```
A
Three B
Not Three C
3 D
Error
Analysis & Theory
Since `$a` equals `3`, it prints `Three`.
Can `if` statements be nested in PHP?
A
Yes B
No C
Only in functions D
Only inside loops
Analysis & Theory
Yes, you can nest `if` statements within each other in PHP.
What is the output of this code?
```
<?php
$x = 7;
if ($x < 5) {
echo "Small";
} else {
echo "Big";
}
?>
```
A
Small B
Big C
7 D
Nothing
Analysis & Theory
`$x` is not less than `5`, so `else` runs and it outputs `Big`.
What will happen if the condition in an `if` statement is false and there is no `else`?
A
It runs the next statement after if block. B
It shows an error. C
It stops the program. D
It prints 'false'.
Analysis & Theory
If the `if` condition is false and there is no `else`, PHP simply skips the `if` block and continues.
Which of the following is a correct full `if-else` statement?
A
if x > y then { echo 'x'; } else { echo 'y'; } B
if (x > y) { echo 'x'; } else { echo 'y'; } C
if x > y { echo 'x'; } elseif { echo 'y'; } D
if x > y { echo 'x' }
Analysis & Theory
The correct syntax is `if (condition) { } else { }`.
What is the output of this?
```
<?php
$x = 5;
if ($x == 5) {
echo "Equal";
}
?>
```
A
Equal B
5 C
Nothing D
Error
Analysis & Theory
`$x` is equal to `5`, so it outputs `Equal`.