Home > Article > Backend Development > Are all parameters passed in php strings?
No, PHP parameters can be strings, numbers, Boolean values, arrays, etc. Starting from PHP version 5.6, it is supported to pass array parameters. The formal parameters of the function can use "..." to indicate that the function can accept a variable number of parameters, and the variable parameters will be passed to the function as an array. The syntax "function" Function name (...$arr){//execution code}".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
PHP customized Function
To declare a custom function in PHP, you can use the following syntax format:
function 函数名 (参数1, 参数2, ..., 参数n){ 函数体; return 返回值; }
When declaring a function, the parentheses "()" after the function name are also It is necessary to include a set of acceptable parameter lists in parentheses. The parameters are the declared variables, and then the variables can be passed to the function when the function is called. The parameter list can be empty or have one or more parameters. Use commas to separate multiple parameters;
In PHP, just use the function name and parameter list to make function calls wherever you need to use the function. :
函数名 (参数1, 参数2, ..., 参数n);
It can be seen that the parameter list of the function is composed of zero or more parameters, separated by commas between each parameter.
According to the location where the parameters are used, parameters are divided into two types: formal parameters and actual parameters.
Formal parameters are the parameter list in parentheses after the function name when defining a function (referred to as "formal parameters"). Just like its name, the formal parameters themselves have no specific values. Because the function body needs to use external parameters, in order for the parameters to be passed in correctly, it needs to be passed through the formal parameters and the data in the function body. The formal parameters are as shown in the figure below.
The actual parameters are the parameters in brackets after the function name when we call the function (referred to as "actual parameters"). The actual parameters and formal parameters need to be in order one by one. Correspondingly, it will replace the corresponding variable value of the formal parameter in the function body. The parameter of the function can be a specific value or a variable. The actual parameters are as shown in the figure below.
php passing parameters
When calling a function, you need to pass parameters to the function, which are passed in The parameters of a function are called actual parameters, while the parameters defined by a function are called formal parameters. There are four ways to pass parameters to a function, namely passing by value, passing by reference, default parameters and variable length parameters.
1. Value passing
Value passing is the default value passing method for functions in PHP, also known as "copy passing by value". As the name suggests, the value passing method will copy the value of the actual parameter and then pass it to the formal parameter of the function, so operating the value of the parameter in the function will not affect the actual parameters outside the function. Therefore, if you do not want the function to modify the value of the actual parameter, you can pass it by value.
【Example】The following defines a simple function. The function has two parameters, and the values of the parameters are exchanged in the function.
<?php function swap($a, $b){ echo '函数内,交换前 $a = '.$a.', $b = '.$b.'<br>'; $temp = $a; $a = $b; $b = $temp; echo '函数内,交换后 $a = '.$a.', $b = '.$b.'<br>'; } $x = 5; $y = 7; echo '函数外,交换前 $x = '.$x.', $y = '.$y.'<br>'; swap($x, $y); echo '函数外,交换后 $x = '.$x.', $y = '.$y; ?>
The running results are as follows:
2. Passing by reference
Passing by reference of parameters is to pass the actual parameters Make a copy of the memory address and then pass it to the formal parameters of the function. Both the actual parameters and the formal parameters point to the same memory address. Therefore, the function's operation on the formal parameters will affect the actual parameters outside the function.
Passing by reference is to pass the memory address of the actual parameter to the formal parameter of the function. Therefore, the actual parameters and formal parameters point to the same memory address. At this time, all operations inside the function will affect the values of the actual parameters outside the function. The method of passing by reference is to add an & symbol on the basis of value passing, as shown below:
function name (&参数1, &参数2, ..., &参数3) { ... }
[Example] Slightly adjust the code of the above example and use the method of passing by reference to pass to the swap function. Parameters, the code is as follows:
<?php function swap(&$a, &$b){ echo '函数内,交换前 $a = '.$a.', $b = '.$b.'<br>'; $temp = $a; $a = $b; $b = $temp; echo '函数内,交换后 $a = '.$a.', $b = '.$b.'<br>'; } $x = 5; $y = 7; echo '函数外,交换前 $x = '.$x.', $y = '.$y.'<br>'; swap($x, $y); echo '函数外,交换后 $x = '.$x.', $y = '.$y; ?>
The running results are as follows:
##3. Default parameters
Default Parameters specify a default value for one or more formal parameters of a function. If the corresponding value is not passed in when calling the function, the function will use this default value. This can avoid errors when calling without parameters, and also Can make some programs appear more reasonable. If the corresponding parameters are passed in, this default value will be replaced. The default parameters of the function are as follows:function name ($str = 'hello', $url) { echo $str; }Among them, the "hello Chinese website" after the formal parameter $str is its default value, and ## needs to be used between the formal parameters and the default values. #=
Connect. [Example] Let’s define a function with default parameters, as shown below:
<?php function add($a, $b=56){ echo $a.' + '.$b.' = '.($a+$b).'<br>'; } add(11); add(37, 29); ?>
默认参数也可以是多个,而且默认参数必须放在非默认参数右边,并且指定默认参数的值必须是一个具体的值,如数字、字符串,而不能是一个变量。
4、可变长度参数--将一个数组传递给函数
在 PHP 5.6 及以后的版本中,函数的形式参数可使用…来表示函数可接受一个可变数量的参数,可变参数将会被当作一个数组传递给函数。示例如下:
<?php function test(...$arr){ var_dump($arr); } test(1, 2, 3, 4); test(5, 6, 7, 8, 9, 10); ?>
推荐学习:《PHP视频教程》
The above is the detailed content of Are all parameters passed in php strings?. For more information, please follow other related articles on the PHP Chinese website!