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条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决