I’m trying to make a function which will do what the following statements do….
<?php
if(isset($var)){
echo $var;
}
else {
echo "";
}
?>
I have done this so far….
<?php
function echo_ifset($dyn_var){
$var = $dyn_var;
if(isset($$var)){
global $$var;
echo $$var;
}
}
but its not displaying anything when I run..
echo_ifset('message');
// while message is a defined variable.
If you work with a reference, you won’t have any problems with warnings (or errrors, my PHP is a little rusty!) if the variable isn’t defined:
Note the
&before the$vardeclaration, this is the reference operator.Then, you can just call it using:
This method is also great if you want to define a method to set a default value:
Some extra reading material can be found at: http://www.php.net/manual/en/language.references.pass.php