Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is absolutely okay except that $_REQUEST['test'] is kind of redundant.
- echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi';
- // This is perfect! Shorter and cleaner, but only in this situation.
- echo null? : 'replacement if empty';
- // This line gives error when $_REQUEST['test'] is NOT set.
- echo $_REQUEST['test']?: 'hi';
- // Fetches the value of $_GET['user'] and returns 'nobody'
- // if it does not exist.
- $username = $_GET['user'] ?? 'nobody';
- // This is equivalent to:
- $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
- // Example usage for: Short Ternary Operator
- $action = $_POST['action'] ?: 'default';
- // The above is identical to
- $action = $_POST['action'] ? $_POST['action'] : 'default';
- $username = $_GET['user'] ?? 'nobody';
- // equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
- function isset_or(&$check, $alternate = NULL)
- {
- return (isset($check)) ? $check : $alternate;
- }
- isset_or($_REQUEST['test'],'hi');
- /**
- * Returns the first entry that passes an isset() test.
- *
- * Each entry can either be a single value: $value, or an array-key pair:
- * $array, $key. If all entries fail isset(), or no entries are passed,
- * then first() will return null.
- *
- * $array must be an array that passes isset() on its own, or it will be
- * treated as a standalone $value. $key must be a valid array key, or
- * both $array and $key will be treated as standalone $value entries. To
- * be considered a valid key, $key must pass:
- *
- * is_null($key) || is_string($key) || is_int($key) || is_float($key)
- * || is_bool($key)
- *
- * If $value is an array, it must be the last entry, the following entry
- * must be a valid array-key pair, or the following entry's $value must
- * not be a valid $key. Otherwise, $value and the immediately following
- * $value will be treated as an array-key pair's $array and $key,
- * respectfully. See above for $key validity tests.
- */
- function first(/* [(array $array, $key) | $value]... */)
- {
- $count = func_num_args();
- for ($i = 0; $i < $count - 1; $i++)
- {
- $arg = func_get_arg($i);
- if (!isset($arg))
- {
- continue;
- }
- if (is_array($arg))
- {
- $key = func_get_arg($i + 1);
- if (is_null($key) || is_string($key) || is_int($key) || is_float($key) || is_bool($key))
- {
- if (isset($arg[$key]))
- {
- return $arg[$key];
- }
- $i++;
- continue;
- }
- }
- return $arg;
- }
- if ($i < $count)
- {
- return func_get_arg($i);
- }
- return null;
- }
- $option = first($option_override, $_REQUEST, 'option', $_SESSION, 'option', false);
Add Comment
Please, Sign In to add comment