Jump to content
Silent-B

How $GLOBALS woks in PHP?

Recommended Posts

I WANT TO MAKE AN ARRAY LIKE $GLOBALS structure:

if you run var_dump($GLOBALS): Capture.PNG

you will see that this is an array and contains another 9 array with key names.

when there is a value(name) in "_POST" you cant use echo $GLOBALS["name"]. but when there is a value("fname") in "GLOBALS" you can use echo $GLOBALS["fname"].

how this array works like that? and how to make an array that behave like that?

Edited by Silent-B

Share this post


Link to post
Share on other sites

Specifying an index without quotes makes php do a tedious evaluation of that unknown value and assuming that it is a constant of some kind.  Definitely not the already-defined var name that you have in mind.

Indices need to be quoted string values or defined php vars (which don't need quotes).

Share this post


Link to post
Share on other sites

Don't use $GLOBALS. Forget it exists. There is never a good reason to use it. Pretend you never saw it.

  • Like 2
  • Great Answer 1

Share this post


Link to post
Share on other sites

I know it sounds like a fun project to emulate something that already exists.  But - just exactly why do you think it necessary?  There is already a $_SESSION array (RTFM) and the ability to make specific local vars global for your entire script (see 'global') .  So what design of yours is going to need your own new version of this?

Besides - good programming practice does not include making everything globally available.

  • Like 1

Share this post


Link to post
Share on other sites
21 hours ago, ginerjm said:

I know it sounds like a fun project to emulate something that already exists.  But - just exactly why do you think it necessary?  There is already a $_SESSION array (RTFM) and the ability to make specific local vars global for your entire script (see 'global') .  So what design of yours is going to need your own new version of this?

Besides - good programming practice does not include making everything globally available.

I`m making php tutorial and  i want to talk about that and say why is this happened.

Share this post


Link to post
Share on other sites

Pretty sure we are not understanding each other.

Exactly what are you trying to do and/or understand?

Share this post


Link to post
Share on other sites
Quote

when there is a value(name) in "_POST" you cant use echo $GLOBALS["name"]. 

 

You are making a tutorial?  Really?  And you don't understand what is wrong with the quote of yours above?

The Globals array contains all defined variables of your current session.  Even your current running process.  So - if you have defined something such as $name that is defined in the Globals array.  Thus if you reference $name it already IS known as $GLOBALS['name'].   

Of course I don't understand your syntax when you mentioned "value(name)" so my assumption of defining a var called $name may be totally wrong.  Perhaps you could clarify what you meant by the use of "value(name)"?

Edited by ginerjm

Share this post


Link to post
Share on other sites
On 1/17/2020 at 4:16 AM, Silent-B said:

when there is a value(name) in "_POST" you cant use echo $GLOBALS["name"]

You are making a tutorial?  Really?  And you don't understand what is wrong with the quote of yours above?

The Globals array contains all defined variables of your current session.  Even your current running process.  So - if you have defined something such as $name that is defined in the Globals array.  Thus if you reference $name it already IS known as $GLOBALS['name'].   

Of course I don't understand your syntax when you mentioned "value(name)" so my assumption of defining a var called $name may be totally wrong.  Perhaps you could clarify what you meant by the use of "value(name)"?

And as others have said.  $GLOBALS is not something that anyone uses.  It's just not.  That and the $_REQUEST array are not preferred even tho they still exist.  If you want to reference something, do it properly, i.e., look for php vars such as $name or $x or $arr, blah,blah,blah or $_POST or $_GET instead of $_REQUEST.  And if you want to make a local script var known inside one of your functions, either pass it in as an argument or declare it as global using the global construct inside each function that you want it known in.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.