问题
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