Php 布尔值无法正常工作?
我有以下代码,我似乎在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
echo
false
看起来会是空的,使用var_dump($bEventCreated)
另外
&
是一个按位运算符,我认为你的意思是&&
echo
false
will look empty usevar_dump($bEventCreated)
Also
&
is a bitwise operator I think you mean&&
确保在执行布尔条件时始终使用相同或不相同的比较运算符:
if($bEventCreated === TRUE)
Make sure you always use identical or not identical comparison operators when doing a boolean condition:
if($bEventCreated === TRUE)
如果值为
false
,则不会回显
或打印
值。例如,请参见:http://codepad.org/98bR4bfn
If a value is
false
, it will notecho
orprint
a value. See for example:http://codepad.org/98bR4bfn
PHP 中的布尔值有一些怪癖。这两个链接可能有帮助:
http://pl.php.net/manual/en/language.types .boolean.php
http://pl.php.net /manual/en/language.types.string.php#language.types.string.casting
There are some quirks with booleans in PHP. This two links may help:
http://pl.php.net/manual/en/language.types.boolean.php
http://pl.php.net/manual/en/language.types.string.php#language.types.string.casting
当您回显变量时,它首先会转换为字符串。
当 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().