Home > Backend Development > PHP Problem > Can function parameter variables and global variables have the same name?

Can function parameter variables and global variables have the same name?

angryTom
Release: 2023-02-28 07:08:01
Original
5652 people have browsed it

Can function parameter variables and global variables have the same name?

Whether function parameter variables and global variables can have the same name

Can have the same name, if you want to use it inside the function To use global variables, you can declare them using the global keyword. At this time, the address of the variable points to the global variable.

<?php 
$a = 1;
function A($a){
echo $a;
global $a;
echo $a;
}
A(2);
?>
Copy after login

Result:

2 1
Copy after login

Formal parameters:

The "formal parameter" in the custom function is called "form Parameter" is also called a dummy variable because it is not an actual variable. Actual parameters and formal parameters can have the same name.

Global variables:

Global variables can be created by an object function or anywhere in this program. Global variables can be referenced by all objects or functions in this program.

For more PHP related knowledge, please visit PHP Chinese website!

The above is the detailed content of Can function parameter variables and global variables have the same name?. For more information, please follow other related articles on the PHP Chinese website!

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