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

发布于 2024-10-30 14:23:57 字数 642 浏览 2 评论 0原文

我的代码:

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() 在底层检查什么?

编辑:

默认值由用户定义。有时它是一个数字,有时是一个字符串。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤城病女 2024-11-06 14:23:57
  1. 传递 NULL 时,会引发通知错误。
  2. 不,函数只看到它的参数(它不关心它是如何被调用的)
  3. 您可以轻松指定默认值 - function func($mandatory, $Optional = 'default value');
  4. 在函数的参数上使用 Isset 是没有意义的,因为参数已经在函数签名中设置了。
  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.
纵情客 2024-11-06 14:23:57

您可以通过放置 & 将变量通过引用传递给方法。声明函数时放在它前面。然后,您只需检查它是否在函数内部设置,而不必担心在传递未设置的值时抑制警告。

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

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

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");
自找没趣 2024-11-06 14:23:57
  1. null 将被传递
  2. PHP AFAIK 中没有未定义的行为,
  3. 通常测试是使用 empty($var) 完成的,例如: Check(empty($foo) ? null : $foo),尽管根据具体情况 isset 可能更合适
  4. isset 所做的正是 记录 -- 它测试范围内是否存在这样的变量并且其值与 null 不同
  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
葬花如无物 2024-11-06 14:23:57

当变量不存在但存在时
传递给函数(抑制
错误)实际传递了什么?

在这种情况下,您尝试读取不存在的变量

因此,您得到null——它被传递给函数。

是否有任何未定义的行为
这个功能可以发挥吗?

我不这么认为——除了使用 @ 运算符并不是一个很好的做法。

When a variable doesn't exist and is
passed to the function (suppressing
the error) what is actually passed?

In this case, you try to read from a non-existent variable

So, you get null -- which is passed to the function.

Is there any undefined behaviour that
this function could exhibit?

Not that I see -- except using the @ operator is not quite a good practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文