1

Why am I getting this error message "Warning: array_push() expects parameter 1 to be array, null given ..." When I removed the function and just run the for loop, it works, but not inside function. why?

<?php

$arr = array();
function callme() {
    for ($x = 1; $x <= 10; $x++) {
        array_push($arr, $x);
    }
    return $arr;
}

callme();

print_r($arr);

?>

3 Answers 3

1

Declare array $arr as global

$arr = array();
function callme() {
    global $arr;
    for ($x = 1; $x <= 10; $x++) {
        array_push($arr, $x);
    }
    return $arr;
}

Or pass $arr as a parameter.

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

1 Comment

I also tried global but didn't work. Turns out we need to define an array before the function too. Thanks for your answer.
1

You need to do either of two things:

Add $array as function parameter and return that.

<?php

$arr = array();
function callme($array) {
    for ($x = 1; $x <= 10; $x++) {
        array_push($array, $x);
    }
    return $array;
}

$arr = callme($arr);

print_r($arr);

?>

If you don't like returns you can have the array as reference...

<?php

$arr = array();
function callme(&$array) {
    for ($x = 1; $x <= 10; $x++) {
        array_push($array, $x);
    };
}

callme($arr);

print_r($arr);

?>

or define $arr als global in the function(least desired, you're stuck with only one array)

<?php

$arr = array();
function callme() {
    global $arr;
    for ($x = 1; $x <= 10; $x++) {
        array_push($arr, $x);
    }
    return $arr;
}

callme();

print_r($arr);

?>

6 Comments

don't forget function callme() use(&$arr) {, though in this case that's not much better than global.
well... seeing his level of understanding that wouldn't be wise... I doubted about using references but decided to add that... Sometimes I find it amazing how many ways php has to accomplish the same thing.
You might be right. Though seeing the code, I thought that he might come from a javascript background (closures), and use is probably the closest to this.
Even in javascript it's tricky to use globals... if you ever need a global in javascript one always need to use the window reference to initiate the variable otherwise you'll never reliably know where the variable will end up and you'll encounter a myriad of problems... the use(&$arr) is for most javascript programmers a bridge to far though... references are tough enough to understand for some people, and to add another scoping parameter that looks like a function to a function is difficult to understand for most.. Plus using globals sucks big time, difficult to track and stuff......
In javascript, same as in php using use, it' wouldn't necessarily be a global. Just in this special case it's basicially the same as using global. In other cases, say class methods for example, use has it's use. ;)
|
0

Someone needs to learn some basics about variable scopes.

http://php.net/manual/language.variables.scope.php

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.