0

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 !

2
  • I'm not sure if this problem can be answered with the give info. Could you try adding more context? Commented Mar 19, 2010 at 23:23
  • It's a bug so weird that I can't begin to find a reson why this occured, but after a reboot it disapear. Probably should have try that sooner.. Commented Mar 22, 2010 at 12:40

3 Answers 3

1

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()

Sign up to request clarification or add additional context in comments.

Comments

0

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?

3 Comments

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"
I kind of find the answer after rebooting my computer, the problem didn't occur anymore.
0

What's the scope of $compiled_tags?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.