Home >Backend Development >PHP Problem >Are all parameters passed in php strings?

Are all parameters passed in php strings?

青灯夜游
青灯夜游Original
2022-12-15 15:07:365078browse

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}".

Are all parameters passed in php strings?

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.

Are all parameters passed in php strings?

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.

Are all parameters passed in php strings?

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 &#39;函数内,交换前 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;;
    $temp = $a;
    $a = $b;
    $b = $temp;
    echo &#39;函数内,交换后 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;;
  }

  $x = 5;
  $y = 7;
  echo &#39;函数外,交换前 $x = &#39;.$x.&#39;, $y = &#39;.$y.&#39;<br>&#39;;
  swap($x, $y);
  echo &#39;函数外,交换后 $x = &#39;.$x.&#39;, $y = &#39;.$y;
?>

The running results are as follows:

Are all parameters passed in php strings?

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 &#39;函数内,交换前 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;;
    $temp = $a;
    $a = $b;
    $b = $temp;
    echo &#39;函数内,交换后 $a = &#39;.$a.&#39;, $b = &#39;.$b.&#39;<br>&#39;;
  }

  $x = 5;
  $y = 7;
  echo &#39;函数外,交换前 $x = &#39;.$x.&#39;, $y = &#39;.$y.&#39;<br>&#39;;
  swap($x, $y);
  echo &#39;函数外,交换后 $x = &#39;.$x.&#39;, $y = &#39;.$y;
?>

The running results are as follows:

Are all parameters passed in php strings?

##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 = &#39;hello&#39;, $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.&#39; + &#39;.$b.&#39; = &#39;.($a+$b).&#39;<br>&#39;;
  }
  add(11);
  add(37, 29);
?>

Are all parameters passed in php strings?

默认参数也可以是多个,而且默认参数必须放在非默认参数右边,并且指定默认参数的值必须是一个具体的值,如数字、字符串,而不能是一个变量。

4、可变长度参数--将一个数组传递给函数

在 PHP 5.6 及以后的版本中,函数的形式参数可使用…来表示函数可接受一个可变数量的参数,可变参数将会被当作一个数组传递给函数。示例如下:

<?php
    function test(...$arr){
        var_dump($arr);
    }
    test(1, 2, 3, 4);
    test(5, 6, 7, 8, 9, 10);
?>

Are all parameters passed in php strings?

推荐学习:《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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn