Return Perl-output to PHP

偶尔善良 提交于 2019-11-28 14:41:29

Quoting from the PHP manual page for exec():

Return Values

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

To get the output of the executed command, be sure to set and use the output parameter.

So one suggestion is stop using exec() and start using passthru() . However, that's nonsense. passthru() doesn't actually return anything. It may be sufficient if all you need to do with $perl_result is print it to the browser, and thus don't really need the output to be stored in a variable at all. But if you need to match against the output, or manipulate it in any way, you don't want passthru().

Instead, try the backtick operator:

<?php
$perl_result = `perl $script_folder $project_folder`;

Or try setting the second argument of exec() to an empty array:

<?php
$perl_result = array();
exec("perl $script_folder $project_folder", $perl_result);

$perl_result = implode("\n", $perl_result);  # array --> string

From php docs,

Return Values

The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

To get the output of the executed command, be sure to set and use the output parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!