1

I've got several config files, and I'm trying to add them together by including them all in my script.

This returns an error [] operator not supported for strings, but $config is not a string?

I've tried declaring my array first like $config = []; and $config = array();, with no luck.

File: config1.php

$config[] = array(
    'database' => array(
        'name' => 'test'
    )
);

File: config2.php

$config[] = array(
    'sarCallbacks' => array(
        'test1',
        'test2',
        'test3'
    )
);

If I put the array key in the $config index, it gets even worse.

What am I doing wrong?

Note: I'm running PHP 5.5.9.

8
  • 2
    What version of PHP are you running? Commented Apr 29, 2014 at 14:14
  • have you tried defining it with array() Commented Apr 29, 2014 at 14:15
  • What line generates that error? Commented Apr 29, 2014 at 14:18
  • 2
    Are you certain you're not assigning $config as a string somewhere? Commented Apr 29, 2014 at 14:18
  • 1
    @KidDiamond no, it will override the value $config, using foreach() doesn't change the scope :) Commented Apr 29, 2014 at 14:29

2 Answers 2

3

For PHP <= 5.3 it is

$config = array();

The new short-array-syntax $config = []; is for PHP5.4+ as documented in the PHP docs.

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

2 Comments

"Note: I'm running PHP 5.5.9."
@mark Not actually true. His code works fine in 5.2.9 and 5.3.22 and 5.5.9
2

As you mentioned in your comments, you're reusing the variable name $config within a foreach() loop, which overrides $config (control structures do not create a local scope). The value of $config then becomes the last value in your looped array, which is a string. You're then trying to append array elements to that string, resulting in the error above.

"The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope."

http://php.net/manual/en/language.variables.scope.php

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.