Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:4072 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 17826 invoked from network); 18 Aug 2003 21:02:46 -0000 Received: from unknown (HELO webteks.com) (64.49.255.97) by pb1.pair.com with SMTP; 18 Aug 2003 21:02:46 -0000 Received: (qmail 6897 invoked from network); 18 Aug 2003 21:02:46 -0000 Received: from wsip-68-15-138-59.hr.hr.cox.net (HELO dexby) (68.15.138.59) by webteks.com with SMTP; 18 Aug 2003 21:02:46 -0000 To: Date: Mon, 18 Aug 2003 17:03:08 -0400 Message-ID: <017801c365cc$22455a10$6901a8c0@dexby> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0179_01C365AA.9B33BA10" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2627 Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Subject: Get value if set, or default From: griggs@webteks.com ("Griggs Domler") ------=_NextPart_000_0179_01C365AA.9B33BA10 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit It's rare to find functionality that cannot be effectively implemented in userland PHP code, but this appears to be one of them. The issue here is the capability to check if an array index (or variable) is set, if so, return its value, or return a passed in default value. This can be accomplished using if statements or the ternary operator, but they quickly become tiresome for so routine a task. Especially when dealing with nested associative arrays, for example: $myVar = (isset($_SESSION['application']['section']['page']['title']))?$_SESSION[ 'application']['section']['page']['title']:"Default Title"; Wouldn't this be better: $myVar = getd($_SESSION['application']['section']['page']['title'],"Default Title"); At first glance defining a function that will accomplish this appears easy: function getd($var,$default='') { if(isset($var)) return $var; else return $default; } $myVar = getd($arr['noindex']); But this is not notice level compliant, producing an error if the index doesn't already exist. Re-writing the function to pass by reference seems to fix this at first glance: function getd(&$var,$default='') { . . . } $myVar = getd($arr['noindex']); It no longer gives a notice. But the call mentioned above will now create the index mentioned, setting its value to null, which, while not necessarily wrong, still means that a foreach iteration or array_keys() call will show that the key now exists. (though isset() will not.) Since writing a userland function to accomplish this seems impossible while maintaining notice-level compliance, could this be accomplished at the language level? Perhaps by adding a statement similar to isset? It would seem a very helpful addition to PHP and would not need to affect other language constructs. ------=_NextPart_000_0179_01C365AA.9B33BA10--