Skip to content
Advertisement

I need replace variable php ->

Hello I have this variable in php with data but in some pages I don’t have it defined so I need to replace it with some data but my code doesn’t work.

$item->restaurant->name

Try with:

$item->item_category->name = "Mi content replace";

But it does not work. What am I doing wrong? Thanks

Advertisement

Answer

Since PHP 7.0 you can use the null coalescing operator ??. Which tries to fetch your $variable or else default to something else.

$nameOrDefault = $item->restaurant->name ?? "Mi content replace";

Or in a blade context

<p>
    {{ $item->restaurant->name ?? "Mi content replace" }}
</p>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement