Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a PHP setting that sets whether you can index the result of a function?

I have two servers. They are both running php 5.3.3. This code works on one server and returns a syntax error on the other. Is there a php ini setting that affects this behaviour? I can't find anything related in the PHP documentation, but I may be looking in the wrong place.

Server 1

> php -v
PHP 5.3.3 (cli) (built: Sep 23 2010 14:15:16) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
with Xdebug v2.0.3, Copyright (c) 2002-2007, by Derick Rethans

php > echo explode(" ", " foo ")[1];
foo

Server 2

> php -v
PHP 5.3.3 (cli) (built: Jan 31 2011 15:57:29) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

php > echo explode(" ", " foo ")[1];
Parse error: syntax error, unexpected '[', expecting ',' or ';' in php shell code on line 1

Another idea: PHP on both servers is custom-compiled, so it could also be a different compile flag.

like image 231
Kyle Wiens Avatar asked Jun 12 '11 18:06

Kyle Wiens


2 Answers

No.

PHP doesn't support this syntax. It's on the trunk, but not yet released (as of PHP 5.3.3).

I have no idea how it's working on your first server, but perhaps this "Xdebug" is making a difference?

like image 93
Lightness Races in Orbit Avatar answered Nov 03 '22 08:11

Lightness Races in Orbit


Aha! I figured it out.

We installed Facebook's XHP to profile our development server. This syntax (which is quite elegant) was added in in the PHP module. Here's a diff of the php.ini file between server 1 and 2:

> ; XHP https://github.com/facebook/xhp/wiki/Building-XHP
> extension=xhp.so
> ; adds support for the [] operator on the return value of a function
> xhp.idx_expr = 1
> ;  Tracking errors in XHP applications is very difficult without annotations.
> xhp.include_debug = 1

I like this syntax, so I'll probably install XHP on the other server. Thanks for the help Michas for suggesting I diff the ini files.

like image 21
Kyle Wiens Avatar answered Nov 03 '22 08:11

Kyle Wiens