PHP can run in two main ways: from the command line or through a web server. Knowing the difference helps you choose the right way to run your PHP code.
PHP CLI vs web server execution
php script.php # Run PHP script from command line
// Or access PHP file via web server URL like http://localhost/script.phpPHP CLI means running PHP scripts directly in the terminal or command prompt.
Web server execution means PHP runs when a user visits a web page through a browser.
<?php // CLI example echo "Hello from CLI!\n";
<?php // Web server example echo "Hello from web server!";
This program detects if PHP is running from the command line or through a web server and prints a message accordingly.
<?php if (php_sapi_name() == 'cli') { echo "Running in CLI mode\n"; } else { echo "Running in web server mode"; }
CLI scripts do not have access to web server variables like $_SERVER['HTTP_HOST'].
Web server execution can handle HTTP requests and send HTML responses.
Use CLI for scripts that do not need a browser, like automation or maintenance tasks.
PHP CLI runs scripts directly in the terminal without a browser.
Web server execution runs PHP when users visit web pages.
Choose CLI for command-line tasks and web server mode for websites.
Practice
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 AQuick Check:
PHP CLI = Terminal execution [OK]
- Thinking CLI needs a browser
- Confusing CLI with web server mode
- Assuming CLI only renders HTML
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 CQuick Check:
php_sapi_name() == 'cli' detects CLI [OK]
- Using $_SERVER['REQUEST_METHOD'] for CLI detection
- Checking for 'web' instead of 'cli'
- Relying on GET parameters to detect CLI
if (php_sapi_name() == 'cli') {
echo "Running in CLI";
} else {
echo "Running on Web Server";
}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 AQuick Check:
php_sapi_name() == 'cli' true in CLI [OK]
- Assuming it prints 'Running on Web Server' in CLI
- Expecting no output
- Confusing syntax error with runtime output
if (php_sapi_name() = 'cli') {
echo "CLI mode";
} else {
echo "Web mode";
}Solution
Step 1: Check the condition operator
The condition uses single equals (=), which is assignment, not comparison.Step 2: Understand correct comparison syntax
To compare values, double equals (==) must be used.Final Answer:
Using single equals (=) instead of double equals (==) in condition -> Option DQuick Check:
Use == for comparison, not = [OK]
- Confusing = with == in if conditions
- Thinking php_sapi_name() is unavailable in CLI
- Ignoring missing semicolons (they are present here)
Solution
Step 1: Understand how to detect execution environment
php_sapi_name() returns 'cli' for CLI mode, which is reliable for branching logic.Step 2: Evaluate other options
Checking $_SERVER['HTTP_HOST'] or $_GET is less reliable; running script twice is inefficient.Final Answer:
Use php_sapi_name() to detect 'cli' and branch logic accordingly. -> Option BQuick Check:
php_sapi_name() detects CLI vs web [OK]
- Relying on $_GET or $_SERVER variables that may not exist
- Running script multiple times unnecessarily
- Assuming HTTP_HOST is always set in web server
Ready to prove it?
Pass the test and this pattern moves unknown
