PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP global and $GLOBALS: Accessing Global Variables Within A Function

In PHP, global and $GLOBALS are used to access variables from the global scope.

1. global

The global keyword is used to access a global variable inside a function. When you declare a variable as global, you're telling PHP to use the variable with the same name from the global scope, not to create a new local variable.

Here's an example:

$x = 5;  // Global scope

function test() {
    global $x;  // Use global scope variable
    echo $x;  // Outputs: 5
}

test();

In this example, we're using the global keyword to tell PHP that we want to use the global variable $x inside the test function.

2. $GLOBALS

$GLOBALS is a PHP superglobal array that contains all global variables. The array keys are the names of the global variables.

$GLOBALS can be used inside a function to access a global variable, even without declaring the variable as global.

Here's an example:

$x = 5;  // Global scope

function test() {
    echo $GLOBALS['x'];  // Outputs: 5
}

test();

In this example, we're using $GLOBALS['x'] to access the global variable $x inside the test function.

Comparison

The main difference between global and $GLOBALS is that global only gives you access to a global variable, while $GLOBALS is an array that also allows you to see all global variables and change their values.

Here's an example of changing a global variable's value with $GLOBALS:

$x = 5;  // Global scope

function test() {
    $GLOBALS['x'] = 10;  // Change the value of $x
}

test();
echo $x;  // Outputs: 10

In this example, the test function changes the value of the global variable $x to 10 by modifying $GLOBALS['x']. After the function is called, the new value of $x is reflected in the global scope.

  1. Using global variables in PHP functions:

    • Global variables can be accessed and modified within functions.
    <?php
    $globalVar = 10;
    
    function incrementGlobalVar() {
        global $globalVar;
        $globalVar++;
    }
    
    incrementGlobalVar();
    echo "Global Variable Value: $globalVar";
    
  2. Accessing global variables with $GLOBALS in PHP:

    • The $GLOBALS array provides access to global variables.
    <?php
    $globalVar = 5;
    
    function accessGlobalWithGlobals() {
        $GLOBALS['globalVar']++;
    }
    
    accessGlobalWithGlobals();
    echo "Global Variable Value: {$GLOBALS['globalVar']}";
    
  3. Scope of variables in PHP and the global keyword:

    • Variables declared outside functions have a global scope.
    <?php
    $globalVar = 15;
    
    function showGlobalVar() {
        echo "Global Variable Value: $globalVar"; // Error, $globalVar is not accessible
    }
    
  4. Modifying global variables inside functions in PHP:

    • Use the global keyword to modify global variables within functions.
    <?php
    $globalVar = 20;
    
    function modifyGlobalVar() {
        global $globalVar;
        $globalVar *= 2;
    }
    
    modifyGlobalVar();
    echo "Modified Global Variable: $globalVar";
    
  5. Advantages of $GLOBALS over the global keyword in PHP:

    • $GLOBALS can be advantageous when dealing with variable variables or dynamic variable names.
    <?php
    $varName = 'dynamicVar';
    $GLOBALS[$varName] = 25;
    
    function modifyDynamicVar() {
        $GLOBALS['dynamicVar'] *= 3;
    }
    
    modifyDynamicVar();
    echo "Modified Dynamic Variable: {$GLOBALS['dynamicVar']}";
    
  6. Creating and managing global constants in PHP:

    • Constants can be defined globally for constant values.
    <?php
    define('PI', 3.14);
    
    function calculateArea($radius) {
        return PI * $radius * $radius;
    }
    
    echo "Area of Circle: " . calculateArea(5);
    
  7. Avoiding variable conflicts with global scope in PHP:

    • Be cautious to avoid conflicts between global and local variables.
    <?php
    $var = 30;
    
    function exampleFunction() {
        $var = 40; // Local variable with the same name
        echo "Local Variable: $var";
    }
    
    exampleFunction();
    echo "Global Variable: $var";
    
  8. Examples of using global and $GLOBALS in PHP functions:

    • Combining both global and $GLOBALS usage.
    <?php
    $globalVar = 50;
    
    function manipulateGlobalVar() {
        global $globalVar;
        $GLOBALS['globalVar'] += 10;
    }
    
    manipulateGlobalVar();
    echo "Updated Global Variable: $globalVar";