PHP passes and receives parameters in the function body_PHP tutorial

WBOY
Release: 2016-07-13 10:33:56
Original
892 people have browsed it

In PHP functions, parameter passing can be divided into two types: value passing and reference passing (also called address passing).

By default, PHP passes parameters by value. Pass-by-value parameters When a function is called, the value of a constant or variable (usually called an actual parameter) is passed to the function's parameter (usually called a formal parameter). The characteristic of value transfer is that the implementation parameters are stored in memory separately and are two unrelated independent variables. Therefore, when the value of the formal parameter is changed inside the function, the value of the actual parameter generally does not change.

The characteristic of passing by reference (passing by address) is that the implementation and row parameters share a memory. Therefore, when the value of the formal parameter changes, the value of the actual parameter will also change accordingly. From this perspective, the formal parameters and actual parameters can be considered to be the same variable.

When defining parameters to be passed by reference, you can add the reference symbol & in front of the parameter.

<?php
function printString(&$string){
	echo($string);
	$string="打印完成";
}

$str="测试字符!n";
printString($str);
echo($str);
?>

// 程序输出:测试字符! 打印完成
Copy after login

php also supports variable length parameter lists. When defining a function, no parameters are specified. When calling a function, you can specify the number of parameters as needed, and obtain parameter information through several system functions related to the parameters. The specific instructions are:

<?php
	function mysum()
	{
		$num = func_num_args();
		echo("函数包含:".$num."个参数n");
		$sum = 0;
		
		for($i=0; $i < $num; $i++)
		{
			$sum = $sum + func_get_arg($i);
		}
		echo("参数累加纸盒为:".$sum);
	}
	
	mysum(1,2,3,4);
?>

// 程序输出:函数包含:4个参数 参数累加纸盒为:10
Copy after login

Func_num_args function function: Returns the number of parameters passed to the function. Its syntax is as follows: int func_num_args (void). Description: Returns the number of parameters passed to the currently defined function. func_get_arg() will generate a warning if this function is called from outside the function definition.

func_num_args( ) can be used in conjunction with func_get_arg( ) and func_get_args( ) to allow user-defined functions to accept variable-length argument lists. Among them, func_get_arg() returns items from the parameter list, its syntax: int func_get_arg (int arg_num), returns the arg_numth parameter of the parameter list that defines the function, and its parameters start from 0. And calling this function outside the function definition will generate a warning; and when arg_num is greater than the number of parameters actually passed by the function, a warning will also be generated and FALSE will be returned.

The difference between the func_get_args() function and the func_get_arg() function is that the func_get_args() function returns an array, and each element of the array is equivalent to the number of parameter columns of the current user-defined function.

When we build a PHP class, flexibly using these three functions can achieve very ideal results. For example, when creating a class that links PHP and MYSQL, you can write the following code:

<?php
class mydb {
	private $user;
	private $pass;
	private $host;
	private $db;
	
	public function __construct(){
		$num_args=func_num_args();
		if($num_args>0){
			$args=func_get_args();
			$this->host=$args[0];
			$this->user=$args[1];
			$this->pass=$args[2];
			$this->connect();
		}
	}
}
?>
Copy after login

Give another example program:

<?php
function foo() {
	$numargs = func_num_args();
	echo "Number of arguments: $numargs<br>n";
	if ($numargs >= 2) {
		echo "Second argument is: " . func_get_arg (1) . "<br>n";
	}
	$arg_list = func_get_args();
	for ($i = 0; $i < $numargs; $i++) {
		echo "Argument $i is: " . $arg_list[$i] . "<br>n";
	}
}
foo (1, 2, 3);
?>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752394.htmlTechArticleIn PHP functions, parameter passing can be divided into two types: value passing and reference passing (also called address passing) kind. By default, PHP passes parameters by value. When calling a function by passing parameters by value...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!