Bird
Raised Fist0
PHPprogramming~20 mins

PHP CLI vs web server execution - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PHP CLI vs Web Server Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output difference between CLI and web server
What is the output of this PHP code when run from the command line (CLI) versus when accessed via a web server?
PHP
<?php
if (php_sapi_name() === 'cli') {
    echo "Running in CLI mode";
} else {
    echo "Running in Web Server mode";
}
ARunning in CLI modeRunning in Web Server mode
BRunning in Web Server mode
CNo output
DRunning in CLI mode
Attempts:
2 left
💡 Hint
php_sapi_name() returns the interface PHP is using.
🧠 Conceptual
intermediate
2:00remaining
PHP environment variables in CLI vs Web Server
Which statement about environment variables in PHP CLI and web server execution is true?
AEnvironment variables in CLI are inherited from the shell, but web server variables depend on server configuration.
BEnvironment variables are always the same in CLI and web server modes.
CPHP CLI cannot access environment variables.
DWeb server execution always has fewer environment variables than CLI.
Attempts:
2 left
💡 Hint
Think about where PHP gets environment variables in each mode.
🔧 Debug
advanced
2:00remaining
Why does PHP script behave differently in CLI and web server?
A PHP script uses $_SERVER['REQUEST_METHOD'] to check the HTTP method. It works fine on the web server but throws an error in CLI. Why?
PHP
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    echo "Form submitted";
} else {
    echo "Show form";
}
AIn CLI, $_SERVER['REQUEST_METHOD'] is not set, causing an undefined index error.
BCLI does not support POST requests, so the script crashes.
CThe script needs a special CLI flag to access $_SERVER variables.
DThe web server disables $_SERVER variables in CLI mode.
Attempts:
2 left
💡 Hint
Check if $_SERVER['REQUEST_METHOD'] exists in CLI mode.
📝 Syntax
advanced
2:00remaining
Which PHP code snippet correctly detects CLI mode?
Choose the code snippet that correctly detects if PHP is running in CLI mode.
Aif (php_sapi_name() == 'cli') { echo 'CLI'; }
Bif (PHP_SAPI === 'cli') { echo 'CLI'; }
Cif (php_sapi_name() = 'cli') { echo 'CLI'; }
Dif (PHP_SAPI == 'CLI') { echo 'CLI'; }
Attempts:
2 left
💡 Hint
Check for correct comparison operator and case sensitivity.
🚀 Application
expert
2:00remaining
How to write a PHP script that behaves differently in CLI and web server?
You want a PHP script to print 'Hello CLI' when run from command line and 'Hello Web' when accessed via web server. Which code achieves this?
A
&lt;?php
if (isset($_SERVER['HTTP_HOST'])) {
    echo 'Hello CLI';
} else {
    echo 'Hello Web';
}
B
&lt;?php
if (PHP_SAPI == 'web') {
    echo 'Hello Web';
} else {
    echo 'Hello CLI';
}
C
&lt;?php
if (php_sapi_name() === 'cli') {
    echo 'Hello CLI';
} else {
    echo 'Hello Web';
}
D
&lt;?php
if (php_sapi_name() === 'cgi') {
    echo 'Hello CLI';
} else {
    echo 'Hello Web';
}
Attempts:
2 left
💡 Hint
Use php_sapi_name() to detect CLI mode exactly.

Practice

(1/5)
1. Which statement best describes PHP CLI execution?
easy
A. It runs PHP scripts directly in the terminal without a browser.
B. It requires a web browser to execute PHP scripts.
C. It only works when accessed through a web server.
D. It is used exclusively for rendering HTML pages.

Solution

  1. Step 1: Understand PHP CLI context

    PHP CLI means running PHP scripts directly from the command line interface (terminal), not through a browser.
  2. Step 2: Compare with web server execution

    Web server execution requires a browser to request PHP pages, unlike CLI which runs standalone scripts.
  3. Final Answer:

    It runs PHP scripts directly in the terminal without a browser. -> Option A
  4. Quick Check:

    PHP CLI = Terminal execution [OK]
Hint: CLI means command line, no browser needed [OK]
Common Mistakes:
  • Thinking CLI needs a browser
  • Confusing CLI with web server mode
  • Assuming CLI only renders HTML
2. Which PHP code snippet correctly checks if the script is running in CLI mode?
easy
A. if (php_sapi_name() == 'web') { echo 'CLI mode'; }
B. if ($_SERVER['REQUEST_METHOD'] == 'CLI') { echo 'CLI mode'; }
C. if (php_sapi_name() == 'cli') { echo 'CLI mode'; }
D. if (isset($_GET['cli'])) { echo 'CLI mode'; }

Solution

  1. Step 1: Identify correct function for checking CLI

    php_sapi_name() returns the interface type; 'cli' means command line interface.
  2. Step 2: Validate condition logic

    if (php_sapi_name() == 'cli') { echo 'CLI mode'; } correctly compares php_sapi_name() to 'cli' to detect CLI mode.
  3. Final Answer:

    if (php_sapi_name() == 'cli') { echo 'CLI mode'; } -> Option C
  4. Quick Check:

    php_sapi_name() == 'cli' detects CLI [OK]
Hint: Use php_sapi_name() == 'cli' to detect CLI [OK]
Common Mistakes:
  • Using $_SERVER['REQUEST_METHOD'] for CLI detection
  • Checking for 'web' instead of 'cli'
  • Relying on GET parameters to detect CLI
3. What will be the output when running this PHP script via CLI?
if (php_sapi_name() == 'cli') {
    echo "Running in CLI";
} else {
    echo "Running on Web Server";
}
medium
A. Running in CLI
B. Running on Web Server
C. No output
D. Syntax error

Solution

  1. Step 1: Check php_sapi_name() value in CLI

    When run in CLI, php_sapi_name() returns 'cli'.
  2. Step 2: Evaluate the if condition

    The condition is true, so it echoes "Running in CLI".
  3. Final Answer:

    Running in CLI -> Option A
  4. Quick Check:

    php_sapi_name() == 'cli' true in CLI [OK]
Hint: php_sapi_name() returns 'cli' in terminal [OK]
Common Mistakes:
  • Assuming it prints 'Running on Web Server' in CLI
  • Expecting no output
  • Confusing syntax error with runtime output
4. 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";
}
medium
A. No error, script runs fine
B. php_sapi_name() cannot be used in CLI
C. Missing semicolon after echo statements
D. Using single equals (=) instead of double equals (==) in condition

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]
Hint: 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)
5. You want to write a PHP script that behaves differently when run via CLI or web server. Which approach correctly implements this behavior?
hard
A. Check if $_SERVER['HTTP_HOST'] is set to detect web server mode.
B. Use php_sapi_name() to detect 'cli' and branch logic accordingly.
C. Use isset($_GET) to decide if running on web server.
D. Run the script twice, once in CLI and once in web server.

Solution

  1. Step 1: Understand how to detect execution environment

    php_sapi_name() returns 'cli' for CLI mode, which is reliable for branching logic.
  2. Step 2: Evaluate other options

    Checking $_SERVER['HTTP_HOST'] or $_GET is less reliable; running script twice is inefficient.
  3. Final Answer:

    Use php_sapi_name() to detect 'cli' and branch logic accordingly. -> Option B
  4. Quick Check:

    php_sapi_name() detects CLI vs web [OK]
Hint: Detect CLI with php_sapi_name() for branching [OK]
Common Mistakes:
  • Relying on $_GET or $_SERVER variables that may not exist
  • Running script multiple times unnecessarily
  • Assuming HTTP_HOST is always set in web server
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard