Home Backend Development PHP Tutorial About the different distinctions between global variables global and $GLOBALS in PHP - WORSHIP Yasa

About the different distinctions between global variables global and $GLOBALS in PHP - WORSHIP Yasa

Oct 03, 2017 am 05:44 AM
global php variable

1.global

The function of Global is to define global variables, but this global variable does not apply to the entire website, but to the current page, including all files in include or require.

However, global variables defined within the function body can be used within the function body, while global variables defined outside the function body cannot be used within the function body. See the example below for details.

(1) Define global variables within the function body, which can be used within the function body.


<?PHP 
$a=123; 
function aa() 
{ 
Global $a; //把$a定义为global变量。 echo $a; //调用函数体外的变量} 
aa(); //输出结果为123,说明函数体内可以使用函数体外的变量;?>
Copy after login

(2) Define global variables outside the function body and cannot use them inside the function body.


<?PHP 
$a=123; 
Global $a; //在函数体外把$a定义为global变量。 function aa() 
{  
  echo $a; 
} 
aa();//会报错,不能输出变量a。?>
Copy after login

2.$GLOBALS

In the $GLOBALS array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the variable's content . $GLOBALS exists in the global scope because $GLOBALS is a superglobal variable. Pay attention to the writing method of $GLOBALS. For example, the variable $a1 is written as $GLOBALS['a1'].

Example: First use global definition


<?PHP 
$a1 = 1; 
$a2 = 2; 
function Sum() 
{ 
  global a1, a2;a2 = a1 + a2; //定义变a1和a2;}
Sum(); 
echo a2; //输出结果为3?>
Copy after login

Use $GLOBALS to define global variables


<?PHP 
$a1 = 1; 
$a2 = 2;function Sum() 
{ 
  $GLOBALS[&#39;a1&#39;] = $GLOBALS[&#39;a1&#39;] + $GLOBALS[&#39;a2&#39;]; //定义变量时每个都要定义}
Sum(); 
echo a2; //输出结果为2?>
Copy after login

eg: global


function test() 
{ 
    global $a;//定义全局变量a 
    unset($a); //删除变量a
    //print $a;//会报错,因为unset已经把$a删除了。 } 
$a = 2; //定义一个变量atest(); //调用test()方法print $a; //输出a,输出的其实是$a = 2,所以结果为2.
Copy after login

eg: $GLOBALS


function test_global() 
{ 
    global $var1, $var2; 
    $var2 =& $var1; 
} 
function test_globals() 
{ 
    $GLOBALS[&#39;var3&#39;] =& $GLOBALS[&#39;var1&#39;]; 
} 
$var1 = 5; 
$var2 = $var3 = 0; 

test_global(); 
print $var2; //输出结果为0test_globals(); 
print $var3; //输结果为5
Copy after login

The above is the detailed content of About the different distinctions between global variables global and $GLOBALS in PHP - WORSHIP Yasa. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

See all articles