Re: repetition of tedious references

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

 



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

--
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