脚本宝典收集整理的这篇文章主要介绍了php – 如何获取可选参数的默认值,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
<?PHP function funcWIThOptParam($param1,$param2 = 'optional') { PRint_r('param2: ' . $param2); } funcWithOptParam('something'); // this is the behavIoUr I want to reproduce // this will result in 'param2: optional' funcWithOptParam('something',null); // i want $param2 to be 'optional',not null // this will result in 'param2: ' instead of 'param2: optional' ?>
现在,最简单的答案是“不要写null然后” – 但在我的特殊情况下,我得到一个函数调用的参数数组,并且只能这样做:
<?PHP funcWithOptParam($param[0],$param[1]); ?>
因此,即使$param [1]为null,null也将覆盖可选参数的默认值
<?PHP function funcWithOptParam($something,$notOptional) { if($notOptional === null) { $notOptional = 'defaultvalue'; } ... } ?>
funcWithOptParams($params);
并像这样处理它:
function funcWithOptParams($params) { if(!isset($params[1])) { $params[1] = 'defaultvalue'; } ... }
以上是脚本宝典为你收集整理的php – 如何获取可选参数的默认值全部内容,希望文章能够帮你解决php – 如何获取可选参数的默认值所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。