[lang-ref] ( execute_when_its_main ) ( php )
<?php
public function testExecuteWhenItsMain(): void
{
// if (realpath($argv[0]) === __FILE__) {
// if (realpath($argv[0]) === __FILE__) { // on CLI
// if ($_SERVER('SCRIPT_FILENAME') === __FILE__) { // on Web server
/* examples/get_executed_command_path.php
<?php
function printArgv0() {
global $argv;
echo($argv[0]);
}
if (realpath($argv[0]) === __FILE__) {
printArgv0();
}
*/
/* examples/get_executed_command_path_call.php
<?php
require_once('get_executed_command_path.php');
printArgv0();
*/
$out = shell_exec('php examples/get_executed_command_path.php');
$this->assertSame('examples/get_executed_command_path.php', $out);
$out = shell_exec('php examples/get_executed_command_path_call.php');
$this->assertSame('examples/get_executed_command_path_call.php', $out);
}