简体   繁体   English

如何在php中使用三元运算符?

[英]How to use ternary operator in php?

When I use ternary operator in PHP I realize that当我在 PHP 中使用三元运算符时,我意识到

0, '0',and null is null

so this is little strange, in my opinion that this value '0' considered as a string and no longer considered as null, but the result in ternary this will return to null value所以这有点奇怪,在我看来,这个值“0”被视为一个字符串,不再被视为 null,但三进制的结果将返回null

maybe this will be help you也许这会对你有所帮助

$a=0
$b='0'
$c=null

$a??'it is null'
//it is null
$b??'it is null'
//it is null
$c??'it is null'
//it is null

$a==null?'it is null':'not null'
//it is null
$b==null?'it is null':'not null'
//it is null
$c==null?'it is null':'not null'
//it is null

so what I want to ask is, how can I make that '0' is not a null value in ternary PHP所以我想问的是,我怎样才能使'0'不是三元PHP中的null值

This is deeper than the ternary operator.这比三元运算符更深。

Boolean logic Boolean逻辑

Boolean logic recognizes true and false values, defines relations and operations with these, builds the Boolean algebra upon logical values and their operations. Boolean 逻辑识别真值和假值,定义这些值的关系和运算,在逻辑值及其运算的基础上构建 Boolean 代数。

PHP (and other languages) supports boolean values of true and false PHP(以及其他语言)支持 boolean 值的truefalse

Truey and falsy真实与虚假

If如果

$a === true

then $a is true .然后$atrue

If如果

$a == true

then $a is truey.那么$a是真实的。 For example, 1 is not true , but it is still truey.例如, 1不是true ,但它仍然是 truey 。

If如果

$b === false

then $b is false .然后$bfalse If如果

$b == false

then $b is falsy.那么$b是假的。 For example, 0 is falsy, but it is not false .例如, 0是假的,但它不是false的。

You wonder about '0' not being false/falsy.你想知道'0'不是假的/假的。 However, logically, the most false string value is '' , which is empty string.但是,从逻辑上讲,最错误的字符串值是'' ,它是空字符串。 '0' is longer than '' . '0'''长。 'false' is also a string and different from false . 'false'也是一个字符串,与false不同。

Ternary operator三元运算符

$a ? $b : $c

evaluates whether $a is truey.评估$a是否为真。 If so, then the expression's result is $b .如果是,则表达式的结果为$b Otherwise the expression's result is $c .否则表达式的结果是$c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM