dongxi5423 2011-04-17 08:25
浏览 29
已采纳

PHP有短路评估吗?

Given the following code:

  1. if (is_valid($string) && up_to_length($string) && file_exists($file))
  2. {
  3. ......
  4. }

If is_valid($string) returns false, does the php interpreter still check later conditions, like up_to_length($string)?
If so, then why does it do extra work when it doesn't have to?

  • 写回答

8条回答 默认 最新

  • douhao6557 2011-04-17 08:28
    关注

    Yes, the PHP interpreter is "lazy", meaning it will do the minimum number of comparisons possible to evaluate conditions.

    If you want to verify that, try this:

    1. function saySomething()
    2. {
    3. echo 'hi!';
    4. return true;
    5. }
    6. if (false && saySomething())
    7. {
    8. echo 'statement evaluated to true';
    9. }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部