Re: repetition of tedious references
| From: | Richard Lynch | Date: | Fri, 20 Jul 2007 23:42:02 +0000 |
| Subject: | Re: repetition of tedious references | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-259260@lists.php.net to get a copy of this message | ||
On Wed, July 18, 2007 7:24 am, Olav Mørkrid wrote:
> consider the following statement:
>
> $language =
> isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) &&
> $_SERVER["HTTP_ACCEPT_LANGUAGE"] != "" ?
> $_SERVER["HTTP_ACCEPT_LANGUAGE"] : "*";
You can do it nicely in 2:
$language = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ?
$_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
if ($language == '') $language = '*';
> when using strings in arrays that may be non-existing or empty, you
> have to repeat the reference *three* times, which gets excessive and
> unreadable.
>
> is there any way to only have to write
> $_SERVER["HTTP_ACCEPT_LANGUAGE"] only once?
Not for this, I don't think...
PHP 6 or 5.3 might have something where you could do:
(expression) ? : '';
And the result of (expression) would be the implied result between ? :
But you have a different test (isset) from the result (actual value)
so that won't help.
--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?