-2

在我的上一个项目中,我必须指定一个值,通常我使用以下语句:

<?php 
  $true = 1;
  $false = 0;
  $hasAccess = $true ? 1 : 0;
  print $hasAccess;
?>

但这适用于:

<?php 
  $true = 1;
  $false = 0;
  $hasAccess = $true || $false;
  print $hasAccess;
?>

为什么?

更新:我知道什么是 OR / || 是,以及我对它的期望。但我以前从未见过这种可能性。

4

4 回答 4

1

因为

$true ? 1 : 0;

计算结果为1,因为$true为真,并且

$true || $false;

也评估为1,出于同样的原因。

于 2012-04-17T09:17:33.197 回答
1

因为 0 会自动转换为 (bool)false,而其他任何值 (bool)true。所以你基本上说的是:

$hasaccess = true OR false;

另见: http: //php.net/manual/en/language.types.boolean.php http://php.net/manual/en/language.operators.logical.php

于 2012-04-17T09:18:21.263 回答
0

一个 || 这是一个 OR 语句返回天气,其中一个语句的计算结果为真。例子:

$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
于 2012-04-17T09:25:11.133 回答
0

如果表达式的任一侧为真,OR 将返回真。

由于 $true = 1 那么整个表达式为真。

基本上你是在说“如果 $true 是真的,或者 $false 是真的那么 $hasAccess 是真的”

于 2012-04-17T09:20:56.080 回答