Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Program entry point

A CLI invocation starts with the selected script and runs its top-level statements.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

php script.php [argument ...]

A CLI invocation starts with the selected script and runs its top-level statements. A user-defined main function runs only if called. In CLI, $argv[0] identifies the script and the remaining entries hold arguments when argv is enabled. Feed an argument array to a function to test defaults independently.

function main(array $args):string{return $args[1]??"default";}
if (isset($argv[1])) {echo main($argv);exit;}
echo json_encode([PHP_SAPI,is_file($argv[0]),main($argv)]),"\n";
$run=proc_open([PHP_BINARY,$argv[0],"short"],[1=>["pipe","w"]],$pipes);echo stream_get_contents($pipes[1]);fclose($pipes[1]);proc_close($run);
["cli",true,"default"]
short

Examples

<?php
function main(array $args):string{return $args[1]??"default";}
if (isset($argv[1])) {echo main($argv);exit;}
echo json_encode([PHP_SAPI,is_file($argv[0]),main($argv)]),"\n";
$run=proc_open([PHP_BINARY,$argv[0],"short"],[1=>["pipe","w"]],$pipes);echo stream_get_contents($pipes[1]);fclose($pipes[1]);proc_close($run);

Result

["cli",true,"default"]
short

Sources