In this snippet, we will have a look at the process of generating a default function parameter (also known as the optional parameter). Show The syntax of this parameter is as follows:
It is capable of accepting only one parameter (here it is the $name). It holds the value of the parameter. The procedure of generating and using the default function parameter is illustrated in the examples below. Example 1
The output of the first example will look as follows: Welcome to W3d Welcome to W3docs Welcome to Computer Science Portal Example 2
The output will be: Greeting: W3docs Computer Science Portal for Everyone Greeting: night_fury Computer Science Portal for Everyone Greeting: night_fury Contributor The prototype of the concept of the default function parameter is C++ style default argument values. In PHP, you can provide default parameters in a way that once a parameter is not passed toward the function, it is still reachable inside the function with a pre-defined value. Note that default values are also known as optional parameters, as it is not a must to pass them to the function. 6.3.3. DiscussionThe example in the Solution sets the default tag value to b, for bold. For example: $string = 'I am some HTML'; wrap_html_tag($string); returns: <b>I am some HTML</b> This example: wrap_html_tag($string, 'i'); returns: <i>I am some HTML</i> There are two important things to remember when assigning default values. First, all parameters with default values must appear after parameters without defaults. Otherwise, PHP can't tell which parameters are omitted and should take the default value, and which arguments are overriding the default. So, wrap_html_tag( ) can't be defined as: function wrap_html_tag($tag = 'i', $string) If you do this and pass wrap_html_tag( ) only a single argument, PHP assigns the value to $tag and issues a warning complaining of a missing second argument. Second, the assigned value must be a constant — a string or a number. It can't be a variable. Again, using wrap_html_tag( ) as our example, you can't do this: $my_favorite_html_tag = 'i'; function wrap_html_tag($string, $tag = $my_favorite_html_tag) { ... } If you want to assign a default of nothing, one solution is to assign the empty string to your parameter: function wrap_html_tag($string, $tag = '') { if (empty($tag)) return $string; return "<$tag>$string</$tag>"; } This function returns the original string, if no value is passed in for the $tag. Or, if a (nonempty) tag is passed in, it returns the string wrapped inside of tags. Depending on circumstances, another option for the $tag default value is either 0 or NULL. In wrap_html_tag( ), you don't want to allow an empty valued-tag. However, in some cases, the empty string can be an acceptable option. For instance, join( ) is often called on the empty string, after calling file( ), to place a file into a string. Also, as the following code shows, you can use a default message if no argument is provided but an empty message if the empty string is passed: function pc_log_db_error($message = NULL) { if (is_null($message)) { $message = 'Couldn't connect to DB'; } error_log("[DB] [$message]"); } Information may be passed to functions via the argument list, which is a comma-delimited list of expressions. The arguments are evaluated from left to right. PHP supports passing arguments by value (the default), passing by reference, and default argument values. Variable-length argument lists are also supported. Example #1 Passing arrays to functions
Passing arguments by referenceBy default, 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 (&) to the argument name in the function definition: Example #2 Passing function parameters by reference
Default argument valuesA function may define C++-style default values for scalar arguments as follows: Example #3 Use of default parameters in functions
The above example will output: Making a cup of cappuccino. Making a cup of . Making a cup of espresso. PHP also allows the use of
arrays and the special type Example #4 Using non-scalar types as default values
The default value must be a constant expression, not (for example) a variable, a class member or a function call. Note that when using default arguments, any defaults should be on the right side of any non-default arguments; otherwise, things will not work as expected. Consider the following code snippet: Example #5 Incorrect usage of default function arguments
makeyogurt("raspberry"); // won't work as expected The above example will output: Warning: Missing argument 2 in call to makeyogurt() in /usr/local/etc/httpd/htdocs/phptest/functest.html on line 41 Making a bowl of raspberry . Now, compare the above with this: Example #6 Correct usage of default function arguments
makeyogurt("raspberry"); // works as expected The above example will output: Making a bowl of acidophilus raspberry.
Type declarations
Type declarations allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception. To specify a type declaration, the type name should be added before the parameter name. The declaration can be made to accept Valid types
Warning Aliases for the above scalar types are not supported. Instead, they are treated as class or interface names. For example, using
The above example will output: Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of boolean, boolean given, called in - on line 1 and defined in -:1 ExamplesExample #7 Basic class type declaration
f(C $c) { The above example will output: C D Fatal error: Uncaught TypeError: Argument 1 passed to f() must be an instance of C, instance of E given, called in - on line 14 and defined in -:8 Stack trace: #0 -(14): f(Object(E)) #1 {main} thrown in - on line 8 Example #8 Basic interface type declaration
f(I $i) { The above example will output: C Fatal error: Uncaught TypeError: Argument 1 passed to f() must implement interface I, instance of E given, called in - on line 13 and defined in -:8 Stack trace: #0 -(13): f(Object(E)) #1 {main} thrown in - on line 8 Example #9 Typed pass-by-reference Parameters Declared types of reference parameters are checked on function entry, but not when the function returns, so after the function had returned, the argument's type may have changed.
The above example will output something similar to: int(1) Fatal error: Uncaught TypeError: Argument 1 passed to array_baz() must be of the type array, int given, called in %s on line %d Example #10 Nullable type declaration
f(C $c = null) { The above example will output: Strict typingBy default, PHP will coerce values of the wrong type into the expected scalar type if possible. For example, a function that is given an integer for a parameter that expects a string will get a variable of type string. It is possible to enable strict mode on a per-file
basis. In strict mode, only a variable of exact type of the type declaration will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a
float. Function calls from within internal functions will not be affected by the To enable strict mode, the
Example #11 Strict typing
sum(int $a, int $b) { The above example will output: int(3) Fatal error: Uncaught TypeError: Argument 1 passed to sum() must be of the type integer, float given, called in - on line 9 and defined in -:4 Stack trace: #0 -(9): sum(1.5, 2.5) #1 {main} thrown in - on line 4 Example #12 Weak typing
The above example will output: Example #13 Catching TypeError
sum(int $a, int $b) { The above example will output: int(3) Error: Argument 1 passed to sum() must be of the type integer, float given, called in - on line 10 Variable-length argument lists PHP has support for variable-length argument lists in user-defined functions. This is implemented using the
|