newbie - global variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
stma
Forum Newbie
Posts: 5
Joined: Fri Aug 20, 2010 5:27 pm

newbie - global variable

Post by stma »

Hello EveryOne!
could someone please tell what I am doing wrong (if I am doing anything wrong at all)
I am confused with the global variables in PHP.

the following code works just fine
function testGlobal()
{
global $globvar;
$globvar=3;
echo " \$globvar: $globvar";
}
testGlobal();

and prints out the value of globvar on the page of browser.

however, if I assign the value to this variable on the same line I declare it - I am getting blank page in the browser.
function testGlobal()
{
global $globvar=3;
echo " \$globvar: $globvar";
}
testGlobal();

Am I doing something wrong? is it bug in PHP? is something wrong with my settings?
just in case, my settings/versions are:
uname -srvmo
Linux 2.6.18-194.11.1.el5 #1 SMP Tue Aug 10 19:05:06 EDT 2010 x86_64 GNU/Linux
PHP 5.3.3 (cli) (built: Aug 18 2010 12:08:14)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies


thank you in advance!
Gargoyle
Forum Contributor
Posts: 130
Joined: Wed Jul 14, 2010 12:25 am

Re: newbie - global variable

Post by Gargoyle »

Code: Select all

global $globvar;
$globvar = 3;
you should, however, rather use the $GLOBALS array because you can easily lose track of what variables have been defined as global.

Code: Select all

$GLOBALS['globvar'] = 3;
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: newbie - global variable

Post by PHPHorizons »

Hello stma,

The issue here is that unlike JavaScript's var keyword, global does not allow assigning a value to a variable.

Code: Select all

global $foo = 1; // syntax error and hence, a blank white screen

global $foo;
$foo = 1; // This is okay
Cheers
stma
Forum Newbie
Posts: 5
Joined: Fri Aug 20, 2010 5:27 pm

Re: newbie - global variable

Post by stma »

Guys!
thank you very much for all your replies - I understood how it works! :)

cheers.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: newbie - global variable

Post by AbraCadaver »

PHP would tell you this if it could:
[text]Parse error: syntax error, unexpected '=', expecting ',' or ';' in file.php on line X[/text]
So help PHP help you:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
stma
Forum Newbie
Posts: 5
Joined: Fri Aug 20, 2010 5:27 pm

Re: newbie - global variable

Post by stma »

Thank you Guys! - much appreciated!
I have another question, which administrator just moved to here:
viewtopic.php?f=31&t=121139

may you guys, could give me a hand with this one as well?

thank you in advance!
Post Reply