|
together and you want to pass the two numbers into the script when you
start it. You can give PHP the two numbers you want it to add together when
you start the script, on the command line, as follows:
php add.php 2 3
In this statement, the script is named
add.php
, and
2
and
3
are the numbers
you want the script to add together. These numbers are available inside the
script in an array called
$argv
. This array contains all the information on the
command line, as follows:
$argv[0]=add.php
$argv[1]=2
$argv[2]=3
So,
$argv
always contains at least one element — the script name.
Then, in your script, you can use the following statements:
$sum = $argv[1] + $argv[2];
echo $sum;
The output is the following:
5
Another variable is also available called
$argc
. This variable stores the number
of elements in
$argv
. Thus,
$argc
always equals at least 1, which is the name
of the script. In the preceding example,
$argc
equals 3.
126
Part II:Variables and Data
|