php / perl 尝试语法“9”?

php / perl Experimenting with syntax "9"?

我目前只是在研究 PHP 及其语法。但我得到的是随机结果。

我的想法是用perl生成HTML然后用PHPdefine替换certian关键字。不管怎么玩,这就是我现在的想法

dino.php

<?php
 $lol = "Seriously, so I can enter text here from DB and stuff...";
function callback($buffer,$lol)

{
  return (str_replace("MSG", "$lol", $buffer));
}

ob_start("callback");

$html = shell_exec("/usr/bin/perl /fox/perl/simple.pl 2>&1");
print ($html);

$css = shell_exec("/usr/bin/perl /fox/perl/css.pl 2>&1");
print ($css);  


ob_end_flush();
?>

simple.pl

#!/usr/local/bin/perl
print <<HTML;
<table width="auto" border="1" class="center" id="container" cellspacing="10">
<td id="main_content" width="auto" ><b>Baa! </b><br />
    Well? what you doing here.. Baaa <br />
     MSG
<br />

</td>

<br />
</table>
HTML

现在,页面呈现完美,但是在字符串 MSG 存在的地方,我得到的结果是 9

Baa!
Well? what you doing here.. Baaa
9 

很好奇是什么导致出现九个,我假设我正在计算某种值但不确定是什么值,如果我是,为什么 9?我应该使用另一种语法来做到这一点吗?

如果我删除带有字符串 ("MSG", "lol", $buffer) 的声明,它会正常运行。用大声笑代替味精。

谢谢。

ob_start 不期望像 $foo 这样的传入变量。如 drew010 所示,您的回调格式必须为:

string handler ( string $buffer [, int $phase ] )

您可以通过多种方式解决这个问题。例如:

ob_start(function($buffer) use ($lol) {
  return (str_replace("MSG", "$lol", $buffer));
});

或者如果您不介意全局变量:

function callback($buffer)
{
  global $lol;
  return (str_replace("MSG", "$lol", $buffer));
}
ob_start("callback")