Pass variable to php script running from command line

php command line arguments example
php command line arguments parser
php command line options
php command line script
run php code from command line
php command line arguments with spaces
php echo to command line
run php script from command line with arguments

I have a PHP file that is needed to be run from command line (via crontab). I need to pass type=daily to the file but I don't know how. I tried:

php myfile.php?type=daily

but this error was returned:

Could not open input file: myfile.php?type=daily

What can I do?

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:

#!/bin/sh
wget http://location.to/myfile.php?type=daily

Or check in the php file whether it's called from the commandline 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)

Pass variable to php script running from command line, 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  You can use the next code to work with command line and webbrowser. Put this code above of your php code. It creates a $_GET variable for each command line parameter. In your code you only need to check for $_GET variables then, not worrying about if script is called from webbrowser or command line.

Just pass it as normal parameters and access it in PHP using the $argv array.

php myfile.php daily

and in myfile.php

$type = $argv[1];

PHP: $argv, $argv — Array of arguments passed to script. Description ¶. Contains an array of all the arguments passed to the script when running from the command line. you might expect to find this topic under the heading "positional parameters". To pass command line arguments to the script, we simply put them right after the script name like so The output produced is Argument #0 - script.php Note that the 0th argument is the name of the PHP script that is run.

These lines will convert the arguments of a CLI call like php myfile.php "type=daily&foo=bar" into the well known $_GET-array:

if (!empty($argv[1])) {
  parse_str($argv[1], $_GET);
}

Though it is rather messy to overwrite the global $_GET-array, it converts all your scripts quickly to accept CLI arguments.

See http://php.net/manual/en/function.parse-str.php for details.

How can we execute PHP script using command line?, How do I pass a command line argument to a PHP script? There is no way to set environment variables from the command line specifically for the execution of a script by passing options to the PHP binary. You have a few options: Set the variable globally on the system. Set the variable on the command line before calling the script.

Using getopt() function we can also read parameter from command line just pass value with php running command

php abc.php --name=xyz

abc.php

$val = getopt(null, ["name:"]);
print_r($val); // output: ['name' => 'xyz'];

Using PHP from the command line, How do you execute a PHP script from the command line? I am calling a PHP script whenever a webpage loads. However, there is a parameter that the PHP script needs to run (which I normally pass through the command line when I am testing the script). How can I pass this argument every time the script is run when the page loads?

parameters send by index like other application

php myfile.php type=daily

and then you can gat them like this

<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
    echo $arg;
?>

PHP $_GET, You can easily parse command line arguments into the $_GET variable by using the parse_str() function. <?php to determine the script to execute and ignores command line. you send somthing to the console screen php is waiting for Contains an array of all the arguments passed to the script when running from the command line. run the script. Note: This variable php" from the command line

Command line usage - Manual, Which PHP binary are you using? The CLI or CGI? I suspect you need a CGI version of the binary for PHP to properly handle accept the environment variables​  When I run the script from my command line the variable passing doesn't work:.\test.ps1 hello .\test.ps1 "hello" .\test.ps1 -a "hello" .\test.ps1 -a hello .\test.ps1 -File "hello" As you can see, I have tried many methos with no success, of the script taking the value an outputting it. The script does run, and waits for me to type a value, and

How to pass $_GET variables to a PHP script via the command line , PHP command line FAQ: How do I read command line arguments in If I save this file as argtest.php, and then run it, like this: As you can see, the first argument is actually the name of your PHP script, which is consistent  Running on MacOS (although this could happen on any *nix I suppose), I was unable to get the script to execute without specifically envoking php from the command line: [macg4:valencia/jobs] tim% test.php./test.php: Command not found. However, it worked just fine when php was envoked on the command line: [macg4:valencia/jobs] tim% php test.php

PHP args, I do not have access to the command line directly but I can run cron jobs OK I am trying to pass command line arguments to a PHP script on a  Pass variable to php script running from command line Using getopt() function we can also read parameter from command line just pass value with php running command.

Comments
  • Recommended way is to use getopt()
  • Use : if (isset($argv[1])) { echo . $argv[1]; } else { die('no ! '); }
  • Perfect answer! Thanks!
  • this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"
  • While this may answer the question, consider adding details on how this solution solves the issue. Kindly refer to stackoverflow.com/help/how-to-answer .
  • Save this code in file myfile.php and run as 'php myfile.php type=daily' if you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.