user7223945
user7223945

Reputation:

How to pass shell variables into a PHP function call as arguments?

I'm calling a PHP script from command line:

/usr/bin/php -r "require '/path/to/php/dummy.php'; echo getFunction(\"DummyParameter\")";

The call works like a charm, and the PHP function returns the correct value. I want to replace DummyParameter with a $ShellVariable. When I replace DummyParameter, it is supposed to be passed as "$ShellVariable" string. Here is what I have tried:

/usr/bin/php -r "require '/path/to/php/dummy.php'; echo getFunction(\"$ShellVariable\")";

Is there any way to pass the shell variable into the PHP script as a function argument?

Thanks in advance.

Upvotes: 2

Views: 174

Answers (1)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

Pass the CLI arguments and use the $argv array, e.g.:

php -r 'printf("%s\n", $argv[1]);' "$(date)"

Sample Output

Tue Nov 29 14:09:07 +07 2016

Upvotes: 2

Related Questions