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.
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 callingfoo().global $productsin the function, before it is initialized by the include.globaldoing in your code ?!