Guest User

Untitled

a guest
Dec 6th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. echo "My variable value is: " . $my_variable_name;
  2. echo "My index value is: " . $my_array["my_index"];
  3.  
  4. $o = [];
  5. $var = ["",0,null,1,2,3,$foo,$o['myIndex']];
  6. array_walk($var,function($v){
  7. if(!isset($v) || $v == false) {
  8. echo "emptyn";
  9. }
  10. if(empty($v)) {
  11. echo "emptyn";
  12. }
  13. });
  14.  
  15. //Initializing variable
  16. $value = ""; //Initialization value; Examples
  17. //"" When you want to append stuff later
  18. //0 When you want to add numbers later
  19. //isset()
  20. $value = isset($_POST['value']) ? $_POST['value'] : '';
  21. //empty()
  22. $value = !empty($_POST['value']) ? $_POST['value'] : '';
  23.  
  24. set_error_handler('myHandlerForMinorErrors', E_NOTICE | E_STRICT)
  25.  
  26. error_reporting( error_reporting() & ~E_NOTICE )
  27.  
  28. //isset()
  29. $value = isset($array['my_index']) ? $array['my_index'] : '';
  30. //array_key_exists()
  31. $value = array_key_exists('my_index', $array) ? $array['my_index'] : '';
  32.  
  33. list($a, $b) = array(0 => 'a');
  34. //or
  35. list($one, $two) = explode(',', 'test string');
  36.  
  37. // recommended solution
  38. $user_name = $_SESSION['user_name'];
  39. if (empty($user_name)) $user_name = '';
  40.  
  41. OR
  42.  
  43. // just define at the top of the script index.php
  44. $user_name = '';
  45. $user_name = $_SESSION['user_name'];
  46.  
  47. OR
  48.  
  49. $user_name = $_SESSION['user_name'];
  50. if (!isset($user_name)) $user_name = '';
  51.  
  52. // not the best solution, but works
  53. // in your php setting use, it helps hiding site wide notices
  54. error_reporting(E_ALL ^ E_NOTICE);
  55.  
  56. function ifexists($varname)
  57. {
  58. return(isset($$varname)?$varname:null);
  59. }
  60.  
  61. <?=ifexists('name')?>
  62.  
  63. function ifexistsidx($var,$index)
  64. {
  65. return(isset($var[$index])?$var[$index]:null);
  66. }
  67.  
  68. <?=ifexistsidx($_REQUEST,'name')?>
  69.  
  70. $a=10;
  71. if($a==5) { $user_location='Paris';} else { }
  72. echo $user_location;
  73.  
  74. $a=10;
  75. if($a==5) { $user_location='Paris';} else { $user_location='SOMETHING OR BLANK'; }
  76. echo $user_location;
  77.  
  78. $value = filter_input(INPUT_POST, 'value');
  79.  
  80. if (!isset($_POST['value'])) {
  81. $value = null;
  82. } elseif (is_array($_POST['value'])) {
  83. $value = false;
  84. } else {
  85. $value = $_POST['value'];
  86. }
  87.  
  88. $value = (string)filter_input(INPUT_POST, 'value');
  89.  
  90. // Echo whatever the hell this is
  91. <?=$_POST['something']?>
  92.  
  93. // If this is set, echo a filtered version
  94. <?=isset($_POST['something']) ? html($_POST['something']) : ''?>
  95.  
  96. $user_location = null;
  97.  
  98. $greeting = "Hello, ".exst($user_name, 'Visitor')." from ".exst($user_location);
  99.  
  100.  
  101. /**
  102. * Function exst() - Checks if the variable has been set
  103. * (copy/paste it in any place of your code)
  104. *
  105. * If the variable is set and not empty returns the variable (no transformation)
  106. * If the variable is not set or empty, returns the $default value
  107. *
  108. * @param mixed $var
  109. * @param mixed $default
  110. *
  111. * @return mixed
  112. */
  113.  
  114. function exst( & $var, $default = "")
  115. {
  116. $t = "";
  117. if ( !isset($var) || !$var ) {
  118. if (isset($default) && $default != "") $t = $default;
  119. }
  120. else {
  121. $t = $var;
  122. }
  123. if (is_string($t)) $t = trim($t);
  124. return $t;
  125. }
  126.  
  127. <?php
  128. error_reporting(E_ALL); // making sure all notices are on
  129.  
  130. function idxVal(&$var, $default = null) {
  131. return empty($var) ? $var = $default : $var;
  132. }
  133.  
  134. echo idxVal($arr['test']); // returns null without any notice
  135. echo idxVal($arr['hey ho'], 'yo'); // returns yo and assigns it to array index, nice
  136.  
  137. ?>
  138.  
  139. ; Common Values:
  140. ; E_ALL (Show all errors, warnings and notices including coding standards.)
  141. ; E_ALL & ~E_NOTICE (Show all errors, except for notices)
  142. ; E_ALL & ~E_NOTICE & ~E_STRICT (Show all errors, except for notices and coding standards warnings.)
  143. ; E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR (Show only errors)
  144. ; Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
  145. ; Development Value: E_ALL
  146. ; Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT
  147. ; http://php.net/error-reporting
  148. error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
  149.  
  150. function foo()
  151. {
  152. $my_variable_name = '';
  153.  
  154. //....
  155.  
  156. if ($my_variable_name) {
  157. // perform some logic
  158. }
  159. }
  160.  
  161. // verbose way - generally better
  162. if (isset($my_array['my_index'])) {
  163. echo "My index value is: " . $my_array['my_index'];
  164. }
  165.  
  166. // non-verbose ternary example - I use this sometimes for small rules.
  167. $my_index_val = isset($my_array['my_index'])?$my_array['my_index']:'(undefined)';
  168. echo "My index value is: " . $my_index_val;
  169.  
  170. $my_array = array(
  171. 'my_index' => ''
  172. );
  173.  
  174. //...
  175.  
  176. $my_array['my_index'] = 'new string';
  177.  
  178. echo "My variable value is: " . (isset($my_variable_name)) ? $my_variable_name : '';
  179. echo "My index value is: " . (isset($my_array["my_index"])) ? $my_array["my_index"] : '';
  180.  
  181. echo "My variable value is: " . (isset($my_variable_name) && !empty($my_variable_name)) ? $my_variable_name : '';
  182. echo "My index value is: " . (isset($my_array["my_index"]) && !empty($my_array["my_index"])) ? $my_array["my_index"] : '';
Add Comment
Please, Sign In to add comment