2

I am having a problem of Undefined variable when I try to use an array inside a function. An example is the code below. How can I be able to access the array $prev inside the function hello()? I've tried searching but I don't really know how to use $GLOBALS[$varname] when the variable is an array. Thanks for any help!

<?php

$prev = [0,1,2];

function hello(){

    echo $prev[1];

}

hello();
hello();
hello();

?>
4
  • 2
    inside the function use global $prev; to make that var available. Commented Feb 22, 2017 at 13:47
  • 1
    but it would be better to pass that var to the function. Avoid using globals anytime possible. Commented Feb 22, 2017 at 13:48
  • 4
    Far better to learn about variable scope and about how to pass arguments to a function Commented Feb 22, 2017 at 13:49
  • 1
    @Jeff what if at anytime I want to change the values inside the array (eg. i want to add 1 to the index 0) how would I do that if I pass the var to the function? Commented Feb 22, 2017 at 13:50

4 Answers 4

8

You can also pass the variable into the function:

$prev = [0,1,2];

function hello(array $array){

    echo $array[1];

}

hello($prev);
hello($prev);
hello($prev);

?>

An other way is to pass the variable by reference.

function hello(&$array){

    $array[1]++;
    echo $array[1];

}
Sign up to request clarification or add additional context in comments.

2 Comments

this is great also however, what if at anytime I want to change the values inside the array (eg. i want to add 1 to the index 0) how would I do that if I pass the var to the function?
@Ralph If you want to change the value of it, just pass the parameter by reference. Added it to my answer.
6

This is the way to use it as global. Btw there are also other ways to use it inside the hello function.

$prev = [0,1,2];

function hello(){
    global $prev;

    echo $prev[1];

}

hello();
hello();
hello();

4 Comments

thank you for the fast reply! sorry I'm new to PHP but thank you so much!
global $prev; this means you declare a new array $prev which can access out side the function
@BunkerBoy no, it means that the outside array is available in the function scope
Thank you for this useful tip. Passing in arrays into functions isn't ideal sometimes. This fixed my issue very quickly.
4

You could do something like:

$GLOBALS["prev"] = [0,1,2];
function hello(){    
    echo $GLOBALS['prev'][1];    
}

hello();

However consider doing something like:

 $prev = [1,2,3];
 function hello($prev) {
      echo $prev[1];
 }
 hello($prev);

As an alternative solution:

class GlobalsContainer {
     public static $prev=[1,2,3];
}

function hello() {
     echo GlobalsContainer::$prev[1];
}
hello();

Comments

0

Declare array with global keyword inside the function. See it -

<?php

    function hello(){

        global $prev = [0,1,2];
        echo $prev[1];

    }


    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.