如何在没有isset()的情况下进行快速判断和分配? [英] How to make quick judgement and assignment without isset()?

查看:88
本文介绍了如何在没有isset()的情况下进行快速判断和分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经厌倦了使用以下代码:

I am tired of using code like:

$blog = isset($_GET['blog']) ? $_GET['blog'] : 'default';

但我不能使用:

$blog = $_GET['blog'] || 'default';

是否有任何方法可以执行此操作而无需使用 isset()

Is there any way to do this without using isset()?

推荐答案

您必须等待下一个PHP版本才能获得 coalesce运算符

You have to wait for the next version of PHP to get the coalesce operator

// Uses $_GET['user'] if it exists -- or 'nobody' if it doesn't
$username = $_GET['user'] ?? 'nobody';

// Loads some data from the model and falls back on a default value
$model = Model::get($id) ?? $default_model;

这篇关于如何在没有isset()的情况下进行快速判断和分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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