Home > Backend Development > PHP Tutorial > Explanation of the formal and actual parameters of functions in PHP_PHP Tutorial

Explanation of the formal and actual parameters of functions in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:34:23
Original
788 people have browsed it

PHP will issue a warning when the number of actual parameters the number of formal parameters, PHP will not report an error. It will only take the first few parameters, and the excess will be discarded.

When writing functions in PHP, generally when calling a function, the changed values ​​are the formal parameters rather than the actual parameters. However, if an address character is added to the formal parameters, the value of the actual parameters will be changed. Why?

Please see the example below:

Copy the code The code is as follows:

//Write a function swap() and test that the actual parameter values ​​of the function have not changed
function swap($a,$b) {
echo "

Before entering the swqp() function< ;br>n";
echo "Before exchange: formal parameter a=$a, formal parameter b=$b
n";
$c=$b;
$a=$b ;
$b=$c;
echo "After exchange: formal parameter a=$a, formal parameter b=$b
n";
echo "Exit the swap() functionn";
}
$variablea=5;
$variableb=10;
echo "Before calling the swap() function: ";
echo "actual parameters a=$variablea,actual parameter b=$variableb
n";
swap($variablea,$variableb);
echo "After calling the swap() function: ";
echo "actual parameter a=$variablea,actual parameter b=$variableb
n";
?>


Copy code Code As follows:

//Test the value change of the swap() function parameter
function swap1(&$a,&$b) {
echo "

Enter swap1() function
n";
echo "Before exchange: formal parameter a=$a, formal parameter b=$b
n";
$c=$ b;
$a=$b;
$b=$c;
echo "After exchange: formal parameter a=$a, formal parameter b=$b
n";
echo "Exit the swap() function

n";
}

$variablea=5;
$variableb=10;
echo "Call swap1( ) before function: ";
echo "actual parameter a=$variablea,actual parameter b=$variableb
n";
swap1($variablea,$variableb);
echo "call swap1( ) function: ";
echo "actual parameter a=$variablea, actual parameter b=$variableb
n";
?>

//The above two An example is just to illustrate, please give me some advice~~~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322440.htmlTechArticlePHP will issue a warning when there are actual parameters and formal parameters, because PHP’s interpretation mechanism will think that there are Parameters are defined but not used, which may affect the functionality of the function. So an alert will be issued...
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