[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-general
Subject:    [PHP] Re: Redundant isset() check?
From:       Christoph Becker <cmbecker69 () gmx ! de>
Date:       2014-11-04 12:37:12
Message-ID: 5458C878.4090103 () gmx ! de
[Download RAW message or body]

Dotan Cohen wrote:

> Consider the situation where an application will use a different set
> of PDO connection details depending on which version of the site is
> running: production, dev, qa, cron job, etc. Considering that
> $_SERVER['DOCUMENT_ROOT'] may not be set, is this check sufficient:
> 
> if ( !isset( !isset($connection_details[$_SERVER['DOCUMENT_ROOT']]) ) {
> 
> Or would it be preferable to first check that
> $_SERVER['DOCUMENT_ROOT'] is set before relying on it:
> 
> if ( !isset($_SERVER['DOCUMENT_ROOT']) ||
> !isset($connection_details[$_SERVER['DOCUMENT_ROOT']]) ) {
> 
> The TRUE case of this if clause will simply use the default connection details:
> $connection_details[0]

Then you might consider using the following:

  $index = isset($_SERVER['DOCUMENT_ROOT'])
      ? $_SERVER['DOCUMENT_ROOT'] : 0;
  do_something_with($connection_details[$index]);

BTW: after upgrading to PHP 7 you could use the null coalesce operator[1]:

  $index = $_SERVER['DOCUMENT_ROOT'] ?? 0;

[1] <https://wiki.php.net/rfc/isset_ternary>

-- 
Christoph M. Becker

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic