将未设置的变量传递给函数 [英] Passing unset variables to functions

查看:60
本文介绍了将未设置的变量传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

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. 在函数的参数上进行设置是没有意义的,因为参数已在函数签名中设置.

这篇关于将未设置的变量传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆