Complete the code to print "Hello from CLI" only when running PHP from the command line.
<?php if (php_sapi_name() === [1]) { echo "Hello from CLI\n"; } ?>
The function php_sapi_name() returns the interface type. "cli" means running from command line.
Complete the code to get the current script name differently for CLI and web server.
<?php if (php_sapi_name() === "cli") { $script = [1]; } else { $script = $_SERVER['SCRIPT_NAME']; } echo $script . "\n"; ?>
In CLI mode, $argv[0] holds the script filename.
Fix the error in the code to detect if PHP runs via web server or CLI.
<?php if (PHP_SAPI == [1]) { echo "Running in CLI mode\n"; } else { echo "Running in web server mode\n"; } ?>
PHP_SAPI is a string constant, so it must be compared to a string literal "cli" with double quotes.
Fill both blanks to create an array with script name and execution mode.
<?php
$info = [
'script' => [1],
'mode' => php_sapi_name() [2] "cli" ? 'CLI' : 'Web'
];
print_r($info);
?>Use $argv[0] for script name in CLI and strict equality === for comparison.
Fill all three blanks to create a message showing execution mode and script name.
<?php $mode = php_sapi_name() [1] "cli" ? 'CLI' : 'Web'; $name = [2]; $message = "Running in [3] mode: $name\n"; echo $message; ?>
Use == to compare, $argv[0] for script name in CLI, and the string 'CLI' in the message.