2

I have some variables defined in a PHP file. I then call a python script from the PHP with these variables as arguments. HOWEVER the values of the variables do not carry over to my python script, it seems as though they as passed as strings and not as variables:

$first = "doggy";
$second = "kitty";

$command = escapeshellcmd('python ./script.py $first $second');
$output = shell_exec($command);

The above code produces not "doggy" and "kitty" respectively in my Python script, but literally "$first" and "$second". I want doggy and kitty.

For example when I print in Python:

print sys.argv[1];
>>$first

is the output I am receiving.

My Python script is NOT outputting anything, the script interacts with an API that I wish to use these variables with.

I have tried these previous posts which seem to be near what I am asking. I am either not understanding them, or they are not working. Some answers are too technical or too vague.

Passing value from PHP script to Python script

PHP <-> Python variable exchange

If shell_exec is not the best way for this, I am open to new ideas. Any and all help is appreciated. Thanks for taking the time to look at my post.

3
  • 2
    try replacing single quotes with double quotes: $command = escapeshellcmd("python ./script.py $first $second"); Commented Feb 26, 2018 at 8:00
  • 1
    Use double quotes, not single Commented Feb 26, 2018 at 8:01
  • @Yamakenji, cdarke. Thank you. It has literally been HOURS... Commented Feb 26, 2018 at 8:07

1 Answer 1

3

A single quoted string will be displayed literally, while a double quoted string will interpret things like variables and new line sequences (\n).

Therefore, change from single to double quotes:

$command = escapeshellcmd("python ./script.py $first $second");

Read more about strings in the PHP manual: https://www.php.net/manual/en/language.types.string.php

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Emil, it worked. I am both amazingly frustrated and relieved at the same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.