Home > Programmig > Does WordPress Control Global Variables in a Different Way?

Does WordPress Control Global Variables in a Different Way?


WordPress And PHP Global Variables Problem

Wordpress And PHP Global Variables Problem

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

<?php
function add_some_user_info()
{
global $current_user; #Global Variable Generated By WP Core
 $current_user->new_info=”My New Value”; # Remember this line
}
add_some_user_info();
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.

<?php
$test=”<br>First Value<br>”;
function test_function()
{
global $test;
echo $test;
$test=”<br>Second Value Changed by Function<br>”;
}
test_function();
echo “<pre>”;print_r($test);echo “</pre>”;
?>

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

First Value
Second Value Changed by Function
---------------------

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

Output:

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?

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: