Home >Backend Development >PHP Tutorial >Using references and global in php

Using references and global in php

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:16:081117browse

In php, the parameters in the function are passed by value. For example:

<?php
	$a = 12;
	function f($a) {
		$a += 10;
		
	}
	f($a);
	echo $a;
?>

The output result is 12.

If you want to change it to pass by reference, then you only need to change the function body and use Reference symbol &:

function f(<span style="color:#FF0000;">&</span>$a) {
		$a += 10;
		
	}
Another way is to change $a into a global variable in the function body, using the global keyword:

function f($a) {
		global $a;
		$a += 10;
		
	}

Copyright statement: This article is written by the blogger Original articles cannot be reproduced without the permission of the blogger.

The above introduces the use of references and global in PHP, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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
Previous article:PHP basic data typesNext article:PHP basic data types