6

I am optimizing my website a bit. Tested the page on local, everything is fine. When I upload it, and access it live, it sudently throws a parse error... but it working perfectly locally, like I said.

 Parse error: syntax error, unexpected '[', expecting ',' or ';' in /home/theriff/www/frvideos.php on line 25

the code is the following one:

echo explode('|',$youtube[$i])[2].'<br />'."\r\n";

$youtube[$i] is a line formated like this:

DFHG-LINKYOUTUBE-HJGHJ|french Description|English Description

The youtube link only is the ID so there's no '|' symbol in it for sure, and it's read from a text file I write myself manually, so I am sure of the entry.

Does anyone know why it's working fine on local (EasyPhp Developper) but not on the distant server?

bjb568
  • 11,089
  • 11
  • 50
  • 71
user3916429
  • 562
  • 6
  • 25

2 Answers2

4
$results =  explode('|',$youtube[$i]);
echo $results[2].'<br />'."\r\n";

Version of PHP is not the same so there's no array chaining available on the 'distant server'.

Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
  • yes yes... it works now... thanks you, i will +rep you when i can. Do you have any idea why it was orking as it on local, but not on distant server maybe? – user3916429 Aug 24 '14 at 10:39
  • 1
    @user3916429 Because the versions of PHP differ. @knittl has linked to the docs on `Array dereferencing` which has only become available in php 5.4. Your production [read: *distant*] server is running php 5.3 or less. – Ohgodwhy Aug 24 '14 at 10:40
3

Old (< 5.4) PHP versions cannot directly dereference arrays of function return values, you have to temporarily store the result in a variable:

$exploded = explode('|',$youtube[$i]);
echo $exploded[2].'<br />'."\r\n";
knittl
  • 246,190
  • 53
  • 318
  • 364