Support » Everything else WordPress » Does WordPress Control Global Variables in a Different Way?

  • Today I have found a new unusual problem of PHP global variables in WordPress. ( It may be my problem). I was trying to change the wp global variable $current_user in a user define function by calling global keyword. My code was

    function add_some_user_info()
    global $current_user; #Global Variable Generated By WP Core
     $current_user->new_info="My New Value"; # Remember this line
    echo "<pre>";print_r($current_user);echo "</pre>";

    Output: Showing WP current user object with my additional Value.
    When I use $current_user=”Test Value”; Output: Test Value
    When I use $current_user=false or null or 0; Output should be blank but it is showing WP current user object without no change. That means I can not change $current_user object to delete, null, false or zero but can change with some string. How it works?
    Then I guessed that $current_user variable may be protected by a different way.(What is the technique?)

    I tested the following codes to identify the PHP Global variable rule.

    $test="<br>First Value<br>";
    function test_function()
    global $test;
    echo $test;
    $test="<br>Second Value Changed by Function<br>";
    echo "<pre>";print_r($test);echo "</pre>";

    I wrote the above code in a normal.php file and run it.

    First Value
    Second Value Changed by Function


    Then I wrote the same code beginning of a WordPress theme file header.php and run it.

    First Value

    Why these different output in normal.php file and wordPress header.php file ?

    If anybody confused about my words test my codes in a simple file and a WP theme file.
    Have You Any Correct Explanation About This Problem?
    I Wrote This Problem Here

  • The topic ‘Does WordPress Control Global Variables in a Different Way?’ is closed to new replies.