Bird
0
0

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:
  1. Step 1: Recall PHP if statement syntax

    PHP requires parentheses around the condition and curly braces around the code block.
  2. 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.
  3. Final Answer:

    if ($a > $b) { echo 'Yes'; } -> Option A
  4. 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes