homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

PHP global variable
How to define a variable that keep the value alive in other file

 10:02 am on Jul 24, 2008 (gmt 0)

Hi All,

I'm very new in this php world and I'd really appreciate your help.

I have 2 files. a.php and b.php

a.php contains a variable ($x) value that I want to use in b.php

b.php executes a.php: require_once('a.php') but this is not enough to pass the $x value.

How can I set $x global in order to user its value trough the other file?

Sorry about the naive nature of this question.
Thanks in advance.



 2:32 pm on Jul 24, 2008 (gmt 0)

All you have to do is define the variable outside of a function; that makes it global. However, in order to use it INSIDE of a function, you need to declare it.

$g_szMyGlobalString = "Hello, World!"

function SomeFunction()
global $g_szMyGlobalString;

echo $g_szMyGlobalString;


 3:23 pm on Jul 24, 2008 (gmt 0)

if you have $x in "a.php" (but not inside a function), you should have no trouble using it when you "include" it in "b.php". in the case that you do have it inside a function, you must declare it global like in the example above before you can "see" it in "b.php."
if you still have problems, feel free to post "a.php" and "b.php"

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved