PHP null使用此值
在c#中,你能做什么??检查是否为null,然后使用如下值PHP null使用此值,php,syntax,Php,Syntax,在c#中,你能做什么??检查是否为null,然后使用如下值 DateTime? today = null; DateTime todayNotNull = today ?? Date.Now; 在PHP中有没有一种速记方法可以做到这一点?是的 $var = ConditionalTest ? ValueIfTrue : ValueIfFalse; 请注意,必须同时使用ValueIfTrue和ValueIfFalse 在您的具体情况下: <?php $today = null;
DateTime? today = null;
DateTime todayNotNull = today ?? Date.Now;
在PHP中有没有一种速记方法可以做到这一点?是的
$var = ConditionalTest ? ValueIfTrue : ValueIfFalse;
请注意,必须同时使用ValueIfTrue
和ValueIfFalse
在您的具体情况下:
<?php
$today = null;
$todayNotNull = isset($today) ? $today : date();
?>
是
请注意,必须同时使用ValueIfTrue
和ValueIfFalse
在您的具体情况下:
<?php
$today = null;
$todayNotNull = isset($today) ? $today : date();
?>
更新:使用隐式空值检查时要小心,比如今天的
$$今天:date()
我强烈建议您显式地测试NULL
更新:使用隐式空值检查时要小心,比如今天的
$$今天:date()
我强烈建议您显式地测试NULL。您可以使用其他用户指示的三元运算符
$today = null;
$todayNotNull = $today ? $today : time();
从PHP 5.3开始,您还可以将其缩短为所需的熟悉语法:
$todayNotNull = $today ?: time();
由于PHP5.3,可以省略
三元运算符。表达式expr1?:如果expr1为expr3,则返回expr1
计算结果为TRUE,否则为expr3
您可以使用其他用户指示的三元运算符
$today = null;
$todayNotNull = $today ? $today : time();
从PHP 5.3开始,您还可以将其缩短为所需的熟悉语法:
$todayNotNull = $today ?: time();
由于PHP5.3,可以省略
三元运算符。表达式expr1?:如果expr1为expr3,则返回expr1
计算结果为TRUE,否则为expr3