Home > Backend Development > PHP Tutorial > Variable generation and scope in PHP_PHP tutorial

Variable generation and scope in PHP_PHP tutorial

WBOY
Release: 2016-07-13 17:25:12
Original
1015 people have browsed it

In PHP, you do not need to notify PHP before using a variable. The place where a variable is used for the first time determines the scope of the variable. In some programming languages ​​such as C, global variables are visible inside the function. This is not the case in PHP. When creating a function in PHP, you must explicitly declare the global variables to be used using glob.
For example:

function printcity($cityname)
{
print("Your favorite city is: $cityname");
}
$city ="Beijing";
function citya() //Define a function
{
$city="Guangzhou";
printcity($city);
}
function cityb( )
{
$city="Shenzhen";
printcity($city);
}
function cityc()
{
global $city; // Get a reputation Global variable
printcity($city);
}
citya();//Output Guangzhou
cityb();//Output Shenzhen
cityc();//Output Beijing
?>
Note:
The variables inside the function are only valid when the function is used. Once the function ends, all variables of the function will be cleared.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532083.htmlTechArticleIn PHP, you do not need to notify PHP in advance before using a variable. The place where a variable is used for the first time determines the scope of the variable. In some programming languages ​​such as C, global variables are visible inside the function...
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