isset()的PHP简写吗? [英] PHP shorthand for isset()?

查看:387
本文介绍了isset()的PHP简写吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果PHP中不存在将变量分配给某物的方法,是否存在一种简便的方法?

Is there a shorthand way to assign a variable to something if it doesn't exist in PHP?

if(!isset($var) {
  $var = "";
}

我想做类似的事情

$var = $var | "";

推荐答案

PHP 7更新(感谢 PHP 7引入了所谓的 null合并运算符,该运算符将以下语句简化为:

PHP 7 introduces the so called null coalescing operator which simplifies the below statements to:

$var = $var ?? "default";

PHP 7之前的版本

否,对此没有特殊的运算符或特殊的语法.但是,您可以使用三元运算符:

No, there is no special operator or special syntax for this. However, you could use the ternary operator:

$var = isset($var) ? $var : "default";

或者像这样:

isset($var) ?: $var = 'default';

这篇关于isset()的PHP简写吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆