Home > Backend Development > PHP Tutorial > 通过调用传递(Passingbyreference)_PHP

通过调用传递(Passingbyreference)_PHP

WBOY
Release: 2016-06-01 12:39:47
Original
909 people have browsed it

默认的,函数参数通过值来传递.如果你希望允许一个函数可以修改它的参数的值,你可以通过调用来传递他们.

如果你希望一个函数参数意志通过引用被传递,你可以预先函数定义中在参数名前加符号(&):

function foo( &$bar ) {

$bar .= ' and something extra.';

}

$str = 'This is a string, ';

foo ($str);

echo $str; // 输出 'This is a string, and something extra.'

如果你希望向一个不是用这种方式定义的函数用调用的方式传递参数,你可以在函数调用中的参数名称前加符号(&).

function foo ($bar) {

$bar .= ' and something extra.';

}

$str = 'This is a string, ';

foo ($str);

echo $str; //输出 'This is a string, '

foo (&$str);

echo $str; //输出 'This is a string, and something extra.'

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