Asked  7 Months ago    Answers:  5   Viewed   45 times

I want to pass parameters from PHP Command Line Interface, and then read in the values using PHP script, something like this:

  $name1 = $argv[1];    
  echo $name1;

I pass the variable from CLI like this:

C:xamppphpphp.exe name.php Robby

The above works, I get Robby as the output.

But I want to do something like this:

C:xamppphpphp.exe name.php -inputFirstName="Robby"

So that the user is well informed to enter the correct parameters in the correct places. What is the appropriate way to parse these parameters?



When calling a PHP script from the command line you can use $argc to find out how many parameters are passed and $argv to access them. For example running the following script:

    var_dump($argc); //number of arguments passed 
    var_dump($argv); //the arguments passed

Like this:-

php script.php arg1 arg2 arg3

Will give the following output

array(4) {
  string(21) "d:Scriptsscript.php"
  string(4) "arg1"
  string(4) "arg2"
  string(4) "arg3"

See $argv and $argc for further details.

To do what you want, lets say

php script.php arg1=4

You would need to explode the argument on the equals sign:-

list($key, $val) = explode('=', $argv[1]);

That way you can have whatever you want in front of the equals sign without having to parse it, just check the key=>value pairs are correct. However, that is all a bit of a waste, just instruct the user on the correct order to pass the arguments.

Wednesday, March 31, 2021
answered 7 Months ago
echo php_uname("n");


Wednesday, March 31, 2021
answered 7 Months ago

The documentation of apc.enable_cli, which control whether APC should be activated in CLI mode, says (quoting) :

Mostly for testing and debugging. Setting this enables APC for the CLI version of PHP. Under normal circumstances, it is not ideal to create, populate and destroy the APC cache on every CLI request, but for various test scenarios it is useful to be able to enable APC for the CLI version of PHP easily.

Maybe APC will store the opcodes in memory, but as the PHP executable dies at the end of the script, that memory will be lost : it will not persist between executions of the script.

So opcode-cache in APC is useless in CLI mode : it will not optimize anything, as PHP will still have to re-compile the source to opcodes each time PHP's executable is launched.

Actually, APC doesn't "optimize" : the standard way of executing a PHP script is like this :

  • read the file, and compile it into opcodes
  • execute the opcodes

What APC does is store in opcodes in memory, so the execution of a PHP script becomes :

  • read the opcodes from memory (much faster than compiling the source-code)
  • execute the opcodes

But this means you must have some place in memory to store the opcodes. When running PHP as an Apache module, Apache is responsible for the persistence of that memory segment... When PHP is run from CLI, there is nothing to keep the memory segment there, so it is destroyed at the end of PHP's execution.
(I don't know how it works exactly, but it's something like that, at least in the principles, even if my words are not very "technical" ^^ )

Or, by "optimization" you mean something else than opcode cache, like the configuration directive apc.optimization ? If so, this one has been removed in APC 3.0.13

Wednesday, March 31, 2021
answered 7 Months ago
 I am not seeing MySQL as an installed module.

Did you install it?

# yum install php-mysql

(from the same repo you installed php from).


run this:

yum --enablerepo=webtatic install php-mysql

this tells yum to get the packages from webtatic repository (in addition to system configured repositories). If you want webtatic among system enabled repositories, run:

 yum --enablerepo=webtatic install  webtatic-release
Saturday, May 29, 2021
answered 5 Months ago

The ?type=daily argument (ending up in the $_GET array) is only valid for web-accessed pages.

You'll need to call it like php myfile.php daily and retrieve that argument from the $argv array (which would be $argv[1], since $argv[0] would be myfile.php).

If the page is used as a webpage as well, there are two options you could consider. Either accessing it with a shell script and Wget, and call that from cron:


Or check in the PHP file whether it's called from the command line or not:

if (defined('STDIN')) {
  $type = $argv[1];
} else {
  $type = $_GET['type'];

(Note: You'll probably need/want to check if $argv actually contains enough variables and such)

Monday, June 7, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :