Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP $GLOBALS
Next article icon

PHP $GLOBALS

Last Updated : 31 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

$GLOBALS is a built-in PHP superglobal associative array that stores all global variables in a PHP script. It provides a way to access global variables from any scope, including inside functions, classes, or methods, without needing to pass them as arguments or use the global keyword.

To access a global variable within a function, you must either declare it as global using the global keyword or reference it through the $GLOBALS array.

How Does $GLOBALS Work?

Here is a simple example to illustrate why $GLOBALS is needed:

PHP
<?php $message = "Hello, World!";  function printMessage() {     echo $message; // Undefined variable error }  printMessage(); ?> 
Notice: Undefined variable: message

Inside printMessage(), $message is undefined because the function has its own local scope.

Now, using $GLOBALS:

PHP
<?php $message = "Hello, World!";  function printMessage() {     echo $GLOBALS['message']; // Access global variable directly }  printMessage(); ?> 

Output
Hello, World!

The function accesses the global variable $message through $GLOBALS['message'].

Accessing and Modifying Global Variables Using $GLOBALS

You can not only read but also change global variables inside functions via $GLOBALS.

PHP
<?php $count = 5;  function incrementCount() {     $GLOBALS['count']++; }  incrementCount(); incrementCount();  echo $count;  // Output: 7 ?> 

Output
7

Here, the function increments the global $count by modifying it inside $GLOBALS.

When to Use $GLOBALS

  • When you need to access or modify global variables inside functions without declaring each one as global.
  • When dealing with many global variables and you want a consistent, readable approach.
  • Useful in procedural code or legacy scripts where passing parameters is impractical.

Best Practices for Using $GLOBALS

  • Limit usage to cases where other alternatives (function parameters, class properties) are impractical.
  • Avoid global state whenever possible; encapsulate variables within functions or classes.
  • Use $GLOBALS primarily for small scripts or debugging.
  • Document variables accessed via $GLOBALS are clearly for maintainability.

Next Article
PHP $GLOBALS

A

anjalisa6ys
Improve
Article Tags :
  • Web Technologies
  • PHP

Similar Reads

    PHP Constants
    A constant is a name or identifier used to store a fixed value that does not change during the execution of a PHP script. Unlike variables, constants do not start with a $ symbol and stay the same once they are defined.Constants are immutable (cannot be changed after definition).They are global by d
    3 min read
    PHP | Superglobals
    PHP superglobals are predefined variables that are globally available in all scopes. They are used to handle different types of data, such as input data, server data, session data, and more. These superglobal arrays allow developers to easily work with these global data structures without the need t
    6 min read
    What is the use of $GLOBALS in PHP ?
    In this article, we will discuss $GLOBALS in PHP. $GLOBALS is a superglobal variable used to access global variables from anywhere in the PHP program. PHP stores all global variables in an array called $GLOBALS[index]. Syntax: $GLOBALS['index']=value;value is the input value.The index is the unique
    1 min read
    PHP Namespace
    A namespace in PHP is a container for logically grouping classes, interfaces, functions, and constants. They help avoid name collisions by allowing the same name to be used in different namespaces without conflict. Using namespaces, you can make your code more organized, maintainable, and scalable.A
    4 min read
    PHP File Handling
    In PHP, File handling is the process of interacting with files on the server, such as reading files, writing to a file, creating new files, or deleting existing ones. File handling is essential for applications that require the storage and retrieval of data, such as logging systems, user-generated c
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences