Pass variable to php script running from command line
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?
php command-line command-line-arguments
add a comment |
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?
php command-line command-line-arguments
add a comment |
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?
php command-line command-line-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?
php command-line command-line-arguments
php command-line command-line-arguments
edited Aug 30 '17 at 2:51
Super Chafouin
4,11564163
4,11564163
asked Jul 26 '11 at 7:33
hd.hd.
6,2843993150
6,2843993150
add a comment |
add a comment |
12 Answers
12
active
oldest
votes
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)
Recommended way is to use getopt()
– ViliusL
Dec 20 '18 at 10:32
add a comment |
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];
had an error : "undefined offset"
– Tarek
Jul 14 '13 at 11:19
thanks everything work now.
– Tarek
Jul 14 '13 at 11:32
2
Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');
– demenvil
Jul 21 '16 at 9:59
add a comment |
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.
Perfect answer! Thanks!
– Reado
Jan 12 '17 at 13:31
This is fantastic - thank you thank you!!
– Tim Curtin
Apr 9 '18 at 23:08
add a comment |
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;
?>
3
this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"
– spybart
Feb 26 '16 at 22:15
add a comment |
Save this code in file myfile.php and run as php myfile.php type=daily
<?php
$a = $argv;
$b = array();
if (count($a) == 1) exit;
foreach ($a as $key => $arg)
if ($key > 0)
list($x,$y) = explode('=', $arg);
$b["$x"] = $y;
?>
If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.
1
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 .
– J. Chomel
Jul 15 '16 at 14:44
Save this code in file myfile.php
– easyaspi
Jul 15 '16 at 15:29
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.
– easyaspi
Jul 15 '16 at 15:35
add a comment |
I strongly recommend the use of getopt.
Documentation at http://php.net/manual/en/function.getopt.php
If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface
Not what is asked, but a great tool....
– Brethlosze
Jan 18 at 15:42
add a comment |
To use $_GET so you dont need to support both if it could be used from command line and from web browser.
if(isset($argv))
foreach ($argv as $arg)
$e=explode("=",$arg);
if(count($e)==2)
$_GET[$e[0]]=$e[1];
else
$_GET[$e[0]]=0;
add a comment |
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);
o/p:-
array (
'name' => 'xyz',
)
add a comment |
<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
echo $arg;
?>
This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script
array(2)
[0]=>
string(13) "phpscript.php"
This one will always execute since will have 1 or 2 args passe
add a comment |
You could use what sep16 on php.net recommends:
<?php
parse_str(implode('&', array_slice($argv, 1)), $_GET);
?>
It behaves exactly like you'd expect with cgi-php.
$ php -f myfile.php type=daily a=1 b=2 b=3
will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').
add a comment |
Just pass it as parameters as follows:
php test.php one two three
and inside test.php:
<?php
if(isset($argv))
foreach ($argv as $arg)
echo $arg;
echo "rn";
?>
add a comment |
if (isset($argv) && is_array($argv))
$param = array();
for ($x=1; $x<sizeof($argv);$x++)
$pattern = '#/(.+)=(.+)#i';
if (preg_match($pattern, $argv[$x]))
$key = preg_replace($pattern, '$1', $argv[$x]);
$val = preg_replace($pattern, '$2', $argv[$x]);
$_REQUEST[$key] = $val;
$$key = $val;
I put parameters in $_REQUEST
$_REQUEST[$key] = $val;
and also usable directly
$$key=$val
use this like that:
myFile.php /key=val
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6826718%2fpass-variable-to-php-script-running-from-command-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
Recommended way is to use getopt()
– ViliusL
Dec 20 '18 at 10:32
add a comment |
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)
Recommended way is to use getopt()
– ViliusL
Dec 20 '18 at 10:32
add a comment |
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)
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)
edited Jul 29 '16 at 16:49
Jared Dunham
383818
383818
answered Jul 26 '11 at 7:41
PtPazuzuPtPazuzu
2,06111210
2,06111210
Recommended way is to use getopt()
– ViliusL
Dec 20 '18 at 10:32
add a comment |
Recommended way is to use getopt()
– ViliusL
Dec 20 '18 at 10:32
Recommended way is to use getopt()
– ViliusL
Dec 20 '18 at 10:32
Recommended way is to use getopt()
– ViliusL
Dec 20 '18 at 10:32
add a comment |
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];
had an error : "undefined offset"
– Tarek
Jul 14 '13 at 11:19
thanks everything work now.
– Tarek
Jul 14 '13 at 11:32
2
Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');
– demenvil
Jul 21 '16 at 9:59
add a comment |
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];
had an error : "undefined offset"
– Tarek
Jul 14 '13 at 11:19
thanks everything work now.
– Tarek
Jul 14 '13 at 11:32
2
Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');
– demenvil
Jul 21 '16 at 9:59
add a comment |
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];
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];
answered Jul 26 '11 at 7:35
ChrFinChrFin
17.7k75391
17.7k75391
had an error : "undefined offset"
– Tarek
Jul 14 '13 at 11:19
thanks everything work now.
– Tarek
Jul 14 '13 at 11:32
2
Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');
– demenvil
Jul 21 '16 at 9:59
add a comment |
had an error : "undefined offset"
– Tarek
Jul 14 '13 at 11:19
thanks everything work now.
– Tarek
Jul 14 '13 at 11:32
2
Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');
– demenvil
Jul 21 '16 at 9:59
had an error : "undefined offset"
– Tarek
Jul 14 '13 at 11:19
had an error : "undefined offset"
– Tarek
Jul 14 '13 at 11:19
thanks everything work now.
– Tarek
Jul 14 '13 at 11:32
thanks everything work now.
– Tarek
Jul 14 '13 at 11:32
2
2
Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');
– demenvil
Jul 21 '16 at 9:59
Use : if (isset($argv[1])) echo . $argv[1]; else die('no ! ');
– demenvil
Jul 21 '16 at 9:59
add a comment |
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.
Perfect answer! Thanks!
– Reado
Jan 12 '17 at 13:31
This is fantastic - thank you thank you!!
– Tim Curtin
Apr 9 '18 at 23:08
add a comment |
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.
Perfect answer! Thanks!
– Reado
Jan 12 '17 at 13:31
This is fantastic - thank you thank you!!
– Tim Curtin
Apr 9 '18 at 23:08
add a comment |
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.
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.
answered Dec 30 '16 at 10:57
fboesfboes
1,5991013
1,5991013
Perfect answer! Thanks!
– Reado
Jan 12 '17 at 13:31
This is fantastic - thank you thank you!!
– Tim Curtin
Apr 9 '18 at 23:08
add a comment |
Perfect answer! Thanks!
– Reado
Jan 12 '17 at 13:31
This is fantastic - thank you thank you!!
– Tim Curtin
Apr 9 '18 at 23:08
Perfect answer! Thanks!
– Reado
Jan 12 '17 at 13:31
Perfect answer! Thanks!
– Reado
Jan 12 '17 at 13:31
This is fantastic - thank you thank you!!
– Tim Curtin
Apr 9 '18 at 23:08
This is fantastic - thank you thank you!!
– Tim Curtin
Apr 9 '18 at 23:08
add a comment |
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;
?>
3
this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"
– spybart
Feb 26 '16 at 22:15
add a comment |
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;
?>
3
this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"
– spybart
Feb 26 '16 at 22:15
add a comment |
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;
?>
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;
?>
answered Jul 26 '11 at 7:40
SubdiggerSubdigger
1,61331739
1,61331739
3
this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"
– spybart
Feb 26 '16 at 22:15
add a comment |
3
this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"
– spybart
Feb 26 '16 at 22:15
3
3
this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"
– spybart
Feb 26 '16 at 22:15
this isn't really that convenient, it doesn't separate out the key and value, it just passes the value "type=daily"
– spybart
Feb 26 '16 at 22:15
add a comment |
Save this code in file myfile.php and run as php myfile.php type=daily
<?php
$a = $argv;
$b = array();
if (count($a) == 1) exit;
foreach ($a as $key => $arg)
if ($key > 0)
list($x,$y) = explode('=', $arg);
$b["$x"] = $y;
?>
If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.
1
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 .
– J. Chomel
Jul 15 '16 at 14:44
Save this code in file myfile.php
– easyaspi
Jul 15 '16 at 15:29
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.
– easyaspi
Jul 15 '16 at 15:35
add a comment |
Save this code in file myfile.php and run as php myfile.php type=daily
<?php
$a = $argv;
$b = array();
if (count($a) == 1) exit;
foreach ($a as $key => $arg)
if ($key > 0)
list($x,$y) = explode('=', $arg);
$b["$x"] = $y;
?>
If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.
1
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 .
– J. Chomel
Jul 15 '16 at 14:44
Save this code in file myfile.php
– easyaspi
Jul 15 '16 at 15:29
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.
– easyaspi
Jul 15 '16 at 15:35
add a comment |
Save this code in file myfile.php and run as php myfile.php type=daily
<?php
$a = $argv;
$b = array();
if (count($a) == 1) exit;
foreach ($a as $key => $arg)
if ($key > 0)
list($x,$y) = explode('=', $arg);
$b["$x"] = $y;
?>
If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.
Save this code in file myfile.php and run as php myfile.php type=daily
<?php
$a = $argv;
$b = array();
if (count($a) == 1) exit;
foreach ($a as $key => $arg)
if ($key > 0)
list($x,$y) = explode('=', $arg);
$b["$x"] = $y;
?>
If you add var_dump($b); before the ?> tag, you will see that the array $b contains type => daily.
edited Jun 19 '18 at 20:43
Santiago
1,211718
1,211718
answered Jul 15 '16 at 14:17
easyaspieasyaspi
411
411
1
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 .
– J. Chomel
Jul 15 '16 at 14:44
Save this code in file myfile.php
– easyaspi
Jul 15 '16 at 15:29
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.
– easyaspi
Jul 15 '16 at 15:35
add a comment |
1
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 .
– J. Chomel
Jul 15 '16 at 14:44
Save this code in file myfile.php
– easyaspi
Jul 15 '16 at 15:29
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.
– easyaspi
Jul 15 '16 at 15:35
1
1
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 .
– J. Chomel
Jul 15 '16 at 14:44
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 .
– J. Chomel
Jul 15 '16 at 14:44
Save this code in file myfile.php
– easyaspi
Jul 15 '16 at 15:29
Save this code in file myfile.php
– easyaspi
Jul 15 '16 at 15:29
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.
– easyaspi
Jul 15 '16 at 15:35
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.
– easyaspi
Jul 15 '16 at 15:35
add a comment |
I strongly recommend the use of getopt.
Documentation at http://php.net/manual/en/function.getopt.php
If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface
Not what is asked, but a great tool....
– Brethlosze
Jan 18 at 15:42
add a comment |
I strongly recommend the use of getopt.
Documentation at http://php.net/manual/en/function.getopt.php
If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface
Not what is asked, but a great tool....
– Brethlosze
Jan 18 at 15:42
add a comment |
I strongly recommend the use of getopt.
Documentation at http://php.net/manual/en/function.getopt.php
If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface
I strongly recommend the use of getopt.
Documentation at http://php.net/manual/en/function.getopt.php
If you wanna the help print out for your options than take a look at https://github.com/c9s/GetOptionKit#general-command-interface
edited Sep 22 '16 at 18:43
answered Sep 20 '16 at 14:52
Francisco LuzFrancisco Luz
1,23811425
1,23811425
Not what is asked, but a great tool....
– Brethlosze
Jan 18 at 15:42
add a comment |
Not what is asked, but a great tool....
– Brethlosze
Jan 18 at 15:42
Not what is asked, but a great tool....
– Brethlosze
Jan 18 at 15:42
Not what is asked, but a great tool....
– Brethlosze
Jan 18 at 15:42
add a comment |
To use $_GET so you dont need to support both if it could be used from command line and from web browser.
if(isset($argv))
foreach ($argv as $arg)
$e=explode("=",$arg);
if(count($e)==2)
$_GET[$e[0]]=$e[1];
else
$_GET[$e[0]]=0;
add a comment |
To use $_GET so you dont need to support both if it could be used from command line and from web browser.
if(isset($argv))
foreach ($argv as $arg)
$e=explode("=",$arg);
if(count($e)==2)
$_GET[$e[0]]=$e[1];
else
$_GET[$e[0]]=0;
add a comment |
To use $_GET so you dont need to support both if it could be used from command line and from web browser.
if(isset($argv))
foreach ($argv as $arg)
$e=explode("=",$arg);
if(count($e)==2)
$_GET[$e[0]]=$e[1];
else
$_GET[$e[0]]=0;
To use $_GET so you dont need to support both if it could be used from command line and from web browser.
if(isset($argv))
foreach ($argv as $arg)
$e=explode("=",$arg);
if(count($e)==2)
$_GET[$e[0]]=$e[1];
else
$_GET[$e[0]]=0;
answered Apr 9 '18 at 13:05
hamboy75hamboy75
346212
346212
add a comment |
add a comment |
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);
o/p:-
array (
'name' => 'xyz',
)
add a comment |
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);
o/p:-
array (
'name' => 'xyz',
)
add a comment |
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);
o/p:-
array (
'name' => 'xyz',
)
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);
o/p:-
array (
'name' => 'xyz',
)
answered Jul 13 '18 at 6:57
RSWRSW
23638
23638
add a comment |
add a comment |
<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
echo $arg;
?>
This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script
array(2)
[0]=>
string(13) "phpscript.php"
This one will always execute since will have 1 or 2 args passe
add a comment |
<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
echo $arg;
?>
This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script
array(2)
[0]=>
string(13) "phpscript.php"
This one will always execute since will have 1 or 2 args passe
add a comment |
<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
echo $arg;
?>
This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script
array(2)
[0]=>
string(13) "phpscript.php"
This one will always execute since will have 1 or 2 args passe
<?php
if (count($argv) == 0) exit;
foreach ($argv as $arg)
echo $arg;
?>
This code should not be used. First of all CLI called like: /usr/bin/php phpscript.php will have one argv value which is name of script
array(2)
[0]=>
string(13) "phpscript.php"
This one will always execute since will have 1 or 2 args passe
answered Mar 10 '13 at 9:55
GrzegorzGrzegorz
1,97711930
1,97711930
add a comment |
add a comment |
You could use what sep16 on php.net recommends:
<?php
parse_str(implode('&', array_slice($argv, 1)), $_GET);
?>
It behaves exactly like you'd expect with cgi-php.
$ php -f myfile.php type=daily a=1 b=2 b=3
will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').
add a comment |
You could use what sep16 on php.net recommends:
<?php
parse_str(implode('&', array_slice($argv, 1)), $_GET);
?>
It behaves exactly like you'd expect with cgi-php.
$ php -f myfile.php type=daily a=1 b=2 b=3
will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').
add a comment |
You could use what sep16 on php.net recommends:
<?php
parse_str(implode('&', array_slice($argv, 1)), $_GET);
?>
It behaves exactly like you'd expect with cgi-php.
$ php -f myfile.php type=daily a=1 b=2 b=3
will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').
You could use what sep16 on php.net recommends:
<?php
parse_str(implode('&', array_slice($argv, 1)), $_GET);
?>
It behaves exactly like you'd expect with cgi-php.
$ php -f myfile.php type=daily a=1 b=2 b=3
will set $_GET['type'] to 'daily', $_GET['a'] to '1' and $_GET['b'] to array('2', '3').
answered Jul 31 '18 at 10:40
K3---rncK3---rnc
3,20612134
3,20612134
add a comment |
add a comment |
Just pass it as parameters as follows:
php test.php one two three
and inside test.php:
<?php
if(isset($argv))
foreach ($argv as $arg)
echo $arg;
echo "rn";
?>
add a comment |
Just pass it as parameters as follows:
php test.php one two three
and inside test.php:
<?php
if(isset($argv))
foreach ($argv as $arg)
echo $arg;
echo "rn";
?>
add a comment |
Just pass it as parameters as follows:
php test.php one two three
and inside test.php:
<?php
if(isset($argv))
foreach ($argv as $arg)
echo $arg;
echo "rn";
?>
Just pass it as parameters as follows:
php test.php one two three
and inside test.php:
<?php
if(isset($argv))
foreach ($argv as $arg)
echo $arg;
echo "rn";
?>
edited Sep 21 '18 at 14:42
cfnerd
2,33392332
2,33392332
answered Sep 21 '18 at 13:12
Sam PrasadSam Prasad
212
212
add a comment |
add a comment |
if (isset($argv) && is_array($argv))
$param = array();
for ($x=1; $x<sizeof($argv);$x++)
$pattern = '#/(.+)=(.+)#i';
if (preg_match($pattern, $argv[$x]))
$key = preg_replace($pattern, '$1', $argv[$x]);
$val = preg_replace($pattern, '$2', $argv[$x]);
$_REQUEST[$key] = $val;
$$key = $val;
I put parameters in $_REQUEST
$_REQUEST[$key] = $val;
and also usable directly
$$key=$val
use this like that:
myFile.php /key=val
add a comment |
if (isset($argv) && is_array($argv))
$param = array();
for ($x=1; $x<sizeof($argv);$x++)
$pattern = '#/(.+)=(.+)#i';
if (preg_match($pattern, $argv[$x]))
$key = preg_replace($pattern, '$1', $argv[$x]);
$val = preg_replace($pattern, '$2', $argv[$x]);
$_REQUEST[$key] = $val;
$$key = $val;
I put parameters in $_REQUEST
$_REQUEST[$key] = $val;
and also usable directly
$$key=$val
use this like that:
myFile.php /key=val
add a comment |
if (isset($argv) && is_array($argv))
$param = array();
for ($x=1; $x<sizeof($argv);$x++)
$pattern = '#/(.+)=(.+)#i';
if (preg_match($pattern, $argv[$x]))
$key = preg_replace($pattern, '$1', $argv[$x]);
$val = preg_replace($pattern, '$2', $argv[$x]);
$_REQUEST[$key] = $val;
$$key = $val;
I put parameters in $_REQUEST
$_REQUEST[$key] = $val;
and also usable directly
$$key=$val
use this like that:
myFile.php /key=val
if (isset($argv) && is_array($argv))
$param = array();
for ($x=1; $x<sizeof($argv);$x++)
$pattern = '#/(.+)=(.+)#i';
if (preg_match($pattern, $argv[$x]))
$key = preg_replace($pattern, '$1', $argv[$x]);
$val = preg_replace($pattern, '$2', $argv[$x]);
$_REQUEST[$key] = $val;
$$key = $val;
I put parameters in $_REQUEST
$_REQUEST[$key] = $val;
and also usable directly
$$key=$val
use this like that:
myFile.php /key=val
answered Jul 17 '17 at 6:12
emmanuelemmanuel
191
191
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6826718%2fpass-variable-to-php-script-running-from-command-line%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown