On Apr 11 12:08:15, Todd Ruth wrote:
> I'm not arguing whether the following code fragment is good
> or bad (it's certainly less than ideal), but given the recent 
> threads, I thought I'd show how I feel I've been encourage by 
> php to code:
> 
> <?php
> $x = array();
> $y = 'something';
> $temp =& $x[$y];
> $temp++;
> unset($temp);
> ?>
> 
> I'm not sure where (if anywhere) that technique is documented
> or even if it should be documented, but if you want to avoid
> "Undefined index" notices, that's one of the more terse
> approaches.  The relative brevity is more obvious when the
> variable names are long and $temp is re-linked several times
> before unsetting.  It's probably less clear than alternatives
> unless you see it often.
> 
> Here is an application to the defaults for configurations thread:
> 
> <?php
> $config = array();
> $param = 'param1';
> $temp =& $config[$param];
> $temp = $temp ?: 'default';
> unset($temp);
> var_dump($config);
> ?>
> 
> It isn't beautiful, but it avoids writing "$config[$param]"
> more than once and it avoids the notices.

I believe that using + for configuration settings makes much more sense:

$defaults = array('foo' => 'bar', 'gee' => 'plop');
$conf += $defaults;

and that's it. You no longer have to worry about 'foo' or 'gee' being
defined, whether you use them or not: $conf is fully defined for every
possible configuration settings.

Now you could of course use the same mechanism for filling $_GET
and $_POST, but I usually prefer leaving $_GET and $_POST untouched, so
that it still reflects what the user actually sent, so even the
technique with references would not work for me.

Best,

> 
> - Todd
> 
> 
> -- 
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
> 

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to