#43236 [Opn->Bgs]: "if set" and "if not set" functions (ifset, ifnset)
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)