When using PHP, I find myself writing code like this a lot:
$target = $_SESSION[AFTER_LOGIN_TARGET];
unset($_SESSION[AFTER_LOGIN_TARGET]);
return $target;
In Python, there is a dict.pop method that would let me do something similar in one statement, without a temporary variable:
return session.pop(AFTER_LOGIN_TARGET)
Is there a similar function or trick in PHP?
I don’t see a built-in function for this, but you can easily create your own.
You can use it with any array, e.g.
$_SESSION:Short and Sweet
With PHP 7+ you can use the null coalescing operator to shorten this function greatly. You don’t even need
isset()!