dosrmo0442 2014-06-16 00:23
浏览 15
已采纳

这样做可以在调用函数时不必定义所有变量

I am using 000webhost and I made a custom PHP function that looks like this:

function example($test1, $test2, $test3) {
   echo $test1 . $test2 . $test3;
}

Then I do example('hello'); and it says:

PHP Error Message

Warning: Missing argument 2 for example(), called in /home/a8525001/public_html/test.php on line 5 and defined in /home/a8525001/public_html/test.php on line 2

Free Web Hosting

PHP Error Message

Warning: Missing argument 3 for example(), called in /home/a8525001/public_html/test.php on line 5 and defined in /home/a8525001/public_html/test.php on line 2

Free Web Hosting
1 

Is there any way I can stop these warnings without access to the server's php.ini? The same code worked fine on my xampp server...

Thanks in advance,

Ben

  • 写回答

2条回答 默认 最新

  • duanbo7517 2014-06-16 01:25
    关注

    You have a few options, and here are a 2 of them:

    You could use the following when set to null (having them defined).

    function example($test = NULL, $test2 = NULL, test3 = NULL) {
        // use variables here but do something like this to check it isn't empty
        if($test !== NULL) {
            echo $test;
        }
        /// etc...and use the rest in whatever you need
    }
    

    or you could use func_get_args(), which allows you to go something like this:

    function example() {
        $args = func_get_args();
        foreach($args as $i => $arg) {
            echo "Argument {$i} is: {$arg} <br />";
        }
    }
    

    Allowing you do something like:

    example('derp', 'derp1', 'derp2');
    

    And the above function would return:

    Argument 0 is: derp
    Argument 1 is: derp1
    Argument 2 is: derp2
    

    Optionally: you could use func_num_args() to ensure that there were parameters set in the function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能