Php Booleans工作不正常?

时间:2011-07-02 21:49:12

标签: php logic boolean

我有以下代码,我似乎在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

5 个答案:

答案 0 :(得分:1)

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

同样&是一个按位运算符我认为你的意思是&&

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

答案 1 :(得分:0)

如果值为false,则不会echoprint值。例如见:

<?php

$true = true;
$false = false;

echo $true."\n";
echo $false."\n";

var_dump($true)."\n";
var_dump($false);

?>

http://codepad.org/98bR4bfn

答案 2 :(得分:0)

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

if($bEventCreated === TRUE)

答案 3 :(得分:0)

答案 4 :(得分:0)

当您回显变量时,它首先转换为字符串。

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