在我的上一个项目中,我必须指定一个值,通常我使用以下语句:
<?php
$true = 1;
$false = 0;
$hasAccess = $true ? 1 : 0;
print $hasAccess;
?>
但这适用于:
<?php
$true = 1;
$false = 0;
$hasAccess = $true || $false;
print $hasAccess;
?>
为什么?
更新:我知道什么是 OR/|| 以及我必须从中得到什么。但我以前从未见过这种可能性。
因为 0 会自动转换为 (bool)false,而其他任何 (bool) 为 true。所以你基本上说的是:
$hasaccess = true OR false;
另请参阅:http://php.net/manual/en/language.types.boolean.phphttp://php.net/manual/en/language.operators.logical.php
因为
$true ? 1 : 0;
计算结果为 1
,因为 $true
为真,并且
$true || $false;
也评估为1
,原因相同。
如果表达式的任何一侧为 true,则 OR 将返回 true。
由于 $true = 1,则表达式作为一个整体为真。
基本上你是说"如果$true是真的,或者$false是真的,那么$hasAccess是真的"
一个 || 是一个 OR 语句,返回天气,其中一个语句的计算结果为 true。例子:
$bool = true || false;
// $bool = true
$bool = false || true;
// $bool = true
$bool = false || false;
// $bool = false
$bool = false || true || false;
// $bool = true
$bool = false || 1;
// $bool = true
$bool = false || 'test';
// $bool = true