
Untitled
By: a guest on
Aug 12th, 2012 | syntax:
None | size: 0.50 KB | hits: 7 | expires: Never
How can i create a function that takes un-assigned variable in PHP
//we have this array
$array = array();
isset($array["test"]);//this one is valid
myFunc($array["test"]);//this one is not valid
function myFunc(&$val)
{
return isset($val);
}
var_dump(myFunc($undefined));
<?php
function defArrayElem(&$array, $key, $default=null) {
if(!isset($array[$key])) {$array[$key]=$default;}
}
//repeat for all the $_GET/$_POST vars you expect...
defArrayElem($_GET,'keyname','default-value');
?>