Silent-B Posted January 17, 2020 Share Posted January 17, 2020 (edited) I WANT TO MAKE AN ARRAY LIKE $GLOBALS structure: if you run var_dump($GLOBALS): 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 January 17, 2020 by Silent-B Quote Link to comment https://forums.phpfreaks.com/topic/309876-how-globals-woks-in-php/ Share on other sites More sharing options...
ginerjm Posted January 17, 2020 Share Posted January 17, 2020 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). Quote Link to comment https://forums.phpfreaks.com/topic/309876-how-globals-woks-in-php/#findComment-1573587 Share on other sites More sharing options...
requinix Posted January 17, 2020 Share Posted January 17, 2020 Don't use $GLOBALS. Forget it exists. There is never a good reason to use it. Pretend you never saw it. 2 1 Quote Link to comment https://forums.phpfreaks.com/topic/309876-how-globals-woks-in-php/#findComment-1573589 Share on other sites More sharing options...
ginerjm Posted January 18, 2020 Share Posted January 18, 2020 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/309876-how-globals-woks-in-php/#findComment-1573614 Share on other sites More sharing options...
Silent-B Posted January 19, 2020 Author Share Posted January 19, 2020 (edited) . Edited January 19, 2020 by Silent-B Quote Link to comment https://forums.phpfreaks.com/topic/309876-how-globals-woks-in-php/#findComment-1573642 Share on other sites More sharing options...
Silent-B Posted January 19, 2020 Author Share Posted January 19, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/309876-how-globals-woks-in-php/#findComment-1573646 Share on other sites More sharing options...
requinix Posted January 19, 2020 Share Posted January 19, 2020 Pretty sure we are not understanding each other. Exactly what are you trying to do and/or understand? Quote Link to comment https://forums.phpfreaks.com/topic/309876-how-globals-woks-in-php/#findComment-1573652 Share on other sites More sharing options...
ginerjm Posted January 19, 2020 Share Posted January 19, 2020 (edited) 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 January 19, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/309876-how-globals-woks-in-php/#findComment-1573653 Share on other sites More sharing options...
ginerjm Posted January 19, 2020 Share Posted January 19, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/309876-how-globals-woks-in-php/#findComment-1573654 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.