【问题标题】:Php Booleans not working correctly?Php 布尔值无法正常工作?
【发布时间】:2011-07-02 21:49:12
【问题描述】:

我有以下代码,我似乎在 php 中遇到布尔值问题,当我转储 bCreatedEvent 的值时,它只是空的,我做错了什么并且我使用了错误的布尔值?它也未能通过我的逻辑检查,因此我可以在底部重定向。我对 php 还很陌生,但认为其中大部分应该类似于 c/c++。

$dbTheatreCMS = new TheatreCMSDB();
                $iEventID = $dbTheatreCMS->InsertNewEvent($sTitle, $sCompany, $iCreateID, $sNotes, $sPrePrice, $sRegPrice);

                $bEventCreated = False;
                echo "bEventCreated1 = " . $bEventCreated . "<br/>";
                $bEventInfoInserted = True;
                $bEventRolesInserted = True;

                if ($iEventID > 0)
                {
                    $bEventCreated = true;
                    if (isset($_POST["Venues"], $_POST["EventDates"]))
                    {
                        $aiVenueIDs = $_POST["Venues"];
                        $adtEvents = $_POST["EventDates"];
                        if (count($adtEvents) == count($aiVenueIDs)) // These should be the same length
                        {
                            for ($i = 0; $i < count($adtEvents); $i++)
                            {
                                $bEventInfoInserted &= ($dbTheatreCMS->InsertNewEventInfo($iEventID, $aiVenueIDs[$i],$adtEvents[$i]) > 0) ? true :false;
                            }
                        }
                    }

                    if (isset($_POST["Troupers"], $_POST["Roles"]))
                    {
                        $trouperIDs = $_POST["Troupers"];
                        $roles = $_POST["Roles"];
                        if (count($trouperIDs) == count($roles))
                        {
                            for ($i = 0; $i < count($trouperIDs); $i++)
                            {
                                $bEventInfoInserted &=  ($dbTheatreCMS->InsertNewTroupeInfo($iEventID, $trouperIDs[$i],$roles[$i]) > 0)? true:false;
                            }
                        }
                    }
                }

                echo "bEventCreated = " . $bEventCreated . "<br/>";
                echo "bEventInfoInserted = " . $bEventInfoInserted . "<br/>";
                echo "bEventRolesInserted = " . $bEventRolesInserted . "<br/>";

                $bEventCreated = $bEventCreated & $bEventInfoInserted & $bEventRolesInserted;
                echo "$bEventCreated = " . $bEventCreated . "<br/>";

                if($bEventCreated == True)
                {
                    echo "<script type='text/javascript'>window.localStorage.href = 'some page.php';</script>";
                }

输出

bEventCreated1 = 
bEventCreated = 
bEventInfoInserted = 1
bEventRolesInserted = 1
0 = 0

【问题讨论】:

  • 1.我在您的代码中没有看到bCreatedEvent? 2.如果一个变量为假,它不会打印一个0而是一个空白(1为真)。
  • echo 可能无法反映真实的变量内容(尤其是具有“false”的布尔变量),请使用var_dump() 查看变量的内容和类型。
  • 这个 :: var_dump($bEventCreated) 的输出是什么
  • 不,您不只是在一种非常流行的语言的最基本和广泛使用的数据类型中发现了一个错误,请放心。 ;o)

标签: php logic boolean


【解决方案1】:

echo false 使用 var_dump($bEventCreated) 会看起来是空的

另外&amp; 是位运算符我想你的意思是&amp;&amp;

$bEventCreated = $bEventCreated & $bEventInfoInserted & $bEventRolesInserted;

【讨论】:

    【解决方案2】:

    如果一个值为false,则它不会是echoprint 一个值。例如:

    <?php
    
    $true = true;
    $false = false;
    
    echo $true."\n";
    echo $false."\n";
    
    var_dump($true)."\n";
    var_dump($false);
    
    ?>
    

    http://codepad.org/98bR4bfn

    【讨论】:

      【解决方案3】:

      确保在处理布尔条件时始终使用相同或不相同的比较运算符:

      if($bEventCreated === TRUE)

      【讨论】:

      • 是的。事实上,最好始终使用类型安全的 === 和 !== 比较运算符,因为它们比较数据类型和数据值。
      【解决方案4】:

      【讨论】:

        【解决方案5】:

        当你回显一个变量时,它首先被转换为一个字符串。

        当 FALSE 或 NULL 转换为字符串时,它们将转换为空字符串“”。 TRUE 转换为“1”。数组转换为“数组”。资源未定义,尽管它们当前已转换为“资源 #N”。对象如果不实现 __toString() 会抛出错误。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-02
          • 2019-06-09
          • 1970-01-01
          • 1970-01-01
          • 2021-05-02
          • 2012-06-21
          • 2018-07-06
          • 2015-11-15
          相关资源
          最近更新 更多