Page 1 of 1

system() not returning result?

Posted: Thu May 13, 2010 3:56 pm
by ashground
I have a Python script that I'd like to display the results of in PHP. This is my code:

Code: Select all

if(system("python /Volumes/code/code/_sendrender/_outputstatus.py")) {
	echo "yes";
} else {
	echo "no";
}
The result is "no". I double-checked the command, and it's correct.

The strange thing is, this works:

Code: Select all

if(system("python -h")) {
	echo "yes";
} else {
	echo "no";
}
That displays the Python help text, followed by "yes".

Does anyone know what's going on? How can I capture the result?

Re: system() not returning result?

Posted: Thu May 13, 2010 4:11 pm
by AbraCadaver
system() returns the last line of the output, so if it's an empty string or 0 then it evaluates to false. Try:

Code: Select all

if(system("python /Volumes/code/code/_sendrender/_outputstatus.py") !== false)
Also, if you actually want to get the output and save it to a var, you need to use exec().

Code: Select all

if(exec("python /Volumes/code/code/_sendrender/_outputstatus.py", $output) !== false)

Re: system() not returning result?

Posted: Thu May 13, 2010 4:20 pm
by ashground
Actually, I found that passthru() is really all I need -- I just need to display the output. But this still doesn't get me the output.

This displays nothing:

Code: Select all

passthru("python /Volumes/code/code/_sendrender/_outputstatus.py");
But this, again, displays the Python help:

Code: Select all

passthru("python -h");