Jump to content

[SOLVED] running php functions on command line


abazoskib

Recommended Posts

Didn't unerstood your first question.

 

Maybe neither the second one... lol... but when you run php from CLI, you can get the count of parameters with the superglobal $_SERVER['argc'] - arguments count - and you can get all the parameters with the superglobal array $_SERVER['argv'] - arguments values.

 

Example:

 

script.php

<?php
print 'Total parameters = '. $_SERVER['argc'] . '\n';
print 'Script name = '. $_SERVER['argv'][0] . '\n';

for ($i = 1; $i < $_SERVER['argc']; $i++) {
    print 'Parameter '. $i . ' = '. $_SERVER['argv'][$i] . '\n';
}
?>

 

Then you can run like this:

 

php script.php first second

 

It will print:

 

Total parameters = 3
Script name = script.php
Parameter 1 = first
Parameter 2 = second

 

Note that the index 0 from $_SERVER['argv'] is ALWAYS the script name (it's also a parameter).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.