我试图将自调用函数的值返回到稍后将使用的变量中,但会出现以下错误解析错误:语法错误,意外T_FUNCTION,期待')'
知道我做错了什么?是这样做的吗?这是代码:
$clientText = call_user_func(function(){
if($lang == 'en'){
return <<<END
<p>hello world</p>
END;
} else {
...
}
});
谢谢。
刚刚发现我的php版本是5.2。是否仍然可以做这样的事情?
答案 0 :(得分:1)
在php 5.2下:
没有什么可以阻止你定义函数然后调用它。
function getClientText($lang){
if($lang == 'en'){
return <<<END
<p>hello world</p>
END;
} else {
...
}
}
$clientText = getClientText($lang);
或者只是这样做:
$clientText = $lang == 'en' ? '<p>hello world</p>' : $something_else
答案 1 :(得分:0)
在我的php上工作(用5.4 alpha 2测试):
$clientText = call_user_func(function($lang)
{
if($lang == 'en')
{
return <<<END
<p>hello world</p>
END;
} else {}
}, 'en');
echo $clientText;
你可能没有php 5.3,但是php 5.2