开发者社区 问答 正文

php 三目运算符和匿名函数???报错

$example = "Hi";

	true ? (function () use ($example){
	   var_dump($example);
	})  : "";

怎么什么都没有输出

或者当我尝试:

$example = "Hi";

	true ? ($str = function () use ($example){
	   return $example;
	})  : "";

	echo $str;                                                                 

报错:

Catchable fatal error: Object of class Closure could not be converted to string

对匿名函数理解的不是很清楚 那位大牛能讲解一下啊

  万分感谢


展开
收起
爱吃鱼的程序员 2020-06-14 16:53:01 534 分享 版权
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    $str();只能这样用么你只是定义了一个匿名函数但是你并没有执行它

    函数只有在调用的时候,才会被执行吧。。。

    js和php好像都是这样。。。

    回复<aclass='referer'target='_blank'>@kideny:恩继续努力回复<aclass='referer'target='_blank'>@会飞的云:多看书,多看代码,迟早都会精通的。回复<aclass='referer'target='_blank'>@kideny:一样最近弄那个websocket中间各种出错发现编程能力还是不行最近书看的有点乱。。。JavaScript不一样echo.............

    2020-06-14 16:53:19
    赞同 展开评论
问答分类:
PHP
问答地址: