I'm looking for the quickest/shortest way to get the first value from comma separated string, in-line.
The best I can do is
$string = 'a,b,c,d';
echo "The first thing is " . end(array_reverse(explode(',', $string))) . ".";
but I feel that's excessive and redundant. Is there a better way?
list($first) = explode(',', 'a,b,c,d');
var_dump($first); // a
probably works :)
In PHP 6.0 you will be able to simply:
$first = explode(',', 'a,b,c,d')[0];
But this is a syntax error in 5.x and lower
How about
echo reset(explode(',', 'a,b,c,d'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With