Req #43236 [Com]: "if set" and "if not set" functions (ifset, ifnset)

From: Date: Sun, 16 Sep 2012 14:24:12 +0000
Subject: Req #43236 [Com]: "if set" and "if not set" functions (ifset, ifnset)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-173482@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=43236&edit=1

 ID:                 43236
 Comment by:         jay-php at vengefulstapler dot com
 Reported by:        zyss at mail dot zp dot ua
 Summary:            "if set" and "if not set" functions (ifset, ifnset)
 Status:             Not a bug
 Type:               Feature/Change Request
 Package:            Feature/Change Request
 Operating System:   All
 PHP Version:        5.2.5
 Block user comment: N
 Private report:     N

 New Comment:

johannes, I haven't had a chance to test out ?: yet but someone else said that 
$val = $var['foo'] ?: ''; would still throw a notice if $var['foo']
was not set.

If that is true, then it would still be nice to have something like what is 
described here.


Previous Comments:
------------------------------------------------------------------------
[2007-11-10 21:38:29] johannes@php.net

PHP 6 has the "?:" operator ($result = $foo ?: $bar;) which is close to your request and
which will most likely be merged back to 5.3.

------------------------------------------------------------------------
[2007-11-10 20:32:33] zyss at mail dot zp dot ua

Description:
------------
PHP programmers often need to check if certain variable is set when assigning its value to other
variable or using it in expression. For example:

  $item_id = isset($_POST['item_id']) ? $_POST['item_id'] : null;

which is quite unreadable.

It would be much easier to write such things if there are function ifnset(mixed $value, mixed $alt)
(If Not Set) which returns $value if $valus is set or $alt otherwise. Example above could be
replaced with:

  $item_id = ifnset($_POST['item_id'], null);

or, if another function is added - ifset - which returns null if its argument is not set or not
defined:

  $item_id = ifset($_POST['item_id']);

Similar function exists in MySQL
(http://dev.mysql.com/doc/refman/5.1/en/control-flow-functions.html#function_ifnull) which makes
life much easier when working with MySQL.

It is not possible to implement this function in PHP because when undefined or unset variable is
passed as function argument PHP emits warning. Although warnings could be suppressed with @ it
significantly slows down performance when such situation happens. Besides debugger catches all
suppressed warnings anyway.

Thanks.



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=43236&edit=1


Thread (3 messages)

« previous php.bugs (#173482) next »