Can this be done in 1 line?

Posted by Angelo on Stack Overflow See other posts from Stack Overflow or by Angelo
Published on 2010-06-07T23:05:20Z Indexed on 2010/06/08 0:42 UTC
Read the original article Hit count: 269

Filed under:
|
|

Can this be done in 1 line with PHP?

Would be awesome if it could:

$out = array("foo","bar");
echo $out[0];

Something such as:

echo array("foo","bar")[0];

Unfortunately that's not possible. Would it be possible like this?

So I can do this for example in 1 line:

echo array(rand(1,100), rand(1000,2000))[rand(0,1)];

So let's say I have this code:

 switch($r){
      case 1: $ext = "com"; break;
      case 2: $ext = "nl"; break;
      case 3: $ext = "co.uk"; break;
      case 4: $ext = "de"; break;
      case 5: $ext = "fr"; break;
 }

That would be much more simplified to do it like this:

$ext = array("com","nl","co.uk","de","fr")[rand(1,5)];

© Stack Overflow or respective owner

Related posts about php

Related posts about array