Which of the following is the correct syntax for an if statement in PHP?
easy📝 Syntax Q12 of 15
PHP - Conditional Statements
Which of the following is the correct syntax for an if statement in PHP?
Aif ($a > $b) { echo 'Yes'; }
Bif $a > $b then echo 'Yes';
Cif ($a > $b) echo 'Yes'
Dif $a > $b { echo 'Yes'; }
Step-by-Step Solution
Solution:
Step 1: Recall PHP if statement syntax
PHP requires parentheses around the condition and curly braces around the code block.
Step 2: Check each option for correct syntax
if ($a > $b) { echo 'Yes'; } uses parentheses and curly braces correctly. if ($a > $b) echo 'Yes' misses semicolon after echo and curly braces, causing syntax error. The other options lack parentheses or use invalid 'then' keyword.
Final Answer:
if ($a > $b) { echo 'Yes'; } -> Option A
Quick Check:
Correct if syntax = if ($a > $b) { echo 'Yes'; } [OK]
Quick Trick:Use parentheses for condition and braces for code block [OK]
Common Mistakes:
Omitting parentheses around condition
Using 'then' keyword which PHP does not have
Missing curly braces for code block
Master "Conditional Statements" in PHP
9 interactive learning modes - each teaches the same concept differently