LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2020, 08:58 AM   #1
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 426
Blog Entries: 4

Rep: Reputation: 187Reputation: 187
Question 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.

Last edited by dogpatch; 09-03-2020 at 10:34 AM. Reason: correct code sample (per SoftSprocket)
 
Old 09-03-2020, 10:18 AM   #2
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 324

Rep: Reputation: Disabled
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.
 
2 members found this post helpful.
Old 09-03-2020, 10:41 AM   #3
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 426

Original Poster
Blog Entries: 4

Rep: Reputation: 187Reputation: 187
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?
 
Old 09-03-2020, 10:50 AM   #4
SoftSprocket
Member
 
Registered: Nov 2014
Posts: 324

Rep: Reputation: Disabled
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();
 
1 members found this post helpful.
Old 09-03-2020, 12:17 PM   #5
boughtonp
Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 570

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366

Quote:
Originally Posted by dogpatch View Post
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/


Last edited by boughtonp; 09-03-2020 at 12:19 PM.
 
1 members found this post helpful.
Old 09-03-2020, 01:09 PM   #6
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 426

Original Poster
Blog Entries: 4

Rep: Reputation: 187Reputation: 187
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.

Last edited by dogpatch; 09-03-2020 at 01:11 PM.
 
Old 09-03-2020, 03:46 PM   #7
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 426

Original Poster
Blog Entries: 4

Rep: Reputation: 187Reputation: 187
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.
 
  


Reply

Tags
array, count, error message, global, php


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] close() generates compiler warning dogpatch Programming 3 11-14-2018 01:36 PM
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
Warning: [fnn_insert] Column count doesn't match value count at row 1 in bondoq Programming 2 09-27-2011 04:11 PM
[newbie][php] global array pawelch Programming 3 05-20-2011 05:00 AM
PHP: Converting a PHP array into an HTML array koosha Programming 4 08-11-2009 08:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration