Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7672797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:22:06+00:00 2026-05-31T16:22:06+00:00

I have the following code: <?php function foo($bar) { global $products; //$products = array();

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:22:07+00:00Added an answer on May 31, 2026 at 4:22 pm

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
I have the following code: <?php function search_reset() { $query = $_GET['q']; if($query) echo
I have the following code: $(document).ready(function() { $.getJSON('../includes/_quiz.php?class_id=163&course_id=183',function(data){ $.each(data,function(k,v){ questions[k] = v.question; answers[k*4] =
I have the following PHP code which works out the possible combinations from a
I have the following PHP code doing a very simple select into a table.
I have the following PHP code to remove special characters from a variable; <?php
I have the following php code from php.net <?php // The i after the
I have the following PHP code; <?php component_customer_init(); component_customer_go(); function component_customer_init() { $customer =
For instance, say I have the following code. $foo = new bar(); And an
I have the following php code which I found here : function download_xml() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.