phppup Posted August 24, 2022 Share Posted August 24, 2022 I want to use elements from an array within a function. I had some success using global $array But then I read that using GLOBAL should be avoided (the explanation of why was not very clear) And that the same result could be achieved by attaching the array to a function (not sure how to do that) I was then thinking of creating an array of global variables. Is this even possible? What is the best way to access an array outside of a function so that its elements can be useful? Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/ Share on other sites More sharing options...
requinix Posted August 24, 2022 Share Posted August 24, 2022 27 minutes ago, phppup said: But then I read that using GLOBAL should be avoided (the explanation of why was not very clear) Using global variables means it can be very difficult to know where they are being set, or modified, or used. If you have a question or problem about one then you could have a hard time finding out what is responsible. 27 minutes ago, phppup said: And that the same result could be achieved by attaching the array to a function (not sure how to do that) "Attaching" is a weird way of saying "pass it as an argument". 27 minutes ago, phppup said: I was then thinking of creating an array of global variables. Is this even possible? I don't know what you mean but I doubt it's a good idea. 27 minutes ago, phppup said: What is the best way to access an array outside of a function so that its elements can be useful? By not trying to access things outside the function at all. Use arguments and return values. Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599758 Share on other sites More sharing options...
ginerjm Posted August 24, 2022 Share Posted August 24, 2022 Where did you read the part about "attaching the array to a function"? Interesting terminology if you ask me. Have you tried to read up on How to Use Functions to see how this is done? Would be a great learning moment for you I'm sure. Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599759 Share on other sites More sharing options...
Barand Posted August 24, 2022 Share Posted August 24, 2022 Just pass the array to the function as one of the arguments $array = [5, 10, 20]; echo array_sum($array); // 35 Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599760 Share on other sites More sharing options...
phppup Posted August 25, 2022 Author Share Posted August 25, 2022 In somewhat of a continuation of the previous issue: function first() { //do stuff and result $A } function two($externalArray) { //do stuff to $A when applicable and result $B } I have an external array that I have been able to access WITHOUT using the GLOBAL feature, but now I am having a problem passing values. Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599831 Share on other sites More sharing options...
mac_gyver Posted August 25, 2022 Share Posted August 25, 2022 do you have an actual example you are trying to make work? well written functions accept all input data as call-time parameters and return the result they produce to the calling code. Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599833 Share on other sites More sharing options...
Solution Barand Posted August 25, 2022 Solution Share Posted August 25, 2022 function first() { $A = [1,2,3,4,5]; return $A; } function second($arr) { return array_sum($arr); } $myarray = first(); echo second( $myarray ); //-> 15 // or echo second( first() ); //-> 15 Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599834 Share on other sites More sharing options...
phppup Posted August 25, 2022 Author Share Posted August 25, 2022 (edited) @Barand Then it appears that I was on the right track (sort of nesting the functions) which then actually makes the "outter" function a parent of the function within it. Is that making sense? Correct? (And eliminating the problem of passing values?) Edited August 25, 2022 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599835 Share on other sites More sharing options...
requinix Posted August 25, 2022 Share Posted August 25, 2022 Named PHP functions do not work like that. There is no such thing as nesting for them. Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599836 Share on other sites More sharing options...
phppup Posted August 25, 2022 Author Share Posted August 25, 2022 (edited) @requinix Am I understanding it "conceptually"? Explanation to develop my understanding or referenced tutorial, please? (I've kind of got the problem solved after removing the FUNCTION() method, but would prefer to achieve my results more systemically. Edited August 25, 2022 by phppup Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599837 Share on other sites More sharing options...
Barand Posted August 25, 2022 Share Posted August 25, 2022 My two example are just two ways of passing the result of first function as an argument to the second function Quote Link to comment https://forums.phpfreaks.com/topic/315230-accessing-global-variables/#findComment-1599839 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.