


How to define global variables in PHP? How to declare? what is the function? (Summarize)
The previous article introduced you to "The little-known function variable scope (implementation steps)". This article continues to introduce to you how to define global variables? How to declare? What works? Let’s learn together!
Global variables:
Ordinary variables: Ordinary variables refer to variables declared outside the function, and their scope is the entire page part, but cannot be used inside the function;
Global variables: Variables that can be accessed and used both inside and outside the function are global variables;
<?php /****** 普通变量*/ $pome = '你最喜欢的唯美古诗词有哪些?'; function pome(){ echo $pome; } pome(); ?>
Demo results:
Code analysis:
Define a function, pome()
, and then can we use the function to define variables outside? According to the results of running our code, it can be seen that it does not work. The result still shows that it is undefined. That is to say, when we define a variable inside the function and then output (echo), the result will appear that we have not defined this variable, so we know that ordinary variables It refers to the variables declared outside the function. Its scope is part of the entire page and cannot be used inside the function;
The code is demonstrated as follows:
<?php /****** 普通变量*/ $pome = '你最喜欢的唯美古诗词有哪些?'; function pome(){ echo $pome; } pome(); echo $pome; ?>
##Code analysis:
First define a variable outside the function, and then define a function function demo() inside the function. If we want to make a global variable, we need to declare it inside the function. A variable with the same name as the outside. After declaring the variable, we need to use a keyword called (global) to declare a variable with the same name as the outside. At this time, it is a global variable, and finally output (echo) this content. The demonstration code is as follows:<?php /****** 全局变量*/ $str = '树叶落到木地板上,说了一句,我喜欢你'; function demo(){ global $str; echo $str; } demo(); ?>
<?php /****** 全局变量*/ $str = '树叶落到木地板上,说了一句,我喜欢你'; function demo(){ global $str; echo $str; $str = '我也喜欢你'; } echo '这是第一个在函数外部输出:'. $str; echo '<hr/>'; echo '这是在函数内部输出'.demo(); echo '<hr/>'; echo '这是第二个在函数外部输出:'. $str; ?>
In summary:
Method 1 to implement global variables:
- Have a variable inside the function
- Also have a variable with the same name outside the function
- Use the global keyword to modify the variable with the same name inside the function to achieve global variables
Note: global declares the global Assignment of variables is prohibited.
If a variable has been assigned a value inside the function, and then uses global to make a global declaration, the result is that the value of the variable becomes the value of the variable outside the functionAchieving globalization Variable method 2:
$GLOBALS-Within the function, you can use $GL0BALS[variable name] to directly call the variable value outside the function. At this time, it can be used inside and outside the function. , another way of global variables is implemented. Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to define global variables in PHP? How to declare? what is the function? (Summarize). For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

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



The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

The go language does not have static global variables. It uses a more flexible way to handle the need for global variables. Global variables are usually declared at the package level, that is, variables declared outside the function. These global variables are throughout the package. are visible and can be used in any function in the package.

As JavaScript becomes more popular, more and more websites and applications rely on JavaScript. However, the use of global variables in JavaScript can have security issues. In this article, I will introduce how to implement global variable safety in JavaScript. The best way to avoid using global variables is to avoid using global variables. In JavaScript, all variables are global by default unless they are declared within a function. Therefore, local variables should be used whenever possible

Golang is a strongly typed programming language with features such as efficiency, simplicity, and concurrency, so it is gradually favored by more and more developers. In the development of Golang, the global variables and local variables of functions often involve data competition issues. This article will analyze the data competition problem of global variables and local variables in Golang functions from the perspective of actual coding. 1. Data competition for global variables Golang global variables can be accessed in all functions, so if rigorous design and coding are not carried out

We will see how C and C++ behave differently when redeclaring a global variable without initialization, redeclaring a global variable with initialization, and redeclaring a global variable and initializing it twice. Additionally, we will repeat the above combination using local variables. 1.A) C program: Re-declare global variables without initialization #include<stdio.h>intvar;intvar;intmain(){ printf("Var=%d",var); return0;} output Var=0B) C++ program:

Yes, Go functions in Goroutine have direct access to global variables by default. Reason: Goroutine inherits the memory space of the Goroutine that created it, including access to global variables.

PHP global variables include: 1. $_SERVER, the super global variable of the server and execution environment information on which the current script is running; 2. $_GET, an associative array of variables passed to the current script through the GET method; 3. $_POST, through the POST method An associative array of variables passed to the current script; 4. $_SESSION, which stores user-related information in the current session; 5. $_COOKIE, an associative array of variables passed to the current script through HTTP Cookies; 6. $_FILES, etc.
