Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 0.50 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can i create a function that takes un-assigned variable in PHP
  2. //we have this array
  3. $array  = array();
  4. isset($array["test"]);//this one is valid
  5. myFunc($array["test"]);//this one is not valid
  6.        
  7. function myFunc(&$val)
  8. {
  9.     return isset($val);
  10. }
  11.  
  12. var_dump(myFunc($undefined));
  13.        
  14. <?php
  15. function defArrayElem(&$array, $key, $default=null) {
  16.     if(!isset($array[$key])) {$array[$key]=$default;}
  17. }
  18.  
  19. //repeat for all the $_GET/$_POST vars you expect...
  20. defArrayElem($_GET,'keyname','default-value');
  21. ?>