tags:

views:

80

answers:

3

I've been going mad trying to figure out why an array would not be an array in php.

For a reason I can't understand I have a bug in a smarty class. The code is this :

$compiled_tags = array();
for ($i = 0, $for_max = count($template_tags); $i < $for_max; $i++) {
   $this->_current_line_no += substr_count($text_blocks[$i], "\n");

   // I tried array push instead to see
   // bug is here
   array_push($compiled_tags,$this->_compile_tag($template_tags[$i]));
   //$compiled_tags[] = $this->_compile_tag($template_tags[$i]);

   $this->_current_line_no += substr_count($template_tags[$i], "\n");

}

the error message is

Warning: array_push() expects parameter 1 to be array, integer given in ....

OR before with []

Warning: Cannot use a scalar value as an array in ....

I trying a var_debug on $compiled_tags and as soon I enter the for loop is not an array anymore but an integer. I tried renaming the variable, but same problem.

I'm sure is something simple that I missed but I can't figure it out. Any help is (as always) welcomed !

A: 

As far as I'm aware, $compiled_tags[] will ALWAYS work. There may be a problem somewhere else in your code. Maybe _compile_tag() uses $compiled_tags as a global?

St. John Johnson
that's what i though too. but i search into my entire project and no result. I'm starting to think i just need to reboot my computer :-)
I recommend commenting-out one piece of code at a time. Remove $this->compile_tag() and replace it with "test"
St. John Johnson
I kind of find the answer after rebooting my computer, the problem didn't occur anymore.
A: 

What's the scope of $compiled_tags?

It looks like the method _compile_tag(...) may be setting it to an integer.

symcbean
A: 

The variable $compiled_tags is getting overwritten by something, probably the method call.

Try adding print_r($compiled_tags); between each line and then see where it changes from an empty array to a scalar. I would bet it happens after the method call $this->_compile_tag()

Cal