Argument function php

obatambeienwasirherbal.com: Function arguments - Manual

Bạn đang xem: Argument function php

obatambeienwasirherbal.com: Function arguments - Manual
obatambeienwasirherbal.com: 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 and parameters Supported Protocols & Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apabịt module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding obatambeienwasirherbal.com Keeping Current Features HTTPhường authentication with obatambeienwasirherbal.comhường Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Commvà line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting obatambeienwasirherbal.com"s Behaviour Audio Formats Manipulation Authentication Services Commvà Line Specific Extensions Compression và Archive Extensions Cryptography Extensions Database Extensions Date và Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing và Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable & Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu thành phầm k Previous thực đơn thành tựu g p Previous man page g n Next man page G Scroll lớn bottom g g Scroll to top g h Goto lớn homepage g s Goto lớn search(current page) / Focus search box
Returning values »
« User-defined functions
obatambeienwasirherbal.comhường 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 các mục of expressions. The arguments are evaluated from left to right.

obatambeienwasirherbal.comhường supports passing arguments by value (the default), passing by reference, và default argument values. Variable-length argument lists and Named Arguments are also supported.


Example #1 Passing arrays to lớn functions


function takes_array($input)

?>

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


Xem thêm: type là gì

Example #2 Function Argument List with trailing Comma


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

?>

As of obatambeienwasirherbal.com 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 khổng lồ this rule are arguments of the size Type $param = null, where the null mặc định makes the type implicitly nullable. This usage remains allowed, though it is recommended to 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 lớn a function always passed by reference, prepend an ampersand (&) to 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, ";
add_some_extra($str);
echo 
$str;    // outputs "This is a string, and something extra."
?>

It is an error lớn pass a value as argument which is supposed 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


function makecoffee($type "cappuccino")

echo 
makecoffee();
echo 
makecoffee(null);
echo 
makecoffee("espresso");
?>