PHP’s global variables are different from other programming languages. In most programming languages, global variables automatically take effect in the functions and classes below them, unless they are overridden by local variables, or they are not allowed to be declared with the same name at all. Type of local variables, but global variables in php are not effective by default. Global variables in PHP must be declared global when used in functions. The Global keyword is only useful when defined in functions. In other words, the role 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.
Use an example directly to illustrate this problem, the following code:
<?php $a=5; function test(){ echo $a; } test(); ?>
It’s very simple, declare a global variable $a=5, then declare a function test() to print this global variable, and then call this test(), According to the general programming language thinking, the output here must be 5. However, in actual execution, it has the following effect:
An error is reported directly. It is simply unreasonable to say that a is not defined, but PHP is set up like this, but I need to use $a as a global variable. There may be many functions and classes below that need to use this $a! This is, you need to declare it with the global keyword every time you use the global variable $a.
The following code is a correct demonstration:
<?php $a=5; function test(){ global $a; echo $a; } test(); ?>The running results are as follows, so that a can be used. Every time you use the global variable $a, you must declare it like this. This is a PHP rule.

It is worth noting that the following code is wrong:
<?php global $a; $a=5; function test(){ echo $a; } test(); ?>The same is true for the running result:
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the use of global and the global variables of PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

LeicareleasedtheLeicaLuxcameraappfortheAppleiPhoneafewdaysago.However,theappwasnotdevelopedbyLeica,butbyFjorden.ThecompanyhasbeenknownprimarilyforitscameragripsfortheiPhoneandwasacquiredbyLeicainDecember2023.Fo

SincethedemiseofLogitech'spopularHarmonyremotecontrols,themarketforhigh-qualityuniversalremotecontrolshasbeenfragmentedatbest.UnfoldedCircleaimstoavoidthefateoftheHarmonyUltimatebyeliminatinganyserverobligationsorsubs

Anintriguingthird-partycasefortheGalaxyS24Ultra(curr.$1,099.99onAmazon)hasappearedonmarketplaceslikeAliExpress.Astheimagesthroughoutthisarticleshow,thecasehasasimplesiliconeconstruction.However,italsocontainsanEInkd

WhiletheLightPhone2from2018wasstillequippedwithaneconomicale-inkdisplay,theLightPhone3usesanOLEDdisplaythatcanonlydisplaygrayscale.Thereasonfortheswitchtothe3.92-inchOLEDpanelwithitsresolutionof1,240x1,080isth

In2012,theeminentautomotivejournalistJeremyClarksonstatedthattheLamborghiniAventador,whichhewasreviewingatthetime,wouldbeamongthelastcarstofeatureanaturallyaspiratedV12.Morethanadecadelater,V12-poweredsupercarsareal

TheJabraEliteserieshascometoanend,withGNCEOPeterKarlstromerannouncingthediscontinuationofthewirelessearbudswithinthelineupinapressrelease.Petersaysthatthechangingmarketdynamicsandhowchallengingithasbecometogetas

Audio-TechnicahasunveiledtheATH-S300BTwirelessheadphoneswithhear-throughnoise-cancellation,multipointpairing,and90hoursofbatterylife.Thenoise-cancellingfeaturehasthreemodes:off,on,andhear-through,whereambientsoundscanbehea

Backin2022,whenPCIe7.0wasstartingtotakeshapeasafuturestandard,PCIe5.0wasjusthittingtheservermarketsandtheprospectofseeingPCIe6.0devicesavailableanytimesoon,letalonePCIe7.0ones,wasquitefar-fetched.CurrentlyPCIe5


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
