How to return 0 instead of null in laravel?

by ryan.murray , in category: PHP General , a year ago

How to return 0 instead of null in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , a year ago

@ryan.murray 

In Laravel, you can use the ?? operator to return a default value if a variable is null.


Here is an example of how you can return 0 instead of null:

1
2
3
4
5
$value = null;

$result = $value ?? 0;

return $result;


In this example, if the $value variable is null, the $result variable will be set to 0 using the ?? operator. You can adjust the default value to whatever you need.