使用短路获得第一个非空变量 [英] Using short circuiting to get first non-null variable

查看:82
本文介绍了使用短路获得第一个非空变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下等价于PHP(基于JS样式)的等效项:

What's the equivalent of the following (based in JS style) in PHP:

echo $post['story'] || $post['message'] || $post['name'];

因此,如果故事存在,则发布该故事;或者如果消息存在,则以此类推,等等...

So if story exists then post that; or if message exist post that, etc...

推荐答案

它将是(PHP 5.3 +):

echo $post['story'] ?: $post['message'] ?: $post['name'];

对于 PHP 7 :

echo $post['story'] ?? $post['message'] ?? $post['name'];

这篇关于使用短路获得第一个非空变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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