PHP $argc


Introduction

This superglobal variable is available when a PHP script is run from command line (and not when executed from HTTP server's document root). It is an integer that corresponds to number of command line arguments passed to current script. As script's filename has to be entered in command line, minimumn value of $argc is 1. This variable is not available if register_argc_argv directive in php.ini is disabled.

$argc

Following script is expected to be run from command line with 3 arguments including name of script

Example

 Live Demo

<?php
if ($argc!=3){
   echo "invalid number of arguments";
   die();
} else{
   echo "number of arguments is valid";
}
?>

Output

This script is run with invalid number of arguments

C:\xampp\php>php test1.php 1 2 3
invalid number of arguments

This script is run with valid number of arguments

C:\xampp\php>php test1.php 1 2
number of arguments is valid

Updated on: 21-Sep-2020

391 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements