Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse error on explode('-','foo-bar')[0] (for instance)

Tags:

php

parsing

Why doesn't php support this syntax:

$s = explode('-', 'foo-bar')[0];

?

like image 697
lost3den Avatar asked Apr 23 '26 11:04

lost3den


2 Answers

It's a limitation in the PHP parser. There's no reason why it can't support this form of reduction, it just doesn't.

like image 81
Ignacio Vazquez-Abrams Avatar answered Apr 26 '26 01:04

Ignacio Vazquez-Abrams


You can write it using list:

list($first_value) = explode(‘-’,‘foo-bar’);
like image 33
Ivan Nevostruev Avatar answered Apr 26 '26 01:04

Ivan Nevostruev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!