I need to turn a variable to a super global

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
Mosab
Forum Newbie
Posts: 1
Joined: Tue Sep 27, 2005 5:06 am
Location: Egypt

I need to turn a variable to a super global

Post by Mosab »

Hi everybody,
I need to convert a variable into a super global one , without using cookies and sessions , is that possible?!
And if so , How ?!!

Thanks in advance ..
User avatar
dbevfat
Forum Contributor
Posts: 126
Joined: Tue Jun 28, 2005 2:47 pm
Location: Ljubljana, Slovenia

Post by dbevfat »

1) You can't.

2) It's a bad idea anyway, you should consider a different design.

3) If you really REALLY want to, you could do something like:

Code: Select all

$myvar =1234;
$GLOBALS['myvar'] = $myvar;

// in a different scope:
echo $GLOBALS['myvar'];
But, did you mean a global or a persistent variable?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: I need to turn a variable to a super global

Post by Roja »

Mosab wrote:Hi everybody,
I need to convert a variable into a super global one , without using cookies and sessions , is that possible?!
And if so , How ?!!

Thanks in advance ..
Sure its possible.

Code: Select all

$whatever = 'important variable to put in the super global area';
$_REQUEST['whatever'] = $whatever;
Done. Now its in the superglobal scope.

The bigger question is why you want that. More likely, you want global, not super-global. In that case, you'd do:

Code: Select all

global $whatever;
$whatever = 'important variable to put in the super global area';
And then code below it (except functions, which would need a similar global call) can 'see' that variable.

But again, its important to ask why you need it global. Your design probably needs more thought if you need more than a few globals.
Post Reply