Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing unset variables to functions

My code:

function Check($Variable, $DefaultValue) {
    if(isset($Variable) && $Variable != "" && $Variable != NULL) {
        return $Variable;
    }
    else {
        return $DefaultValue;
    }
}

$a = Check(@$foo, false);
$b = Check(@$bar, "Hello");

//$a now equals false because $foo was not set.
//$b now equals "Hello" because $bar was not set.
  1. When a variable doesn't exist and is passed to the function (suppressing the error) what is actually passed?
  2. Is there any undefined behaviour that this function could exhibit?
  3. Is there a better way of wrapping the testing for variable existence and supplying a default value from a function?
  4. What does isset() check under the hood when testing a variable?

EDIT:

The default value is there to be user defined. Sometimes it will be a number, sometimes a string.

like image 949
Gary Willoughby Avatar asked Apr 05 '11 12:04

Gary Willoughby


People also ask

Can I pass undefined to a function?

We can pass the value 'undefined' to a function with multiple parameters using a variable that has the value of 'undefined'. In JavaScript, whenever a user declares a variable, it automatically contains the value of 'undefined'.

Which function is used to unset a variable?

The unset() function is a predefined variable handling function of PHP, which is used to unset a specified variable. In other words, "the unset() function destroys the variables". The behavior of this function varies inside the user-defined function.

What's the difference between unset () and unlink ()?

The unlink() function is used when you want to delete the files completely. The unset() Function is used when you want to make that file empty.

What does the unset () function do?

unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.


3 Answers

  1. NULL is passed, notice error is raised.
  2. No, function sees only it's parameters (it doesn't care how it is being called)
  3. You can specify default value easily - function func($mandatory, $optional = 'default value');
  4. Isset withing a function on its parameters is pointless, because the parameters are already set in the functions signature.
like image 98
Mārtiņš Briedis Avatar answered Sep 22 '22 00:09

Mārtiņš Briedis


You can pass the variable to the method by reference by putting an & in front of it when declaring the function. Then you can just check if it set inside the function and not have to worry about suppressing warnings when passing the value that isn't set.

function Check(&$Variable, $DefaultValue) {
    if(isset($Variable) && $Variable != "" && $Variable != NULL) {
        return $Variable;
    }
    else {
        return $DefaultValue;
    }
}

$a = Check($foo, false);
$b = Check($bar, "Hello");
like image 23
365SplendidSuns Avatar answered Sep 21 '22 00:09

365SplendidSuns


  1. null will be passed
  2. There is no undefined behaviour in PHP AFAIK
  3. Usually testing is done with empty($var), e.g.: Check(empty($foo) ? null : $foo), although depending on the circumstances isset may be more appropriate
  4. What isset does is exactly documented -- it tests if there is such a variable in scope and its value is not identical to null
like image 39
Jon Avatar answered Sep 25 '22 00:09

Jon