Guest User

Untitled

a guest
Feb 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. // This is absolutely okay except that $_REQUEST['test'] is kind of redundant.
  2. echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi';
  3.  
  4. // This is perfect! Shorter and cleaner, but only in this situation.
  5. echo null? : 'replacement if empty';
  6.  
  7. // This line gives error when $_REQUEST['test'] is NOT set.
  8. echo $_REQUEST['test']?: 'hi';
  9.  
  10. // Fetches the value of $_GET['user'] and returns 'nobody'
  11. // if it does not exist.
  12. $username = $_GET['user'] ?? 'nobody';
  13. // This is equivalent to:
  14. $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
  15.  
  16. // Example usage for: Short Ternary Operator
  17. $action = $_POST['action'] ?: 'default';
  18.  
  19. // The above is identical to
  20. $action = $_POST['action'] ? $_POST['action'] : 'default';
  21.  
  22. $username = $_GET['user'] ?? 'nobody';
  23. // equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
  24.  
  25. function isset_or(&$check, $alternate = NULL)
  26. {
  27. return (isset($check)) ? $check : $alternate;
  28. }
  29.  
  30. isset_or($_REQUEST['test'],'hi');
  31.  
  32. /**
  33. * Returns the first entry that passes an isset() test.
  34. *
  35. * Each entry can either be a single value: $value, or an array-key pair:
  36. * $array, $key. If all entries fail isset(), or no entries are passed,
  37. * then first() will return null.
  38. *
  39. * $array must be an array that passes isset() on its own, or it will be
  40. * treated as a standalone $value. $key must be a valid array key, or
  41. * both $array and $key will be treated as standalone $value entries. To
  42. * be considered a valid key, $key must pass:
  43. *
  44. * is_null($key) || is_string($key) || is_int($key) || is_float($key)
  45. * || is_bool($key)
  46. *
  47. * If $value is an array, it must be the last entry, the following entry
  48. * must be a valid array-key pair, or the following entry's $value must
  49. * not be a valid $key. Otherwise, $value and the immediately following
  50. * $value will be treated as an array-key pair's $array and $key,
  51. * respectfully. See above for $key validity tests.
  52. */
  53. function first(/* [(array $array, $key) | $value]... */)
  54. {
  55. $count = func_num_args();
  56.  
  57. for ($i = 0; $i < $count - 1; $i++)
  58. {
  59. $arg = func_get_arg($i);
  60.  
  61. if (!isset($arg))
  62. {
  63. continue;
  64. }
  65.  
  66. if (is_array($arg))
  67. {
  68. $key = func_get_arg($i + 1);
  69.  
  70. if (is_null($key) || is_string($key) || is_int($key) || is_float($key) || is_bool($key))
  71. {
  72. if (isset($arg[$key]))
  73. {
  74. return $arg[$key];
  75. }
  76.  
  77. $i++;
  78. continue;
  79. }
  80. }
  81.  
  82. return $arg;
  83. }
  84.  
  85. if ($i < $count)
  86. {
  87. return func_get_arg($i);
  88. }
  89.  
  90. return null;
  91. }
  92.  
  93. $option = first($option_override, $_REQUEST, 'option', $_SESSION, 'option', false);
Add Comment
Please, Sign In to add comment