[英]How do I get the return value of a c++ function in php code?
这是一个初学者的问题,但我研究了很多,几乎没有发现任何东西。 许多关于 zend 的文章或示例已不复存在,我无法真正取得进展。 我想从现有的 c++ function 中获取结果,以便在 php 代码中使用它。 我设置了一个超级简单的示例:我的 c++ 代码现在如下所示:
#include<iostream>
int returnnumber() {
return 4 + 2;
}
int main() {
std::cout << "test";
return 0;
}
我的 php 代码如下所示:
<html>
<head>
<title>PHP-Test</title>
</head>
<body>
<?php
$filename = "pathtofile";
if (file_exists($filename)) {
echo "the file exists, ";
} else {
echo "The file doesnt exist";
}
$output = 0;
$output = shell_exec($filename);
//$output = 2
echo $output; ?>
</body>
</html>
output for this is the file exists, test
但是,当我删除$output = 2
的注释时,结果变成the file exists, 2
我真的不明白。 为什么 $output 我的 cout 字符串值而不是我的主要 function 中的返回 integer 值,所以在这种情况下为 0? 以及如何获得值 0,此外,在这种情况下,如何获得int returnnumber()
的结果?
你可以试试这个:
<?php
$out = shell_exec("date > /dev/null 2>&1; echo $?");
echo "result is: $out";
?>
这就是我们如何获得 shell 中任何命令的退出状态。 您执行命令并检查$?
(可能除以 256 但在这种情况下不是)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.