Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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 三元条件中的转义字符串/关键字_Php_Laravel_Escaping_Blade - Fatal编程技术网

Php 三元条件中的转义字符串/关键字

Php 三元条件中的转义字符串/关键字,php,laravel,escaping,blade,Php,Laravel,Escaping,Blade,我使用Laravel作为我的PHP框架,使用Blade作为我视图的模板引擎。我喜欢保持代码整洁,所以我经常使用三元运算符来保持条件句简短 我有以下代码 <h4>{{ $product['instalments'] === 0 ? 'this or that' : 'something else' }}</h4> {{$product['instalments']==0?'this or that':'something'} 问题是这会抛出一个错误,我已经把它缩小到在字

我使用Laravel作为我的PHP框架,使用Blade作为我视图的模板引擎。我喜欢保持代码整洁,所以我经常使用三元运算符来保持条件句简短

我有以下代码

<h4>{{ $product['instalments'] === 0 ? 'this or that' : 'something else' }}</h4>
{{$product['instalments']==0?'this or that':'something'}

问题是这会抛出一个错误,我已经把它缩小到在字符串中使用“或”,因为这在Laravel中用作关键字。我知道我可以使用@if指令重写它,但如果可能的话,我希望避免这样做。有人知道我怎样才能逃避这个问题,这样它就不会尝试和评估了吗?

嫌疑犯在
$product['installaments']==0

在尝试从中获取元素之前,需要首先确保
$product
存在

也许您可以将代码更改为:

<h4>{{ empty($product['installments']) ? 'this or that' : 'something else' }}</h4>
{{empty($product['installants'])?'this或that':'something'}

是将其写成“this o.”还是“this o.”选项?干杯,串接字符串可以工作,但有点混乱,如果没有更干净的解决方案,我可能只使用@if。干杯!问题在于包含“or”的字符串,因为$product['instalments']返回0否,php不会弄乱引号中的关键字。您可以编写简单的代码,如echo'this或that';试一试