TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   What does this function do? (http://www.talkphp.com/absolute-beginners/5355-what-does-function-do.html)

Dave 03-15-2010 09:36 PM

What does this function do?
 
Could someone briefly explain this function?

PHP Code:

function l($var
{
    global 
$l;
    return 
$l[$var];


Thanks!

delayedinsanity 03-15-2010 10:46 PM

php Code:
$l = array( 'one' => 'value for one', 'two' => 'value for two' );

// These both assign 'value for one' to $value
$value = $l['one'];
$value = l( 'one' );

It's just an overcomplicated method to return a value of $l based on its key. It's also a waste of resources, considering it has to first retrieve $l from the global namespace, and then just pipes back a value that you could have easily gotten on your own (as illustrated above). It doesn't perform any error checking to see if the value exists and return a default if it doesn't, which is the only reason I could see it being useful.

sketchMedia 03-16-2010 10:35 AM

Well that's one of the most pointless functions I have seen in a good long while. Not only have you got the extra and unnecessary function call overhead, your also using a global variable, which as we all know can be linked to global warming and 3rd world debt.

Dave 03-16-2010 01:17 PM

I thought that had all been disproved, and Al Gore had switched to .NET.

delayedinsanity 03-16-2010 03:00 PM

Al Gore created .NET, but he uses Basic.

3rd world countries have debt? I thought we gave them the option to offer up their first born sons to the Masons and then we supported them with supplies of AK-47s to handle any dissidents?

Dr John 03-18-2010 02:56 PM

Al Gore helped with the funding for .NET, long before global variables came along to personally help programmers.

cerealgirl 03-20-2010 11:10 PM

And isn't thisfunction a shining example of why we should comment our code and select logical function names...

Quote:

Originally Posted by Dave (Post 30243)
Could someone briefly explain this function?

PHP Code:

function l($var
{
    global 
$l;
    return 
$l[$var];


Thanks!

Ummm, I'm pretty sure it's a variable variable - basically a way to have a dynamically named variable. Here they are in the manual http://php.net/manual/en/language.va...s.variable.php
Cool though they may be, I never use variable variables, because they're a bit confusing - they save a few lines of code but lose maintainability and readability IMHO.

Enfernikus 03-21-2010 05:24 AM

Quote:

Originally Posted by cerealgirl (Post 30272)
And isn't thisfunction a shining example of why we should comment our code and select logical function names...


Ummm, I'm pretty sure it's a variable variable - basically a way to have a dynamically named variable. Here they are in the manual http://php.net/manual/en/language.va...s.variable.php
Cool though they may be, I never use variable variables, because they're a bit confusing - they save a few lines of code but lose maintainability and readability IMHO.

Brackets not curly braces.

delayedinsanity 03-21-2010 05:40 AM

php Code:
foreach ( $_POST as $key => value )
    $_POST[$key] = my_sanitize( $value, $args );

extract( $_POST );

// or

foreach ( $_POST as $key => $value )
    ${$key} = my_sanitize( $value, $args );

No readability lost there. :) Not exactly the best use case example in the world, but in the end variable variables are just helpful shortcuts for those who like them.


All times are GMT. The time now is 05:30 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0