LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php global array generates count() warning (https://www.linuxquestions.org/questions/programming-9/php-global-array-generates-count-warning-4175681474/)

dogpatch 09-03-2020 08:58 AM

php global array generates count() warning
 
I need to define an array as global and then populate it and access it through one or more functions. So my php code looks something like:
Code:

global $myarray;
.
.
$myarray = array();

function A () {
global $myarray;
.
.
$myarray[$mycounter++] = "SomeValue";
.
.
}

function B () {
global $myarray
.
.
$var1 = count($myarray);
.
.
}

This seems to run as expected, the count() function returning the current number of elements in the array. But my error log file is always filled with warnings that ". . . count(): Parameter must be an array or an object that implements Countable. . .", referring to the line in function B that invokes count().

I've looked at many tutorials and examples, and can't see what may be wrong with my implementation. Would like to avoid these warning messages, as they make it very difficult to see any real errors that may be hidden in the bloated error log file.

SoftSprocket 09-03-2020 10:18 AM

Aside from this line which I doubt is what you meant:
Code:

$myarray($mycounter++) = "SomeValue";
and this line:
Code:

function B {
There's nothing obvious wrong in what you've posted. If you were to post the smallest working example that demonstrates the problem it will be easier for someone to help you.

dogpatch 09-03-2020 10:41 AM

Thanks for correcting my code sample. Have edited my first post accordingly.

It might take me some time to put together a working example, as the production code is quite lengthy.

If the global array ends up being populated with other non-global arrays, would that create a warning message for count()ing elements in the global array?

SoftSprocket 09-03-2020 10:50 AM

Here's an example of writing a complete example to test something. When I run this it doesn't generate and error or warning.
Code:

global $test_array;
global $n;

$count = 0;
$test_array = array();

function add($str) {
    global $test_array;
    global $n;
   
    $test_array[$n++] = $str;   
}

function add_array() {
    global $test_array;
    global $n;
   
    $a = array();
    $a[0] = "Hello";
    $a[1] = "Embedded";
    $a[2] = "World";
   
    $test_array[$n++] = $a;   
}

function print_count() {
    global $test_array;
   
    $c = count($test_array);
    echo "$c";
}

add("Hello");
add("World");
add_array();

print_count();


boughtonp 09-03-2020 12:17 PM


 
Quote:

Originally Posted by dogpatch (Post 6162143)
It might take me some time to put together a working example, as the production code is quite lengthy.

Duplicate the code then delete everything not relevant - that shouldn't take much time.

Confirm that the code compiles and the problem still exists - if it doesn't, something you removed was relevant, and you need to systematically modify the code until you get the minimal example.

Sometimes the act of cutting it down helps you understand the issue - so it's usually a good idea to start with that before writing a forum post; you may solve the problem quicker, but if not you'll make it easier for others to help you. (Again, so long as the code you share has been confirmed to exhibit the issue and isn't over-sanitized.)

There's a better explanation somewhere, but this one has an easier to remember URL: http://www.sscce.org/


dogpatch 09-03-2020 01:09 PM

Have used your (SoftSprockets) code as a template, but adding a couple peculiarities from my production code. Here's what I have tested:
Code:

global $test_array;
global $array0;
global $array1;

$test_array = $array0 = $array1 = array();

function add_array($array_no) {
  $loc_array = array();
  $ctr = 0;
  switch($array_no) {
  case "0" :
    $a = array();
    $a[0] = "Hello ";
    $a[1] = "World ";
    $a[2] = "from ";
    $a[3] = "Array ";
    $a[4] = "#0";
    $loc_array[$ctr++] = $a;
    $a[0] = "Next ";
    $a[1] = "line ";
    $a[2] = "from ";
    $a[3] = "Array ";
    $a[4] = "#0";
    $loc_array[$ctr++] = $a;
    return($loc_array);
  case "1" :
    $a = array();
    $a[0] = "Hello ";
    $a[1] = "World ";
    $a[2] = "from ";
    $a[3] = "Array ";
    $a[4] = "#1";
    $loc_array[$ctr++] = $a;
    return($loc_array);
  }
}

function print_array($array_no) {
global $array0;
global $array1;
    $sub_array = array();
    $loc_array = array();
    if ($array_no == "0") $loc_array = $array0;
    else if ($array_no == "1") $loc_array = $array1;
    else die("invalid array number");

    $c = count($loc_array);
//    echo "$c"."<br/>";
    for ($c0 = 0; $c0 < $c; $c0++) {
        $sub_array = $loc_array[$c0];
        $c1 = count($sub_array);
        for ($c2 = 0; $c2 < $c1; $c2++)
        print($sub_array[$c2]);
        print ("<br/>(".$c1." words in this line)<br/>");
        }
    print ("<br/>Array ".$array_no." has ".$c." element");
    if ($c != 1) print ("s");
    print ("<br/><br/>");
}

$array0 = add_array("0");
$array1 = add_array("1");
print_array("0");
print_array("1");
print("End eg.php");

I get a warning this line
Code:

    $c = count($loc_array);
but not for this line
Code:

        $c1 = count($sub_array);
This more closely reflects what my production code is doing. That is, function "add_array()" builds a local array and passes its contents back. This return value is assigned to the proper global array. Then in function "print_array()", a different local array is set to the proper global array, and it tries to count the elements. It succeeds but generates a php warning.

Should I declare the local $loc_array in add_array as global? Is there a danger that the contents of that local array might be lost between
Code:

  return($loc_array);
in add_array, and its assignment to a global array below
Code:

$array0 = add_array("0");
Interestingly, the sub-array which is delared locally in one function and read locally in another, generates no count() warning.

dogpatch 09-03-2020 03:46 PM

Forget post #6. Don't know why I was getting a php warning there; maybe was misreading the error log. (Like I mentioned, difficult to read such a bloated file).

Anyway, it occurred to me that I may not always be building anything, so both the local and global arrays are empty. In the sample code, that would be like calling print_array without verifying that add_array had in fact built an array. I think the answer is to include something like
Code:

    if (empty($loc_array)) {
        print("Nothing found<br/>");
        return;
        }

before trying to count().

Will mark this [Solved], make the correction to the production code, delete the error log, and check it later to verify. Thanks, SoftSprocket and boughtonp.

dogpatch 09-22-2020 09:43 PM

Just a follow-up to say that the above did solve the issue, and, since post 7, am no longer getting the count warning messages in my error log file.


All times are GMT -5. The time now is 05:52 AM.