PHP syntax question: global $ argv, $ argc;

So I have a PHPUnit test and found this code inside a function.

global $argv, $argc;
echo $argc;
print_r($argv);

      

I understand what these variables represent (the arguments passed from the command line), but I've never seen this syntax before: global $argv, $argc;

What exactly is going on here?

+2


a source to share


4 answers


The keyword global

tells PHP to use the global version of the variable version and also display it in the current scope, so variables declared outside of functions / classes can also be accessed there.

Otherwise, trying to read / assign these variables will work with a different local version.

For comparison:



$foo = 1;

function test() {
    $foo = 2;
}

echo $foo; // prints 1

      

compared with...

$foo = 1;

function test() {
    global $foo;
    $foo = 2;
}

echo $foo; // prints 2

      

+3


a source


In languages ​​like Java, they allow you to declare multiple variables of the same type on the same line, separated by commas.

int sum, counter, days, number;

      



Without an IDE to validate the code, I would say it is PHP specific, it just declares these two variables as global

. You can write them separately on two separate lines,

global $argv;
global $argc;

      

+3


a source


argv

and arc

are the parameters passed when starting the PHP script from the command line. As far as I know, these variables should never appear using HTTP.

See: argc and argv in the PHP manual.

Others have already explained what global means. This comma is simply grouping similar ads.

For example, this line will declare a bunch of private variables for a class:

private $name, $email, $datejoined;

      

this is the same as writing:

private $name;
private $email;
private $datejoined;

      

+2


a source


The keyword global

makes the specified variables .. well, global variables accessible from anywhere in that file.

+1


a source







All Articles