Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7841081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:01:16+00:00 2026-06-02T16:01:16+00:00

As we know, global keyword makes variable( or object, array) visible inside current function

  • 0

As we know, global keyword makes variable( or object, array) visible inside current function we are dealing with

<?php

$some_var = 'some string';

function __test(){
    global $some_var;
    echo $some_var; // some string
}

But some dev’s still use the global keyword outside the functions
at first look it doesn’t make any sense to me.

Well, the question is: Does it make any sense to use ‘global’ keyword outside the function ???

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T16:01:17+00:00Added an answer on June 2, 2026 at 4:01 pm

    From the docs:

    Using global keyword outside a function is not an error. It can be used if the file is included from inside a function.

    Essentially you could have your function guts in a different file than your function declaration. These guts would then be included into the function. This would give the impression, if you view the guts alone, of a user using global outside of a function, however the fact is that when this code is interpreted, it will be interpreted from within a function.

    $name = "Jonathan";
    
    function doSomething () {
      include 'functionGuts.php';
    }
    

    Where the contents of our functionGuts.php file could be:

    global $name;
    echo "Hello, " . $name;
    

    When viewed on its own, functionGuts.php will give the impression that global is being used outside of a function, when in reality it’s being used like this:

    $name = "Jonathan";
    
    function doSomething () {
      global $name;
      echo "Hello, " . $name;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do know that in javascript, when you use this keyword inside a function,
How do I create or use a global variable inside a function? How do
I know how to do a global variable, but whenever I try to define
As I know, I can use global variable transfer the value among the multi
I'm new to object oriented programming and flash. As far as I know, global
I know you can define a global variable in Objective-C by using extern, but
How do i know that this is global or local environment for my php
I know global variables are created when they are declared outside a function (says
I know that, in Delphi, instance variables and global variables are initialized to zero
I know static is an overloaded keyword in C. Here, I'm only interested in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.