Skip to content Skip to sidebar Skip to footer

Executing Python Script With Php Variables

I am writing a simple application that uses information from a form, passes it through $_POST to a PHP script that executes a python script and outputs the results. The problem I a

Solution 1:

Update -

Now that I am aware of PHP, the mistake lies in using the single-quotes '. In PHP, single quoted strings are considered literals, PHP does not evaluate the content inside it. However, double quoted " strings are evaluated and would work as you are expecting them to. This is beautifully summarized in this SO answer. In our case,

$output = passthru("python wordgame2.py $start_word$end_word");

would work, but the following won't -

$output= passthru('python wordgame2.py $start_word$end_word');

Original answer -

I think the mistake lies in

$output = passthru("python wordgame2.py $start_word$end_word");

Try this

$output = passthru("python wordgame2.py ".$start_word." ".$end_word);

Solution 2:

Thank you for your contributions. I have figured out my problem with this simple fix:

$command = 'python wordgame2.py ' . $start_word . ' ' . $end_word;$output = passthru($command);

In order for passthru to properly handle the php variables, it needs to be concatenated into the string before executing.

Solution 3:

well if I understood you want pass a big amount of text like content of something, so the right way is;

$output = passthru("python wordgame2.py ".json_encode($end_word)." ".json_encode($start_word));

Post a Comment for "Executing Python Script With Php Variables"