April 14, 2018 —John Koster
The abort_unless
helper is the logical opposite of the abort_if
helper function. Like abort
and abort_if
, the abort_unless
helper function has the potential to abort the executed of the running application; if the argument supplied for $boolean
evaluates to false
, the abort_unless
function will abort execution of the application.
This function allows you to perform a conditional check and invoke the abort
function in one line.
The signature of the abort_unless
function is:
1function abort_unless(2 $boolean,3 $code,4 $message = '',5 array $headers = []6);
The following example assumes that some $user
object exists with the property admin
. The example will check to make sure that the admin
property is true
. If not, the code will abort with a 401
(unauthorized access) error code.
1 2abort_unless($user->admin, 401);
The above code example is equivalent to the following function calls, however, the abort_unless
allows developers reading the code to clearly understand the intent behind the actions taking place:
1if ($user->admin == false) {2 abort(401);3}4 5abort_if($user->admin == false, 401);
∎
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.