On Thu, Mar 15, 2012 at 11:04 AM, Tedd Sperling <tedd.sperl...@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;

When not working within my framework (which facilitates this
automatically), I tend to have a function for each just to save time:

function g($key){
   return isset($_GET[$key]) ? $_GET[$key] : null;
}

function p($key){
   return isset($_POST[$key]) ? $_POST[$key] : null;
}

function c($key){
   return isset($_COOKIE[$key]) ? $_COOKIE[$key] : null;
}

function s($key, $val = null){
   !isset($_SESSION) && session_start();

   if ($val === null) {
      return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
   } else {
      return $_SESSION[$key] = $val;
   }
}

Then, you can just write:

$first_name = s('first_name', p('first_name'));

Adam

-- 
Nephtali:  A simple, flexible, fast, and security-focused PHP framework
http://nephtaliproject.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to