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

  • Home
  • SEARCH
  • 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 6564763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:00:30+00:00 2026-05-25T14:00:30+00:00

Please look at the following two examples: Example 1: $variable = ‘some value’; class

  • 0

Please look at the following two examples:

Example 1:

$variable = 'some value';

class Foo {
   public function bar() {
      global $variable;
      print $variable;       
   }
}

Example 2:

$variable = 'some value';

class Foo {
   public function bar() {
      print $GLOBALS['variable'];  
   }    
}

Examples can be used like this:

$foo = new Foo();
$foo->bar();

Both seem to do the same thing? What is the difference? Is one way better than the other? Why are there two different ways of doing this?

The first example seems strange to me because it looks like a declaration followed by using the variable without assigning it.. looks weird to me.

Thank you.

  • 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-05-25T14:00:31+00:00Added an answer on May 25, 2026 at 2:00 pm

    The global keyword ‘imports’ the variable into the local scope, while accessing a global through $GLOBALS does not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please look at the two examples below: irb(#<ActionView::Base:0x2863d58>):030:0> paintings_path => /some-nice-alias-path irb(#<ActionView::Base:0x2863d58>):029:0> self.controller_name.to_s +
Please, take a look at this example: import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Test
please take a look at the two following c statements printf(a very long string);
Please look at the following code mydiv1.innerHTML=mydiv1.innerHTML + '<label>*Functional Area:<span class=small>Enter Company Name</span></label><input type=text
Please look at the following test page : sample page showing two images .
Please take a look at the following: jQuery.fn.jqPos = function(target, settings) { settings =
am new here. i have a slight problem; PLease look at the following code
Getting the above error in following code. How to rectify it. Thanks. Please look
Please take a look at the following .htaccess ErrorDocument 404 /404/ RewriteEngine On RewriteRule
In my current project, Workflows have comments. Please have a look at the following

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.