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!
newbie - global variable
Moderator: General Moderators
Re: newbie - global variable
Code: Select all
global $globvar;
$globvar = 3;
Code: Select all
$GLOBALS['globvar'] = 3;
- PHPHorizons
- Forum Contributor
- Posts: 175
- Joined: Mon Sep 14, 2009 11:38 pm
Re: newbie - global variable
Hello stma,
The issue here is that unlike JavaScript's var keyword, global does not allow assigning a value to a variable.
Cheers
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 okayRe: newbie - global variable
Guys!
thank you very much for all your replies - I understood how it works!
cheers.
thank you very much for all your replies - I understood how it works!
cheers.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: newbie - global variable
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:
[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.
Re: newbie - global variable
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!
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!