How to execute an Octave file using php

巧了我就是萌 提交于 2020-01-16 02:26:10

问题


I have an Octave file sr.m

sqrt(3^2 + 4^2)

I have to execute this file using PHP and display output in browser. The sr.m file is resides in octavepgm folder in Desktop.

my php code is

$cmd = "octave3.6.4 -qf C:\\Users\\deepu_000\\Desktop\\octavepgm\\sr.m";
$ex = passthru($cmd, $output);
var_dump($output);

This gives int 1 as output.

How can I make this working..please help me..

The expected output is 5.


回答1:


There's several issues which I explain further down but here's how you do it:

$ php -a
Interactive mode enabled

php > $cmd = "octave -qf --eval 'printf (\"%f\", sqrt (3^2 + 4^2));'";
php > $output = exec ($cmd);
php > var_dump ($output);
string(8) "5.000000"

Issue #1:

You are using passthru() but if you want to capture the output of octave, you should be using exec(). This is explained on the php manual (emphasis added):

The passthru() function is similar to the exec() function in that it executes a command. This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser.

Issue #2:

If you only want the result from Octave, you should use printf() otherwise you get the display of a variable, including the ans = part. Also, with printf() you can better control the format of the output.

Issue #3:

For such simple cases, use Octave's --eval option.



来源:https://stackoverflow.com/questions/31554059/how-to-execute-an-octave-file-using-php

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