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

Using references and global in php

WBOY
Release: 2016-07-29 09:16:08
Original
1073 people have browsed it

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;
?>
Copy after login

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;
		
	}
Copy after login
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;
		
	}
Copy after login

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.

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