开发者

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

开发者 https://www.devze.com 2022-12-22 16:55 出处:网络
Why doesn\'t 开发者_StackOverflow社区php support this syntax: $s = explode(\'-\', \'foo-bar\')[0];

Why doesn't 开发者_StackOverflow社区php support this syntax:

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

?


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.


You can write it using list:

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


The syntax ‘foo-bar’)[0] is wrong as far as php is concerned. I don't know which language you have seen such thing in but PHP has no implementation for such syntax. However, you can split your string like this:

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


Instead you could use this if you'r force to use inline : substr($var,0, strrpos($var,'-')); But I prefer the list solution , it's more elegant !


0

精彩评论

暂无评论...
验证码 换一张
取 消