0
0
PHPprogramming~20 mins

PHP CLI vs web server execution - Practice Questions

Choose your learning style9 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.