PHP Laravel Blade {{{ $name or 'Default' }}} Printing 1

Asked

Viewed 235 times

0

According to Laravel’s documentation

{{{ $name or 'Default' }}}

should behave as

if(isset($name))
   echo $name
else
   echo 'Default'

or

echo isset($title) ? $title : 'Default'

But it’s coming back 1 as if it were a true.

Now I’m using {{ isset($title) ? $title : 'Default' }} and it’s working, but I’d like to understand what happens in the first version, does anyone know why I get 1?

  • Hello @Vitor. Put your question in English, you are on Sopt. Thank you.

  • In the first example you use the variable $nome and in the second variable $title.

1 answer

1

Try to use {{ $name or 'Default' }} (with only two keys). Apparently the way your code is, the value returned is the logical value of the expression "$name or 'Default'", which in this case returns true because PHP interprets a non-empty string as true.

  • I copied and pasted, changed the variable $name like this: {{ $title or 'Default' }} returned 1 the same way :)

  • 1

    What’s your version of Laravel? What I’ve been looking for, the operator or was discontinued in version 5.5, as commented here. If your PHP version is >= 7, you can use {{ $variable ?? "default" }}. If you’re a minor, use {{ $variable ? $variable : "Default" }}.

  • Thank you so much man, that’s it ... my version is the last, I installed Laravel yesterday, THANK YOU GUY !

  • You’re welcome, man!

  • @Victorhugo If the problem has been solved, you can accept the answer, see here how to do it. This is useful as it indicates to future visitors and other users that this solution worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.