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; 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. |
Aside from this line which I doubt is what you meant:
Code:
$myarray($mycounter++) = "SomeValue"; Code:
function B { |
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? |
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; |
Quote:
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/ |
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; Code:
$c = count($loc_array); Code:
$c1 = count($sub_array); 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); Code:
$array0 = add_array("0"); |
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)) { 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. |
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. |