dongyi1524 2009-12-14 00:05
浏览 44

PHP中的可选函数输入

I notice that in some PHP built in functions such as str_replace there are optional input variables. Can I have have optional input variables in my own functions? If so, how?

Thanks, Brian

  • 写回答

3条回答 默认 最新

  • dtg25862 2009-12-14 00:06
    关注

    The first way is to use default values for some arguments:

    function doStuff($required, $optional = '', $optional2 = '') {
      ...
    }
    

    Now you just include a default value and then someone can do:

    doStuff('foo');
    doStuff('foo', 1);
    doStuff('foo', 2, 3);
    

    You need to choose an appropriate default value if the field isn't specified or to indicate that no value was set. Typical examples are '', 0, false, null or array().

    Or you don't need to specify them at all with func_get_args():

    function doStuff() {
      print_r(func_get_args());
    }
    

    func_get_args() can be used with explicit arguments (the first example) as well.

    评论

报告相同问题?

悬赏问题

  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程