将未设置的变量传递给函数

问题描述:

我的代码:

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. 当变量不存在并传递给函数(抑制错误)时,实际传递的是什么?
  2. 此功能可能表现出任何未定义的行为吗?
  3. 是否有更好的方法包装测试变量是否存在并提供函数的默认值?
  4. isset()在测试变量时会在后台检查什么?
  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?


默认值由用户定义.有时是数字,有时是字符串.

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

  1. 传递了NULL,引发通知错误.
  2. 不,函数只能看到它的参数(它不在乎如何调用)
  3. 您可以轻松指定默认值-函数func($ mandatory,$ optional ='default value');
  4. 在函数的参数上进行设置是没有意义的,因为参数已在函数签名中设置.