Explosion analysis error ('-', 'foo-bar') [0] (for example)

Why php does not support this syntax:

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

?

+3
source share
4 answers

This is a limitation in the PHP parser. There is no reason why he cannot support this form of contraction, just not.

+7
source

You can write it with list:

list($first_value) = explode(‘-’,‘foo-bar’);
+5
source

‘foo-bar’)[0] php. , ​​, PHP . :

explode(‘-’, ‘foo-bar’);
0

Instead, you can use this if you are forced to use inline: substr ($ var, 0, strrpos ($ var, '-')); But I prefer a list solution, it's more elegant!


0
source

Source: https://habr.com/ru/post/1735795/


All Articles