Get the App
SLTechnology News&Howtos  ›  Development  › 

How many access methods are there for variables in php?

Shulou Source: shulou.com Published: 2022-06-01 13:10:57 09月14日 Update

Editor to share with you in php variables are divided into how many access methods, I believe that most people do not understand, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

Description: in php, variables are divided into three access methods (scope): local variables, global variables, static variables.

1. Global variable

Description: variables defined outside the function are global variables. Normally, it can only be used outside the function. The scope is outside the function.

Sample code:

/ / the scope of the global variable $name = 'admin'; / / the global variable cannot be used inside the function because the storage location is different function user () {echo $name;} user (); / / the error cannot be reported using $name 2. Local variable

Description: the variable defined in the function is a local variable. Normally, it can only be used inside the function.

Sample code:

/ / Local variable scope $name = 'admin';echo $name;// adminfunction user2 () {$name2 =' qingyu'; / / local variable echo $name2; / / qingyu} user (); echo $name2; / / local variable cannot be used externally. Static variable

Note: normally variables are destroyed after running the current page (php's own mechanism). If you don't want to destroy, you can use static variables.

Declaration of static variables: static variable name

Sample code:

/ / static variable scope $age = 18 position variables + Echo $age; / / 19function age1 () {$age = 18; echo $age;} age1 (); / / 18function age2 () {$age = 18; echo $age; $age++;} age2 (); age2 () / / 18 18 18 function age3 () {/ / static variable declaration, the current page will not destroy the variable after the static variable declaration, but keep static $age = 18; echo $age; $age++;} age3 (); / / 18age3 (); / / 19age3 (); / / 20age3 (); / / 21age3 (); / 22 4. Global and local conversion

Description: local variables can be declared as global variables within the function, at this time the local can be used outside the function.

Manner of declaration:

$GLOBALS variable name

Global variable name

Globas $sex = 'male'; / / error reporting does not work in this way $sex = 'male'; function user () {globas $name,$sex; / / local to global, global to local $name = 'admin'; echo $sex;} user (); echo $name; is all the content of this article entitled "how many access methods are divided into variables in php". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Variable local global function static mode action code situation example article content page no not much location reason backward most of the time Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux MariaDB Xiaomi Huawei