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 condition { echo 'Yes'; }
Bif (condition) { echo 'Yes'; }
Cif condition: echo 'Yes'; endif;
Dif [condition] { echo 'Yes'; }
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP if statement syntax

    In PHP, the condition must be inside parentheses and the code block inside curly braces.
  2. Step 2: Check each option

    if (condition) { echo 'Yes'; } matches the correct syntax: if (condition) { ... }. if condition { echo 'Yes'; } misses parentheses, C uses alternate syntax but missing parentheses, D uses square brackets which is invalid.
  3. Final Answer:

    if (condition) { echo 'Yes'; } -> Option B
  4. Quick Check:

    Parentheses around condition = correct syntax [OK]
Quick Trick: Always use parentheses around condition in if [OK]
Common Mistakes:
  • Omitting parentheses around condition
  • Using square brackets instead of parentheses
  • Confusing alternate syntax without proper format

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes