更新时间:2022-03-01 20:51:55
您需要通过引用传递变量:
You need to pass the variable by reference:
function AmISet(&$fieldName) {
if (isset($fieldName)) {
echo "I am set\n";
} else {
echo "I am not set\n";
}
}
测试用例:
$fieldName = 'foo';
AmISet($fieldName); // I am set
unset($fieldName);
AmISet($fieldName); // I am not set
但是,此函数实际上没有用,因为它只会输出一个字符串.您可以创建一个接受变量的函数,如果变量存在则返回(来自
However, this function is not useful as it is, because it will only output a string. You can create a function that accepts a variable and return if it exists (from this post):
function issetor(&$var, $default = false) {
return isset($var) ? $var : $default;
}
现在可以像这样使用了:
Now it can be used like so:
echo issetor($fieldName); // If $fieldName exists, it will be printed