Re: repetition of tedious references
| From: | Arpad Ray | Date: | Wed, 18 Jul 2007 12:59:00 +0000 |
| Subject: | Re: repetition of tedious references | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-259104@lists.php.net to get a copy of this message | ||
Olav Mørkrid wrote:
consider the following statement: $language = isset($_SERVER["HTTP_ACCEPT_LANGUAGE"]) && $_SERVER["HTTP_ACCEPT_LANGUAGE"] != "" ? $_SERVER["HTTP_ACCEPT_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?You can use empty() to take one of them out, since "0" is presumably also not a desired input: $language = empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? "*" : $_SERVER['HTTP_ACCEPT_LANGUAGE']; There's a new ?: operator in PHP 6 which will make that even shorter, however unlike empty(), it currently throws a notice with unset operands. Arpad