【问题标题】:Query prolog with PHP使用 PHP 查询序言
【发布时间】:2016-12-25 12:47:10
【问题描述】:

我正在尝试使用 PHP 和 prolog 制作数独游戏,想法是使用 prolog 程序解决数独然后通过 PHP 获得结果,我已经写下了这段代码,但我得到一个空输出 我什至尝试使用var_dump 进行测试,我得到string '' (length=0) 这是我的代码:

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Soduku</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="content">
<?php

$output=exec("\"c:/Program Files (x86)/swipl/bin/swipl-win.exe\" -g \"test,halt\" -t \"halt(1)\" test.pl ");
var_dump($output);
echo $output;
echo"<pre>";
print_r($output);
echo"</pre>";

?>


</div>
</body>
</html>

修改后的代码

<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Soduku</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="content">
<?php

exec('"c:\\Program Files (x86)\\swipl\\bin\\swipl-win.exe" -g "st,halt" -t "halt(1)"  -s test.pl',$output,$return_var);
var_dump($output);
echo $return_var;


?>


</div>
</body>
</html>

它显示这个结果

array (size=0)
  empty
0

【问题讨论】:

  • 如果我是你,首先,我会在 PHP 代码之外打开和关闭 &lt;pre&gt; 标记。然后:真的,也使用exec()的最后一个参数,并检查它是0(成功)还是1。如果它真的是0,那么你应该弄清楚你是否以正确的方式打印。 var_dump()肯定不会将数组的内容打印到行中......恐怕你真的需要自己做一些阅读。
  • 当我在 cmd 中执行命令行 "c:\\Program Files (x86)\\swipl\\bin\\swipl-win.exe" -g "st,halt" -t "halt(1)" test.pl 时,它运行良好,但是当我尝试在 exec() 中使用它时,它返回一个空输出,我不确定问题出在哪里,我尝试阅读该文档很多次,但我真的找不到我做错了什么,这就是我在这里寻求帮助的原因
  • $return_var 返回 0 表示成功

标签: php prolog swi-prolog


【解决方案1】:

我终于找到了方法,它太简单了,我只需要执行 swipl.exe 而不是 swipl-win.exe,正确的语法是

$cmd='"c:/Program Files (x86)/swipl/bin/swipl.exe" -g solve_problems,halt -t halt(1) sud.pl' ;

【讨论】:

    【解决方案2】:

    doc 表示exec 返回最后一行,您可能有一个空行作为最后一行。要获得完整的输出,请像这样使用第二个参数:

    exec("\"c:/Program Files (x86)/swipl/bin/swipl-win.exe\" -g \"test,halt\" -t \"halt(1)\" test.pl ", $output);
    var_dump($output);
    

    【讨论】:

    • 我也厌倦了,我得到了这个错误( ! ) Notice: Array to string conversion in C:\wamp\www\Sudoku\index.php on line 15 array (size=0) empty
    • @MessiAdrinho 你能把第 15 行粘贴到这里吗?
    • 我已经更改了我的代码exec('"c:\\Program Files (x86)\\swipl\\bin\\swipl-win.exe" -g "st,halt" -t "halt(1)" test.pl',$output,$return_var); var_dump($output); echo $return_var; 我不再收到该错误但我仍然有一个空输出并且 return_var 返回 0 这意味着命令行已成功执行,我真的无法弄清楚问题出在哪里
    • 梅西如果你从命令行使用相同的参数运行给定的程序,你得到什么输出?另外,不要将返回值与 $output 相关联,因为 $output = ... 因为 $output 是通过引用传递的,并且会在函数运行时获取该值。如果您将该值分配给 $output,那么您将使用最后一行(即空行)覆盖该值,如问题中所述。
    • 它是一个简单的 prolog 程序,它返回“hello”,它在命令行中运行良好,问题是当我通过 php exec() 运行它时我什么都没有得到,输出是空的,我没有将任何内容分配给 $output
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-28
    • 2023-03-10
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多