php variable scope
The scope of a variable is the context in which it is defined (that is, its effective scope). Most PHP variables have only a single scope. This single scope span also includes files introduced by include and require . For example:
1 2 3 4 |
|
This variable $a will take effect in the include file b.inc. However, in user-defined functions, a local function scope will be introduced. Any variables used inside a function will be restricted to the local function scope by default. For example:
1 2 3 4 5 6 7 8 |
|
This script will not produce any output because the echo statement refers to a local version of the variable $a, and it is not assigned a value in this scope. You may notice that PHP's global variables are a little different from C language. In C language, global variables automatically take effect in functions unless overridden by local variables. This may cause some problems, someone may accidentally change a global variable. Global variables in PHP must be declared global when used in functions.
global keyword
First, an example of using global:
Example #1 Using global
1 2 3 4 5 6 7 8 9 10 11 |
|
The output of the above script will be "3". After global variables $a and $b are declared in a function, all references to either variable point to its global version. PHP has no limit on the maximum number of global variables that a function can declare.
The second way to access variables in the global scope is to use special PHP custom $GLOBALS array. The previous example can be written as:
Example #2 Use $GLOBALS instead of global
1 2 3 4 5 6 7 8 9 10 |
|
$GLOBALS is an associative array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the content of the variable. $GLOBALS exists in the global scope because $GLOBALS is a superglobal variable. The following example shows the usefulness of superglobal variables:
Example #3 Example demonstrating superglobal variables and scope
1 2 3 4 5 6 7 8 9 10 |
|
Using static variables
Another important feature of variable scope is static variables. Static variables only exist in the local function scope, but their values are not lost when program execution leaves this scope. Take a look at the following example:
Example #4 Demonstrates an example that requires a static variable
1 2 3 4 5 6 7 8 |
|
This function is not very useful because it will set the value of $a to 0 and output 0 every time it is called. $a++, which increments the variable by one, has no effect because the variable $a no longer exists once the function exits. To write a counting function that will not lose the current count value, define the variable $a as static:
Example #5 Example of using static variables
1 2 3 4 5 6 7 8 |
|
Now, the variable $a is only used when test( is called for the first time ) function is initialized, and every time the test() function is called thereafter, the value of $a will be output and incremented by one.
Static variables also provide a way to deal with recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, as they may recurse indefinitely. You must ensure that there are adequate means to terminate recursion. The following simple function recursively counts to 10, using the static variable $count to determine when to stop:
Example #6 Static variables and recursive functions
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Static variables can be declared as in the example above. Assigning it with the result of an expression in a declaration will result in a parsing error.
Example #7 Declare static variables
1 2 3 4 5 6 7 8 9 |
|
Static declarations are resolved at compile time.
There is nothing wrong with using the global keyword outside of a function. Can be used when including files within a function.
References to global and static variables
In Zend engine 1 generation, which drives PHP4, static and global definitions of variables are implemented in the form of references. For example, a true global variable imported with the global statement inside a function scope actually establishes a reference to the global variable. This may lead to unexpected behavior, as demonstrated in the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
The above routine will output:
NULL
object(stdClass)(0) {}
Similar behavior also applies to static statements . References are not stored statically:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
The above routine will output:
1 2 3 4 5 6 |
|
The above example demonstrates that when a reference is assigned to a static variable, its value is not remembered the second time the &get_instance_ref() function is called. live.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.
