Re: echo returnArray()['a']; // workaround
| From: | Shawn McKenzie | Date: | Fri, 29 Feb 2008 19:27:27 +0000 |
| Subject: | Re: echo returnArray()['a']; // workaround | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-270756@lists.php.net to get a copy of this message | ||
Nathan Rixham wrote:
> Nathan Nobbe wrote:
>> On Thu, Feb 28, 2008 at 6:14 PM, Nathan Rixham <nrixham@gmail.com> wrote:
>>
>>> if I show you guys how to do this:
>>>
>>> echo sillyFunc()['a'] . PHP_EOL;
>>>
>>> using a few brackets and things do you promise not to laugh?
>>> *it's a bit weird*
>>
>>
>> id like to see it.
>>
>> -nathan
>>
>
> don't say I didn't warn ya fellow nathan!
>
> #!/usr/bin/php
> <?php
> function sillyFunc() {
> return array('a' => 1, 'b' => 2, 'c' => 3,
> 'd' => 4, 'e'=>'some
> string');
> }
>
> echo
> !${~${''}='sillyFunc'}=&${''}().${~${''}}['e']
> . PHP_EOL;
>
> --- output ---
> some string
>
>
> To make things a little weirder yet here's the var's lol:
>
> print_r(get_defined_vars());
>
> [] => sillyFunc
> [¹] => Array
> (
> [a] => 1
> [b] => 2
> [c] => 3
> [d] => 4
> [e] => some string
> )
>
>
> to reference the var holding "string sillyFunc" (any of):
> echo ${''}; echo ${NULL}; echo ${FALSE};
>
> to reference our array [¹] (yeah it is called ¹)
> print_r(${~${''}});
>
>
> here's a quick simplification + alternatives.
>
> echo (!$array = sillyFunc()),$array['e'];
> echo (!$array = sillyFunc()).$array['e'];
>
> and a useful ternary one:
> echo is_array($array = sillyFunc()) ? $array['e'] : '';
>
>
> told you it was a bit weird [took me a couple hours to figure out]!
>
> nath :)
ROFLMAO, ha ha ha ha ha ha ha ha hoo ha ha ha ha ha hooo haaa!