📖 Scenario: Imagine you are a PHP developer who wants to understand how PHP scripts run differently when executed from the command line (CLI) versus through a web server. This knowledge helps you write scripts that behave correctly in both environments.
🎯 Goal: You will create a PHP script that detects if it is running in CLI mode or via a web server, and then outputs a message accordingly.
📋 What You'll Learn
Create a PHP script with a variable to detect the execution mode
Use a configuration variable to store the CLI mode identifier
Write a conditional statement to check the execution mode
Print the correct message based on the execution mode
💡 Why This Matters
🌍 Real World
Many PHP scripts need to behave differently when run from the command line versus through a web server, such as maintenance scripts or cron jobs.
💼 Career
Understanding PHP execution environments is important for backend developers to write flexible and environment-aware code.
Progress0 / 4 steps
1
Create a variable to detect PHP execution mode
Create a variable called php_sapi and set it to the value of php_sapi_name() function.
PHP
Hint
The php_sapi_name() function returns the interface type, like 'cli' for command line.
2
Add a configuration variable for CLI mode
Add a variable called cli_mode and set it to the string 'cli'.
PHP
Hint
This variable will help us compare the current mode easily.
3
Write a conditional to check execution mode
Write an if statement that compares $php_sapi with $cli_mode. Inside the if, set a variable $message to 'Running in CLI mode.'. In the else block, set $message to 'Running in Web Server mode.'.
PHP
Hint
Use strict comparison === to check the mode.
4
Print the execution mode message
Write a print statement to display the value of $message.
PHP
Hint
Use print($message); to show the message.
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
Step 1: Understand PHP CLI context
PHP CLI means running PHP scripts directly from the command line interface (terminal), not through a browser.
Step 2: Compare with web server execution
Web server execution requires a browser to request PHP pages, unlike CLI which runs standalone scripts.
Final Answer:
It runs PHP scripts directly in the terminal without a browser. -> Option A
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
Step 1: Identify correct function for checking CLI
php_sapi_name() returns the interface type; 'cli' means command line interface.
Step 2: Validate condition logic
if (php_sapi_name() == 'cli') { echo 'CLI mode'; } correctly compares php_sapi_name() to 'cli' to detect CLI mode.
Final Answer:
if (php_sapi_name() == 'cli') { echo 'CLI mode'; } -> Option C
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
Step 1: Check php_sapi_name() value in CLI
When run in CLI, php_sapi_name() returns 'cli'.
Step 2: Evaluate the if condition
The condition is true, so it echoes "Running in CLI".
Final Answer:
Running in CLI -> Option A
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: