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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:09:12+00:00 2026-05-14T03:09:12+00:00

Regarding global variable initialization, function hello_testing() { global $conditional_random; if (isset($conditional_random)) { echo foo

  • 0

Regarding global variable initialization,

function hello_testing() {
    global $conditional_random;
    if (isset($conditional_random)) {
        echo "foo is inside";
    }
}

The global variable (conditional_random) may not be initialized before the hello_testing() function is called.

So, what happens to my validation via isset() when $conditional_random is not initialized? Will it fail or it will always be true?

  • 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-14T03:09:12+00:00Added an answer on May 14, 2026 at 3:09 am

    Well, why don’t you just test ? 😉

    Note: It is not as easy as you’d think — read the full answer 😉

    Calling the `hello_testing();` function, without setting the variable:

    hello_testing();
    

    I get no output — which indicates isset returned false.

    Calling the function, after setting the variable:

    $conditional_random = 'blah';
    hello_testing();
    

    I get an output:

    foo is inside
    

    Which indicates global works as expected, when the variable is set — well, one should not have any doubt about that ^^


    But note that isset will return false if a variable is set, and null!

    See the manual page of isset()

    Which means that a better test would be:

    function hello_testing() {
        global $conditional_random;
        var_dump($conditional_random);
    }
    
    hello_testing();
    

    And this displays:

    null
    

    No Notice: the variable exists! Even if null.

    As I didn’t set the variable outside of the function, it shows that global sets the variable — but it doesn’t put a value into it; which means it’s null if not already set outside the function.

    While:

    function hello_testing() {
        //global $conditional_random;
        var_dump($conditional_random);
    }
    
    hello_testing();
    

    Gives:

    Notice: Undefined variable: conditional_random
    

    It proves that notices are enabled 😉

    And, if global didn’t "set" the variable, the previous example would have given the same notice.

    And, finally:

    function hello_testing() {
        global $conditional_random;
        var_dump($conditional_random);
    }
    
    $conditional_random = 'glop';
    hello_testing();
    

    Gives:

    string 'glop' (length=4)
    

    (This is to purely to demonstrate my example is not tricked ^^)

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

Sidebar

Related Questions

Regarding, Local variables ... function foo(){var x = 1; ...} ...and Global variables ,
I was reading some questions regarding the $@ global variable and how it can
Is there any function / global variable in PHP that returns the current state
Regarding TJvRichEdit and Delphi... How? Load/Save rtf (including text and images - not only
I have a javascript file with a global object that must not be renamed
While reading from a site a read that you can not make a global
I need a Global variable/class that stores some basic information about the currently logged
If I define a global variable in a .c file, how can I use
I know there as various posts regarding global variables in SO, but nothing helped.
I have a question regarding gcc. Why I get an error of unused variable

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.