doutian4046 2014-08-06 09:42
浏览 20
已采纳

while($ variable)在PHP中意味着什么?

I am new to PHP and am currently constructing a do/while loop from a tutorial. I would understand if the whole condition was ($variable == true) or ($variable == false), however in the tutorial the while condition is simply while($variable). Could anyone explain this to me?

Here is the tutorial code.

<?php
    $loopCond = false;
    do {
        echo "<p>The loop ran even though the loop condition is false.</p>";
    } while ($loopCond);


    echo "<p>Now the loop is done running.</p>";
?>
  • 写回答

7条回答 默认 最新

  • dongyaobo9081 2014-08-06 09:50
    关注

    All such conditional statements, including while and if, are evaluating the given expression against true. If the expression results in true, the statement executes the action. If it results in false, it won't.

    $var == true is an expression which compares $var to true. The result of this expression is either true or false. The important point to understand here is expressions. Expressions are things which return values. Try var_dump($var == true) or var_dump(4 > 6). It shows you that the expressions return a boolean value. Here:

    if ($var == true)
    

    first $var is compared to true, which yields either the value true or false, which is then evaluated by if whether it's true or false, which then prompts if to execute the following statement or not.

    In other words: it's redundant.

    if ($var)
    

    This simply causes if to evaluate whether $var is true or false and then execute the following statement. The == true is essentially already "built in".

    The following statements are all essentially equivalent:

    if ($var)
    if ($var == true)
    if (($var == true) == true)
    if ((($var == true)) == true) == true)
    ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?