Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP null使用此值_Php_Syntax - Fatal编程技术网

PHP 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;

在c#中,你能做什么??检查是否为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