PHP Tutorial PHP Advanced PHP References

PHP $argc Variable



The PHP $argc variable contains the number of arguments passed to the current script when running from the command line.

Note: The script's filename is always passed as an argument to the script, therefore the minimum value of $argc is 1.

Note: This variable is not available when register_argc_argv in php.ini is disabled.

Example: $argc example

The example below demonstrates the usage of $argc variable.

<?php
var_dump($argc);
?>

When executing the above example with: php script.php arg1 arg2 arg3 arg4

The output of the above script will be similar to:

int(5)

Example: adding multiple arguments

Consider one more example where this variable is used to add multiple number of arguments.

<?php
$n = $argc;
$result = 0;

for($i = 1; $i < $n; $i++)
  $result = $result + $argv[$i];

echo "Addition = ". $result;
?>

When executing the above example with: php script.php 10 20 30

The output of the above script will be similar to:

Addition = 60

Note: This variable is also available as $_SERVER['argc'].

❮ PHP - Predefined Variables