[PHP] Count command-line arguments with $argc

Created on November 30, 2026

PHP's $argc includes the script name. Subtract 1 to count only the arguments supplied after it. Save the following as sample.php.

<?php

echo 'Including the script name: ' . $argc . PHP_EOL;
echo 'Arguments supplied: ' . ($argc - 1) . PHP_EOL;
php sample.php apple banana
Including the script name: 3
Arguments supplied: 2

With no arguments, $argc is 1. PHP's command-line interface enables register_argc_argv so the variable is available.

References

PHP