Re: $POST and $_SESSION

From: Date: Thu, 15 Mar 2012 18:34:03 +0000
Subject: Re: $POST and $_SESSION
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-317035@lists.php.net to get a copy of this message
On 15 Mar 2012, at 18:31, Stuart Dallas wrote: > On 15 Mar 2012, at 15:13, Daniel Brown wrote: > >> On Thu, Mar 15, 2012 at 11:04, Tedd Sperling <tedd.sperling@gmail.com> wrote: >>> Hi gang: >>> >>> What's a better/shorter way to write this? >>> >>> $first_name = $_SESSION['first_name'] ? $_SESSION['first_name'] : >>> null; >>> $first_name = isset($_POST['first_name']) ? $_POST['first_name'] : >>> $first_name; >>> $_SESSION['first_name'] = $first_name; >> >> A simple reusable function and pass-through variable is one method: >> >> <?php >> >> session_start(); >> $_SESSION['first_name'] = $first_name = >> thisorthat(@$_POST['first_name'],null); >> >> function thisorthat($a,$b) { >> return isset($a) ? $a : $b; >> } >> >> ?> > > The @ prefix is banned from all code I go anywhere near - it's evil! I've used the > following 'V' function for a long time, primarily for accessing the superglobals but it > works for any array. > > <?php > session_start(); > $_SESSION['first_name'] = $first_name = V($_SESSION, 'first_name'); > > function V($arr, $key, $def = null) { > return isset($arr[$key]) ? $arr[$key] : $def; > } > ?> That'll teach me for not reading the code properly... <?php session_start(); $_SESSION['first_name'] = V($_POST, 'first_name', V($_SESSION, 'first_name')); function V($arr, $key, $def = null) { return isset($arr[$key]) ? $arr[$key] : $def; } ?> -Stuart -- Stuart Dallas 3ft9 Ltd http://3ft9.com/

« previous php.general (#317035) next »