Re: echo returnArray()['a']; // workaround

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Nathan Nobbe wrote:
On Thu, Feb 28, 2008 at 6:14 PM, Nathan Rixham <nrixham@xxxxxxxxx> 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 :)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux