Home > Backend Development > PHP Problem > What is the difference between formal parameters and actual parameters in php

What is the difference between formal parameters and actual parameters in php

青灯夜游
Release: 2023-03-14 10:06:02
Original
2640 people have browsed it

Difference: 1. The formal parameters are the parameters in brackets after the function name when defining the function, and the actual parameters are the parameters in brackets after the function name when calling the function; 2. The formal parameters have no actual Meaningful parameters, and actual parameters are parameters with actual data meaning.

What is the difference between formal parameters and actual parameters in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The parameters of the function are divided into two types: formal parameters and actual parameters

  • Formal parameters: Formal parameters, parameters that have no actual meaning, are the parameters used when defining the function declaration

  • actual parameters Parameters: Actual parameters, parameters with actual data meaning, are parameters used when calling functions

1. Formal parameters

Formal parameters It is 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.

What is the difference between formal parameters and actual parameters in php

[Example] The formal parameters of a function are as follows:

<?php
    function hello($str){
        echo &#39;参数 $str 的值为:&#39;.$str.&#39;<br>&#39;;
        echo &#39;php中文网&#39;;
    }
?>
Copy after login

Among them, the variable $str in parentheses after the function name in line 2 of the code is The formal parameters of this function.

2. Actual parameters

The actual parameters are the several parameters in parentheses after the function name when we call the function (referred to as "actual parameters"). The actual parameters and The formal parameters need to correspond one-to-one in order. 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.

What is the difference between formal parameters and actual parameters in php

[Example] The following code demonstrates the actual parameters used when calling the function:

<?php
    function add($a, $b){
        echo $a.&#39; + &#39;.$b.&#39; = &#39;.($a+$b).&#39;<br>&#39;;
    }
    add(11, 32);
?>
Copy after login

Among them, the function name in line 6 of the code is in parentheses. 11 and 32 are the actual parameters.

Note: The actual parameters must be the same as the number of formal parameters, and correspond one to one, otherwise the program will go wrong.

[Example] When calling a function, when the number of actual parameters is different from the number of formal parameters, a fatal error will occur:

<?php
    function add($a, $b){
        echo $a.&#39; + &#39;.$b.&#39; = &#39;.($a+$b).&#39;<br>&#39;;
    }
    add(11);
?>
Copy after login

The running structure is as follows:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function add().
Copy after login

Recommended learning : "PHP Video Tutorial"

The above is the detailed content of What is the difference between formal parameters and actual parameters in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template