Param in function php Function arguments - Manual

Bạn đang xem: Param in function php Function arguments - Manual Function arguments - Manual
Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Errors Exceptions Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces và Classes Context options & parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding Keeping Current Features HTTP authentication with Cookies Sessions Dealing with XForms Handling tệp tin uploads Using remote files Connection handling Persistent Database Connections Commvà line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting"s Behaviour Audio Formats Manipulation Authentication Services Commvà Line Specific Extensions Compression và Archive sầu Extensions Cryptography Extensions Database Extensions Date và Time Related Extensions File System Related Extensions Human Language và Character Encoding Support Image Processing & Generation Mail Related Extensions Mathematical Extensions Non-Text MIME đầu ra Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next thực đơn cửa nhà k Previous thực đơn tác phẩm g p Previous man page g n Next man page G Scroll lớn bottom g g Scroll to top g h Golớn homepage g s Golớn search(current page) / Focus search box
Returning values »
« User-defined functions Manual Language Reference Functions
Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Romanian Russian Spanish Turkish Other
Submit a Pull Request Report a Bug

Function arguments

Information may be passed to functions via the argument danh mục, which is a comma-delimited danh sách of expressions. The arguments are evaluated from left lớn right. supports passing arguments by value (the default), passing by reference, & mặc định argument values. Variable-length argument lists and Named Arguments are also supported.

Example #1 Passing arrays to functions

function takes_array($input)


As of 8.0.0, the list of function arguments may include a trailing comma, which will be ignored. That is particularly useful in cases where the danh sách of arguments is long or contains long variable names, making it convenient to các mục arguments vertically.

Xem thêm: Ym Là Gì ? “Chat Chit” Trên Ym Thú Vị Hơn Với Yahoo! Friend

Example #2 Function Argument List with trailing Comma

function takes_many_args(
$arg_with_default 5,
$again "a default string"// This trailing comma was not permitted before 8.0.0.


As of obatambeienwasirherbal.comhường. 8.0.0, passing mandatory arguments after optional arguments is deprecated. This can generally be resolved by dropping the mặc định value. One exception lớn this rule are arguments of the khung Type $param = null, where the null mặc định makes the type implicitly nullable. This usage remains allowed, though it is recommended to lớn use an explicit nullable type instead.

Example #3 Passing optional arguments after mandatory arguments

function foo($a = <>, $b)       // After

function bar(A $a null$b)        // Recommended

Passing arguments by reference

By mặc định, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference.

To have an argument to a function always passed by reference, prepend an ampersand (&) lớn the argument name in the function definition:

Example #4 Passing function parameters by reference

function add_some_extra(&$string)

$str "This is a string, ";
$str;    // outputs "This is a string, and something extra."

It is an error lớn pass a value as argument which is supposed to lớn be passed by reference.

Default argument values

A function may define C++-style default values for scalar arguments as follows:

Example #5 Use of default parameters in functions

Xem thêm: Mang Thai Mấy Tháng Biết Trai Hay Gái Chưa? MấY TuầN BiếT Trai Hay Gái

function makecoffee($type "cappuccino")


Chuyên mục: