#43236 [Opn->Bgs]: "if set" and "if not set" functions (ifset, ifnset)

From: Date: Sat, 10 Nov 2007 21:38:30 +0000
Subject: #43236 [Opn->Bgs]: "if set" and "if not set" functions (ifset, ifnset)
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-119419@lists.php.net to get a copy of this message
 ID:               43236
 Updated by:       johannes@php.net
 Reported By:      zyss at mail dot zp dot ua
-Status:           Open
+Status:           Bogus
 Bug Type:         Feature/Change Request
 Operating System: All
 PHP Version:      5.2.5
 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[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 http://bugs.php.net/?id=43236&edit=1


Thread (3 messages)

« previous php.bugs (#119419) next »