Jump to content

accessing global variables


Go to solution Solved by Barand,

Recommended Posts


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?

Link to comment
https://forums.phpfreaks.com/topic/315230-accessing-global-variables/
Share on other sites

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.

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.

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.

  • Solution
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

 

@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 by phppup

@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 by phppup
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.