system() not returning result?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashground
Forum Newbie
Posts: 2
Joined: Thu May 13, 2010 3:52 pm

system() not returning result?

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: system() not returning result?

Post 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)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
ashground
Forum Newbie
Posts: 2
Joined: Thu May 13, 2010 3:52 pm

Re: system() not returning result?

Post 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");
Post Reply