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 8315057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:02:03+00:00 2026-06-08T21:02:03+00:00

The following code produces a warning: <?php $GLOBALS[‘foo’] = Example content<BR><BR>; echo $foo; //

  • 0

The following code produces a warning:

<?php
$GLOBALS['foo'] = "Example content<BR><BR>";
echo $foo; // that works!
Test();

function Test()
{
    echo $foo; // that doesn't work!
}
?>

The warning is:

Notice: Undefined variable: foo

How come ?

  • 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-08T21:02:04+00:00Added an answer on June 8, 2026 at 9:02 pm

    Inside the function, $foo is out of scope unless you call it as $GLOBALS['foo'] or use global $foo. Defining a global with $GLOBALS, although it improves readability, does not automatically reserve the variable name for use in all scopes. You still need to explicitly call the global variable inside lower scopes to make use of it.

    function Test()
    {
        echo $GLOBALS['foo'];
    
        // Or less clear, use the global keyword
        global $foo;
        echo $foo;
    }
    

    It’s even possible to have both a local and a global $foo in the same function (though not at all recommended):

    $GLOBALS['foo'] = "foo! :)";
    function getFoo()
    {
      $foo = "boo :(";
      echo $GLOBALS['foo'] . "\n"; // Global $foo
      echo $foo;            // Local scope $foo since it has no global keyword
    }
    
    getFoo();
    // foo! :) 
    // boo :(
    

    Review the PHP documentation on variable scope and the $GLOBALS documentation for more examples.

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

Sidebar

Related Questions

I have the following example code <?php `echo test > /tmp/test.txt`; $f=fopen(/tmp/test.txt,w+); `rm /tmp/test.txt`;
Can anyone tell me why the following code produces the alert box that I
I have the following code that produces a background image in a table cell.
Given the following code: var a = 'somegarbage=http://somesite.com/foo/bar/&moregarbage'; var result = a.match(/http.+\//g); Produces the
I am getting the following error while running the code, Warning: require_once(product.php) [function.require-once]: failed
When uploading a file from iPhone, my code produces the following error: Warning: move_uploaded_file()
I've got the following code that does not produce an CA1804 warning (declared variable
The following code produces the error: Warning: Dimensions of AlphaData must be 1x1, or
I have the following code $List2 = json_decode(file_get_contents(https://___URL____&format=json),true); the following works perfectly echo '<pre>';print_r($List2);echo
Following code produces a nested array as a result for keys containing three items:

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.