Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51954 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 10591 invoked from network); 20 Apr 2011 14:55:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Apr 2011 14:55:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=markg85@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=markg85@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.177 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: markg85@gmail.com X-Host-Fingerprint: 209.85.216.177 mail-qy0-f177.google.com Received: from [209.85.216.177] ([209.85.216.177:38108] helo=mail-qy0-f177.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2B/B3-24878-ED3FEAD4 for ; Wed, 20 Apr 2011 10:55:26 -0400 Received: by qyl38 with SMTP id 38so520930qyl.8 for ; Wed, 20 Apr 2011 07:55:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:from:date:message-id:subject:to :content-type; bh=ytrEtJaYIT1uTm3vp0EuXUOemAg2j977bNQ3hTZ+gA0=; b=CfJNNkSE2/IUJQRDKXpAWmxMXAg/mS+z1kNuPkkrm2V9UGFoP2MJMiWvaJQjR8XsXM HskYw+p5GQxZbbCKcRsmt03O3lTbniqpplwt5SD77dIKe/HpVgJjBktcHU6rtTWtXVK8 bKTwysooIcnp0TJIq+xFnEucRnVxcz6vIlo/I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:from:date:message-id:subject:to:content-type; b=FsLBixTmaI9MAM8AGEFRp01h0jlDcpOY3GMsCxBLBfmcWfTszRR1mU0TbKhAFrW7sk 8gU7po/4yfmML0jMO+R0Rj75S3GRao3btWGWxuSTR4y0ZvgYsS+WYbqQPxC5S1TFjJBi lXdXThV0qzXpnleSoU3ZP4PW8VNgpRQCpL+wY= Received: by 10.229.63.151 with SMTP id b23mr5427958qci.289.1303311323373; Wed, 20 Apr 2011 07:55:23 -0700 (PDT) MIME-Version: 1.0 Received: by 10.229.185.200 with HTTP; Wed, 20 Apr 2011 07:55:00 -0700 (PDT) Date: Wed, 20 Apr 2011 16:55:00 +0200 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=0016e65dc8ec761fd204a15ad034 Subject: Function proposal: varset From: markg85@gmail.com (Mark) --0016e65dc8ec761fd204a15ad034 Content-Type: text/plain; charset=ISO-8859-1 Hi, This proposal is for the often called line like this: $var = isset($_GET['var']) ? $_GET['var'] : ''; Only a shorter and imho a cleaner solution to get the same: $var = varset($_GET['var']); The implementation for this in PHP code is this: # Arg 1 = the variable to check for existence # Arg 2 = the default return value which is an empty string by default function varset($var, $default = '') { return (isset($var) ? $var : $default); } However there is a slight issue with this approach. If notices are turned on this code will generate a notice while i think it should not do that. But perhaps this approach is "to short". A slightly different implementation (and longer) prevents the notice: # Arg 1 = the array in which a given key should be checked for existence # Arg 2 = the key to check in the array from arg 1 # Arg 3 = the default return value which is an empty string by default function varset($arr, $key, $default = '') { return (isset($arr[$key]) ? $arr[$key] : $default); } where the call would be: $var = varset($_GET, 'var'); I personally like both ways... My proposal is to make this function a core php function in PHP 5.4. The added benifit is obvious. People can, with this, use a way shorter notation to validate if a given array element exists. Right now that needs to be done with a ternary, filter_var or some other method (there are quite a few ways to check for existence). I tried to look in the PHP source to see if i could make a patch to add this but i couldn't find the function that defines the isset function (i wanted to base it on that). So a pointer to the right location would be nice (along with docs that tell me what i all need to do to implement a new php function). If someone else wants to make the implementation, please be my guest :) So, what do you think of this? Regards, Mark --0016e65dc8ec761fd204a15ad034--