Laravel 5: Retrieving the Default Value of a Given Variable With value

April 15, 2018 —John Koster

The value function will return the default value of the supplied $value. Although this sounds redundant, if the supplied $value is an instance of the Closure class (an anonymous function), the function will be executed and the value will be returned. If the $value is not an instance of Closure, the value is simply returned.

#Signature

The signature of the value function is:

1function value(
2 $value
3);

#Example Use

Using value with an instance of Closure:

1$value = value(function()
2{
3 return 42;
4});

In the above example, $value would contain the integer 42, because it was returned from the supplied function.

The following example does not use an instance of Closure:

1$value = value(42);

Again, $value would contain the integer 42.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.