Bird
0
0

Identify the error in this PHP script intended to run only on CLI:

medium📝 Debug Q14 of 15
PHP - Basics and Execution Model
Identify the error in this PHP script intended to run only on CLI:
if (php_sapi_name() = 'cli') {
    echo "CLI mode";
} else {
    echo "Web mode";
}
ANo error, script runs fine
Bphp_sapi_name() cannot be used in CLI
CMissing semicolon after echo statements
DUsing single equals (=) instead of double equals (==) in condition
Step-by-Step Solution
Solution:
  1. Step 1: Check the condition operator

    The condition uses single equals (=), which is assignment, not comparison.
  2. Step 2: Understand correct comparison syntax

    To compare values, double equals (==) must be used.
  3. Final Answer:

    Using single equals (=) instead of double equals (==) in condition -> Option D
  4. Quick Check:

    Use == for comparison, not = [OK]
Quick Trick: Use == to compare, = is assignment [OK]
Common Mistakes:
  • Confusing = with == in if conditions
  • Thinking php_sapi_name() is unavailable in CLI
  • Ignoring missing semicolons (they are present here)

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes