如何在php中创建可选参数? [英] How do you create optional arguments in php?

查看:85
本文介绍了如何在php中创建可选参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP手册中,为了显示具有可选参数的函数的语法,它们在每组相关的可选参数周围使用方括号.例如,对于 date() 函数,该手册为:

In the PHP manual, to show the syntax for functions with optional parameters, they use brackets around each set of dependent optional parameter. For example, for the date() function, the manual reads:

string date ( string $format [, int $timestamp = time() ] )

$timestamp是可选参数,如果留空,则默认为 time() 函数的返回值值.

Where $timestamp is an optional parameter, and when left blank it defaults to the time() function's return value.

在PHP中定义自定义函数时,如何创建像这样的可选参数?

How do you go about creating optional parameters like this when defining a custom function in PHP?

推荐答案

与手册非常相似,在参数定义中使用等号(=):

Much like the manual, use an equals (=) sign in your definition of the parameters:

function dosomething($var1, $var2, $var3 = 'somevalue'){
    // Rest of function here...
}

这篇关于如何在php中创建可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆