Table of contents
1.
Introduction
2.
What are PHP Global Variables?
3.
Syntax
4.
How to Declare a Global Variable in PHP?
4.1.
1. Declare the variable outside any function:
4.2.
2. Use the `global` keyword inside functions
4.3.
3. Access and modify the global variable
5.
Examples
5.1.
Example 1: Accessing a global variable inside a function
5.2.
Example 2: Using multiple global variables
5.3.
Example 3: Modifying global variables inside a function
6.
Frequently Asked Questions
6.1.
Can I access a global variable inside a function without using the global keyword? 
6.2.
What happens if I modify a global variable inside a function? 
6.3.
Is it a good practice to rely heavily on global variables in PHP? 
7.
Conclusion
Last Updated: Oct 12, 2024
Easy

PHP Global Variable

Author Rinki Deka
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Have you ever wondered how to access variables from anywhere within your PHP script? Global variables are the answer! In PHP, global variables provide a convenient way to share data across different functions and scopes. They are a fundamental concept that every PHP developer should understand. Global variables are variables that can be accessed from anywhere within a PHP script, regardless of their scope. It offers various features and tools to create dynamic and interactive websites. 

PHP Global Variable

In this article, we will learn about PHP global variables, learn their syntax, declaration, and usage with the help of examples.

What are PHP Global Variables?

In PHP, variables declared outside any function have a global scope, which means they can be accessed from anywhere within the script. However, when you want to use a global variable inside a function, you need to declare it as a global variable using the "global" keyword. This tells PHP that the variable should be treated as a global variable within the function.

Global variables are useful when you need to share data between different parts of your script or when you want to maintain the value of a variable across multiple function calls. They provide a way to store and access data that is not limited to a specific function's scope.

Note : It's important to note that while global variables can be useful in certain scenarios, they should be used very carefully. Overusing global variables can make your code harder to understand, maintain, and debug. 

Syntax

To declare and use a global variable in PHP, you need to follow a specific syntax. Let’s see how you can declare a global variable:

global $variableName;


To declare multiple global variables at once, you can separate them with commas:

global $variable1, $variable2, $variable3;


Once you have declared a variable as global within a function, you can access and manipulate it just like any other variable.

For example : 

<?php
$globalVar = 10; // Global variable

function myFunction() {
    global $globalVar; // Declaring the variable as global
    echo "The value of globalVar inside the function is: " . $globalVar;
}
myFunction();
echo "The value of globalVar outside the function is: " . $globalVar;
?>
You can also try this code with Online PHP Compiler
Run Code


In this example, we declare a global variable `$globalVar` outside the function and assign it a value of 10. Inside the `myFunction()`, we declare `$globalVar` as a global variable using the `global` keyword. This allows us to access and use the global variable within the function.

The output of this script would be:

The value of globalVar inside the function is: 10
The value of globalVar outside the function is: 10

How to Declare a Global Variable in PHP?

To declare a global variable in PHP, you need to follow a few simple steps. Let’s see how you can declare and use global variables in your PHP script:

1. Declare the variable outside any function:

To make a variable global, you first need to declare it outside any function. This means that the variable should be defined at the top level of your PHP script, not inside any function or block.

   <?php
   $globalVariable = "I am a global variable";
   ?>

2. Use the `global` keyword inside functions

If you want to access or modify the global variable inside a function, you need to declare it as global using the `global` keyword. This tells PHP that you want to use the global variable, not a local variable with the same name.

   <?php
   function myFunction() {
       global $globalVariable;
       echo $globalVariable;
   }
   ?>


In the above example, we use the `global` keyword followed by the variable name `$globalVariable` inside the `myFunction()`. This allows us to access and use the global variable within the function.

3. Access and modify the global variable

Once you have declared the variable as global inside a function, you can access and modify its value just like any other variable.

   <?php
   $globalVariable = "I am a global variable";


   function myFunction() {
       global $globalVariable;
       echo $globalVariable . "\n";
       $globalVariable = "I have been modified inside the function";
   }


   myFunction();
   echo $globalVariable;
   ?>
You can also try this code with Online PHP Compiler
Run Code


In this example, we declare `$globalVariable` outside the function and assign it an initial value. Inside `myFunction()`, we declare `$globalVariable` as global, echo its value, and then modify it. After calling the function, we echo the value of `$globalVariable` again, which reflects the modified value.

Output: 

   I am a global variable
   I have been modified inside the function

   

Examples

Let's discuss a few examples to show the usage of global variables in PHP.

Example 1: Accessing a global variable inside a function

<?php
$count = 0;


function incrementCount() {
    global $count;
    $count++;
    echo "Count inside the function: " . $count . "\n";
}


incrementCount();
incrementCount();
echo "Count outside the function: " . $count;
?>
You can also try this code with Online PHP Compiler
Run Code


In this example, we declare a global variable `$count` and initialize it to 0. We define a function `incrementCount()` that declares `$count` as a global variable using the `global` keyword. Inside the function, we increment the value of `$count` and echo its value.

We call the `incrementCount()` function twice and then echo the value of `$count` outside the function.

Output: 

Count inside the function: 1
Count inside the function: 2
Count outside the function: 2


As you can see, the value of `$count` is incremented each time the `incrementCount()` function is called, and the changes are reflected both inside and outside the function.

Example 2: Using multiple global variables

<?php
$firstName = "Rahul";
$lastName = "Singh";
function fullName() {
    global $firstName, $lastName;
    echo "Full name: " . $firstName . " " . $lastName;
}


fullName();
?>
You can also try this code with Online PHP Compiler
Run Code


In this example, we declare two global variables `$firstName` and `$lastName` and assign them values. We define a function `fullName()` that declares both variables as global using the `global` keyword. Inside the function, we concatenate the values of `$firstName` and `$lastName` to create a full name and echo it.


Output:

Full name: Rahul Singh


This example shows how you can use multiple global variables within a function.

Example 3: Modifying global variables inside a function

<?php
$message = "Hello";
function appendToMessage($text) {
    global $message;
    $message .= " " . $text;
}
appendToMessage("World!");
appendToMessage("How are you?");
echo $message;
?>
You can also try this code with Online PHP Compiler
Run Code


In this example, we declare a global variable `$message` and assign it an initial value of "Hello". We define a function `appendToMessage()` that takes a parameter `$text`. Inside the function, we declare `$message` as a global variable and append the value of `$text` to it.

We call the `appendToMessage()` function twice, passing different texts as arguments. Finally, we echo the value of `$message`.

Output: 

Hello World! How are you?


This example shows how you can modify a global variable inside a function and how the changes persist outside the function.

Frequently Asked Questions

Can I access a global variable inside a function without using the global keyword? 

No, to access a global variable inside a function, you must declare it using the global keyword. This tells PHP that you want to use the global variable, not a local variable with the same name.

What happens if I modify a global variable inside a function? 

When you modify a global variable inside a function, the changes will persist outside the function as well. The global variable will retain its modified value even after the function has finished executing.

Is it a good practice to rely heavily on global variables in PHP? 

No, it is generally not considered a good practice to rely heavily on global variables in PHP. Overusing global variables can make your code harder to understand, maintain, and debug. It's recommended to use function parameters and return values to pass data between functions whenever possible and only use global variables when absolutely necessary.

Conclusion

In this article, we have learned about PHP global variables and their use. We explained what global variables are, their syntax, and how to declare them in PHP. We also looked at several examples that showed how to access and modify global variables inside functions. Global variables provide a way to share data across different parts of a PHP script, but it's important to use them very carefully. Overusing global variables can lead to code that is harder to understand and maintain.

You can also check out our other blogs on Code360.

Live masterclass