Unexpected Bracket '[' - PHP

Unexpected bracket - PHP

If you'd format your code properly so it'd actually be readable, the superfluous bracket (and missing opening bracket) would be easy to spot:

    <div class="media">
<?php echo ($inf['post_url']) ?>
<img src="<?php echo($inf['photos'][0]['alt_sizes'][0][url]); ?>" />
</div>
{block:Caption}
<?php if (array_key_exists('caption', $inf))?>
<div class="copy">
<?php Echo ($inf['caption']);?>
</div>
<?php }; ?>
<?php }; ?>

Unexpected bracket '[' - PHP

That's called array dereferencing and only works in PHP 5.4+. You're probably running PHP 5.3.x wherever you are getting that error.

See results based on different PHP versions

Php square brackets syntax error

The first is because the new [] syntax for instantiating arrays only works in 5.4 and above. So, replace it with array():

// 5.4+ only:
array($userName => ['score' => $score]);
// 5.3 (and earlier) and 5.4+
array($userName => array('score' => $score));

The second is a different 5.4 feature, that of accessing arrays returned from functions, where you should use a temporary variable:

// 5.4+ only:
$this->Auth->user()['id']
// 5.3 (and earlier) and 5.4+:
$result = $this->Auth->user()
$result[id]

Or, for preference, upgrade your production server to a PHP version that's a bit more modern than the four-or-five year old version you're using. To save on more of these headaches, you either need to do that or start developing locally in 5.3. (If you need to do the latter, I'd look into virtualising your development setup, so you could develop in a virtual box against 5.3 for older production systems.)

PHP Array Syntax Parse Error Left Square Bracket [

You can't use function array dereferencing

return f1(/*some args*/)[0];

until PHP 5.4.0 and above.

PHP Parse error: syntax error, unexpected '['

That particular syntax (function array dereferencing) has been introduced in PHP 5.4. Previous versions of PHP would mark it as a syntax error.

You could use the PHP < 5.4 syntax instead:

$categories = get_the_category($post->ID);
$category = $categories[0]->name;

Lower versions of php complaining about brackets. Why?

In older PHP versions you have to assign result from reset (or any other function) to variable and then access it using [].

 $shipping = reset($arrShipOptions['options']);
$shipping = $shipping[0]['price'];

I have issues with stdclass [brackets] and I cant tell why

Actually, you need 5.4 to use that construct. The docs say;

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

EDIT: Your followup regarding how to change the code for 5.3 requires using a temporary variable and index on that instead. That means translating;

public function first() {
return $this->results()[0];
}

...to...

public function first() {
$tmp = $this->results();
return $tmp[0];
}

PHP Parse error syntax error

In the latest version of PHP that's fine, for backward compatibility, I recommend something like:

$clicks = each($array); $click = $clicks[1];

Now use $click instead of $clicks, in your code below.

Syntax error: Unexpected '[', what am I doing wrong?

Take a look at the manual on arrays: http://www.php.net/manual/en/language.types.array.php Specifically look at example 7.

It says that:

// on PHP 5.4
$secondElement = getArray()[1];

// previously
$tmp = getArray();
$secondElement = $tmp[1];

So you need to do:

$table = $html->find('table[class=Table2]'); 
$table = $table[0]


Related Topics



Leave a reply



Submit