Php 布尔值无法正常工作?

发布于 2024-11-17 23:54:15 字数 2734 浏览 3 评论 0原文

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

I have the following code, I seem to be having problems with booleans in php, when ever i dump out the value of bCreatedEvent it is just empty, what am i doing wrong and I'm using booleans wrong? It also fails my logic check so i can redirect at the bottom. I'm fairly new to php, but thought most of this should work similar to 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>";
                }

output

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

野鹿林 2024-11-24 23:54:15

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

另外 & 是一个按位运算符,我认为你的意思是 &&

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

echo false will look empty use var_dump($bEventCreated)

Also & is a bitwise operator I think you mean &&

$bEventCreated = $bEventCreated & $bEventInfoInserted & $bEventRolesInserted;
2024-11-24 23:54:15

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

if($bEventCreated === TRUE)

Make sure you always use identical or not identical comparison operators when doing a boolean condition:

if($bEventCreated === TRUE)

已下线请稍等 2024-11-24 23:54:15

如果值为 false,则不会回显打印 值。例如,请参见:

<?php

$true = true;
$false = false;

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

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

?>

http://codepad.org/98bR4bfn

If a value is false, it will not echo or print a value. See for example:

<?php

$true = true;
$false = false;

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

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

?>

http://codepad.org/98bR4bfn

£噩梦荏苒 2024-11-24 23:54:15

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

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

When you echo a variable it is first converted to a string.

When FALSE or NULL is converted to a string they are converted to an empty string, "". TRUE is converted to "1". Arrays are converted to "Array". Resources are undefined although they are currently converted like "Resource #N". Objects will throw an error if they don't implement __toString().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文