1

I have the following code:

<?php

function foo($bar) 
{
    global $products; 

    //$products = array();

    $query = 'SELECT p_name FROM 0_products WHERE p_category IN (' . $bar . ')';
    $results = mysql_query($query);

    while($row = mysql_fetch_array($results, MYSQL_ASSOC))
    {
        array_push($products, $row);
        echo 'name pushed, ';
    }
}

require('mysql_ipb_connect.php'); // connect to ipb mysql database

$products = array(); 
foo(5);

?>

When I run it I get the following output:

Warning: array_push() [function.array-push]: First argument should be an array in /home/rgcpanel/public_html/category/category.php on line 14
name pushed,
Warning: array_push() [function.array-push]: First argument should be an array in /home/rgcpanel/public_html/category/category.php on line 14
name pushed,
Warning: array_push() [function.array-push]: First argument should be an array in /home/rgcpanel/public_html/category/category.php on line 14
name pushed, 

If I uncomment "$products = array();" then the output is correct:

name pushed, name pushed, name pushed, 

Why is this happening? I declare the $products array outside of a function (so it's global), and then specify it as being a global inside the function. Something is not right, but I'm not sure what that is?

Thanks for your advice.

5
  • Test that although $products is declared elsewhere, that it is actually an array. var_dump($products), and that you declared it outside the function at global scope, rather than in another function, and finally that you declared it before calling foo(). Commented Mar 22, 2012 at 18:01
  • Michael, @CoryDee -- I didn't think of this until now, but the file this is in is actually being included from inside a function that's a part of the CMS I'm using, so while the $products is outside of a function in this file, it's actually being included into a function. Does that mean it's not considered as a global variable? Commented Mar 22, 2012 at 18:10
  • 1
    If it was defined in an include inside a function, it is not global unless you first declare global $products in the function, before it is initialized by the include. Commented Mar 22, 2012 at 18:13
  • I added that as an answer below... Commented Mar 22, 2012 at 18:15
  • 1
    what the hell is global doing in your code ?! Commented Mar 22, 2012 at 18:16

4 Answers 4

3

Per the comments, $products was initialized by an included file which was included inside a function. That defines its scope to the function, rather than globally. So you'll need to use global $products; before calling the include.

function func_that_defined_products() {
  global $products;
  include('file_that_defines_products.php');
}

// Now when called globally later, it will be at the correct scope.


function foo($bar) 
{
    global $products; 
    $query = 'SELECT p_name FROM 0_products WHERE p_category IN (' . $bar . ')';
    // etc...
}

In any case, I find it a little more readable to use $GLOBALS['products'] instead of the global keyword. And as always, wherever possible it is a preferred practice to pass the variable into a function rather than accessing it globally.

// If you can, do it this way
function foo($bar, $products) {
  // $products was a param, and so global is unnecessary
}

However in your case, if the CMS defines it you may lose the flexibility to do it that way...

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

Comments

0

You haven't initialized the global variable as an array. To PHP, that variable is just null, which is NOT AN ARRAY.

1 Comment

"$products = array();" isn't initializing $products as an array?
0

Make sure you initialize variables before using them. Code like yours will result in side effects (working fine if there is a database result; failing if its empty).

Another point: Why don't you return the result instead of using the global operator? This is really bad style.

Next one: When creating SQL statements: escape your variables!

Comments

0

Uncomment this part

//$products = array();

Initialise the $products as an array

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.