Command Line argument in PHP

Properties of $argv


1. compile time input

2. its datatype is array

3. these values are space seperated

4. every element has unique index and 0th index is reserved for filename it is automatically assignd by compiler/Integer

5. $argv is a dynamic array list

6. By default it support all the type of datatype int,string,etc but string should be supplied within double qoutes only

7. it is hetrogenus array

8. $argv is a super global varible used for accepting command line

9. no other super global varible can run other command line arguemnt

10. by default datatype of each arguments inside command line is of string

11.by default size of $argv is 1

12.Min size of input is 1

13. min index of $argv is 0


Till Now we know that we can take input using scanner Method implemented by us


No of ways taking input.


1. using Scanner class/Method

2. Command Line Argument


Command Line Argument are the compile time inputs


Generally there are two types of input


1. Runtime Input : 


Example : Input is given after programs starts running

java p1


Enter your name : input supplying...


2. Compile Time Input

javac p1.java 2 2


In Python

runtime input


py p1.py


Enter a Number : 2


Enter a Second No : 2


4


Compile Time : 


py p1.py 2 2


4




Runtime input


php p1.php


Enter a name : awnish


awnish


compiletime Input


php p1.php awnish


awnish



PHP Has inbuilt varible set : SUPER GLOBAL VARIBLE


for example $_POST,$_GET,$_REQUEST....$_FILES


$argv : 


$argv : it is super global varible which is used to accept the argument varible from the compile  time input or (command line argument)


1. print_r($argv);


1.#wap a program in php to find sum and average using command-line argument and size of 


2.#wap a program in php to find sum and average using command-line argument without sizeof


Offset Error in $argv

------------------------


java Index Out of Bound Error is same as Offset Error in php  if you try to get the value of such index or key which

donot exist in array of php it will lead to offset Error


for Example 


php p1.php 10 20 30


total size : 4


0 - p1.php


1 - 10


2 -20


3 -30


if we try to get the value of index greater 3


$argv[4]


$argv[5]


$argv[6]


$argv[7]


$argv[8]...


Notice or Warning Error Undefined Offset Error


min and Maximum of $argv

-------------------------


$argv is special array because in normal array min  = 0th Index


1th Index Min


because file name is at index 0 

$min = $argv[1];

$max = $argv[sizeof($argv)-1];

Calculating the Size of any array 


1.sizeof()


2.count() 




What is difference B/w size of and count ?


1. count : static Array  Runtime
2. sizeof : Dynamic Array  Compile Time


<?php

#wap in php to show use of sizeof and count


print_r(count($argv));

echo "\n";

print_r(sizeof($argv));


Algorithm for calculating the size of Array.


Suppose : $a = [10,20,30,40];

$a[0] = 10 $a[$i]=10 where $i=0;

$a[1] =20 $a[$i]=20 where $i=0+1;

$a[2] =30 $a[$i]=30 where $i=1+1;

$a[3] =40 $a[$i]=40 where $i=2+1;

$a[4] = '' or false $a[$i]='' where $i=3+1; in general $i=$i+1;

$a[5] = ''

$a[6] = ''

$a[7] = ''

$a[8] = ''

$i=0;



Command Line Argument in PHP | Postmoprtem of  Command Line Argument in PHP