如果参数(通过引用传递)被传递到PHP函数中,如何检查PHP函数内部?
下面是一个例子:如果参数(通过引用传递)被传递到PHP函数中,如何检查PHP函数内部?,php,function,parameters,Php,Function,Parameters,下面是一个例子: function example(&$outDbgOutput = null) { $bUseDbgOutput = how_to_know_if_this_parameter_was_passed_into_this_function($outDbgOutput); //... } 然后是两种类型的呼叫: example(); 及 另外,我使用的是PHP7.4,因为$outDbgOutput的默认值为空 if($outDbgOu
function example(&$outDbgOutput = null)
{
$bUseDbgOutput =
how_to_know_if_this_parameter_was_passed_into_this_function($outDbgOutput);
//...
}
然后是两种类型的呼叫:
example();
及
另外,我使用的是
PHP7.4
,因为$outDbgOutput
的默认值为空
if($outDbgOutput !== null){
//logic
}
或者,您可以使用isset()
并检查该值是否不等于null,因为isset()
在分配给该值时不返回任何内容
我正在阅读评论部分提到的函数。为了将来我的使用,我也将在这里添加它
if(func_num_args($outDbgOutput) > 0){
//since func_num_args will return the number of parameters that was passed
}
if(isset())
?@Mech-no,这并不总是正确的。如果我这样做,它会工作:$output=“”;范例(产出)代码>但如果我只是这样做,它将不起作用:example($output)代码>如我上面所示。在这种情况下,isset()
将始终返回false
func\u num\u args()
是一个计数器函数,您也可以使用@Mech:在这种情况下,isset($output)&&$output!==“
将解析为false
,而当我在$output=7276363464中将$output
传递到它时,它应该是true
;范例(产出)代码>。您不能将其硬编码为!==“”
。据我所见,func_num_args()
是唯一能产生可靠结果的方法,但它需要我对输入参数的计数进行硬编码,我并不喜欢这种方法。所以我在这里问…@Mech是的,等到有人找到一个重复的调用is_null()
:)$outDbgOutput!==空
请。否则它将传递空字符串、零、false
…这是错误的,或者只是覆盖了某些情况。请阅读我对第一篇文章的评论。。。。您的第二次编辑也未完成。
if(isset($outDbgOutput) && $outDbgOutput !== null){
//logic
}
if(func_num_args($outDbgOutput) > 0){
//since func_num_args will return the number of parameters that was passed
}