I have a PHP file that runs on Windows command line, using PowerShell. I can pass in an "associative array" to the file, like so:
php .\my_file.php --first="eke" --second="orie" --third="ubochi afor" --fourth="nkwo"
To inspect the arguments within my_file.php
, one can use the following:
print_r($argv);
Array
(
[0] => .\schedule_mail.php
[1] => --first=eke
[2] => --second=orie
[3] => --third=ubochi afor
[4] => --fourth=nkwo
)
print_r(getopt(null, ['first:', 'second:', 'third:', 'fourth:']));
Array
(
[first] => eke
[second] => orie
[third] => ubochi afor
[fourth] => nkwo
)
This all works well until I replace the passed in data with a variable like so:
$Var = '--first="eke" --second="orie" --third="ubochi afor" --fourth="nkwo"'
php .\my_file.php $Var
In this latter case, here is what I get instead:
print_r($argv);
Array
(
[0] => .\schedule_mail.php
[1] => --first=eke --second=orie --third=ubochi
[2] => afor --fourth=nkwo
)
print_r(getopt(null, ['first:', 'second:', 'third:', 'fourth:']));
Array
(
[first] => eke --second=orie --third=ubochi
)
How can I effectively pass this data to the PHP file?
The reason $Var = '--first="eke" --second="orie" --third="ubochi afor" --fourth="nkwo"'
didn't work, was because you made $Var
one big string.
Thisway what you actually parsed to the file was something like this:
php .\my_file.php '--first="eke" --second="orie" --third="ubochi afor" --fourth="nkwo"'
Powershell interprets this not as multiple parameters to the file, but as one.
What you need to do instead, is make $Var
an array:
$Var = @('--first="eke"', '--second="orie"', '--third="ubochi afor"', '--fourth="nkwo"')
php .\my_file.php $Var
This way Powershell interprets every entry of the array as one unique parameter to the file.
Might be this could help. You can check here -
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.