如果值未设置刀片,如何回显默认值 [英] How to echo a default value if value not set blade
本文介绍了如果值未设置刀片,如何回显默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果未设置给定值,我想知道显示默认值的最佳方式是什么.我在刀片文件中有以下内容(我不能保证设置了密钥,这取决于多种因素).
I would like to know what would be the best way to display a default value if the given value is not set. I have the following in a blade file (I can not guaranty that the key is set, it depends on a multitude of factors).
{{ $foo['bar'] }}
我想知道以下是否是最好的方法,
I would know if the following is the best way to go about it,
{{ (isset($foo['bar']) ? $foo['bar'] : 'baz' }}
或者有更好的方法吗?
谢谢:)
推荐答案
使用 php 的 null 合并运算符:
Use php's null coalesce operator:
{{ $variable ?? "Default Message" }}
<小时>
从 Laravel 5.7 开始移除
使用 Laravel 4.1-5.6,你可以简单地这样做:
With Laravel 4.1-5.6 you could simply do it like this:
{{ $variable or "Default Message" }}
这与:
echo isset($variable) ? $variable : 'Default Message';
这篇关于如果值未设置刀片,如何回显默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文