Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is PHP throwing this parse error?

I have a small question for you all. I currently have my site on 000webhost, and the following line:

$price  = explode(".",$item->sellingStatus->currentPrice)[0];

causes the following error:

Parse error: syntax error, unexpected '[' in /home/a1257018/public_html/scripts/myfile.php on line 58

When it doesn't cause this on localhost. The code should work... explode returns an array and [0] simply calls the first item. Why is this throwing an error?

like image 713
Nickersoft Avatar asked Dec 19 '22 22:12

Nickersoft


1 Answers

This syntax is only allowed in PHP 5.4+. You have to use temporary variables in older versions:

$tmp = explode('.', $item->sellingStatus->currentPrice);
$price  = $tmp[0];

Has been discussed on SO.

like image 126
just lerning Avatar answered Dec 24 '22 01:12

just lerning