php default arguments(php 默认参数)
问题描述
在 PHP 中,如果我有这样写的函数
In PHP, if I have a function written like this
function example($argument1, $argument2="", $argument3="")
我可以像这样在其他地方调用这个函数
And I can call this function in other place like this
example($argument1)
但是如果我想给第三个参数一些价值,我应该这样做吗
But if I want to give some value to the thrid argument, Should I do this
example($argument1, "", "test");
或者
example($argument1, NULL, "test");
我认为两者都可以,但是更好的方法是什么?因为将来如果在主函数中更改 $argument2 的值并且如果我有",那么会有什么问题吗?还是 NULL 是最好的选择?
I think both will work but what is the better way? Because in future if the value for the $argument2 is changed in the main function and if I have "", then will there be any problem? Or NULL is the best option?
谢谢
推荐答案
你可以使用:
example($argument1, '', 'test');
或者
example($argument1, NULL, 'test');
您始终可以使用而不是空字符串来检查 NULL:
You can always check for NULL using instead of empty string:
if ($argument === NULL) {
//
}
我认为这完全取决于带有 args 的函数内部发生的情况.
I think it all depends on what happens inside the function with the args.
这篇关于php 默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:php 默认参数
基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
