Jump to content

Can I shorten this?


tutorialstuff

Recommended Posts

I'm currently creating a search page and I have I'm declaring some php variables and I was wondering if there is a way to shorten this or a better way to do this.

 

if($_GET['age'] != ''){
$age = $_GET['age'];	
}elseif($_POST['age'] != ''){
$age = $_POST['age'];	
}
if($_GET['gender'] != ''){
$gender = $_GET['gender'];	
}elseif($_POST['gender'] != ''){
$gender = $_POST['gender'];	
}
if($_GET['rate'] != ''){
$rate = $_GET['rate'];	
}elseif($_POST['rate'] != ''){
$rate = $_POST['rate'];	
}
if($_GET['last_login'] != ''){
$last_login = $_GET['last_login'];	
}elseif($_POST['last_login'] != ''){
$last_login = $_POST['last_login'];	
}

Link to comment
Share on other sites

Not tested, but something like this should work;

 

<?php
$array = array('age', 'gender', 'rate', 'last_login');

foreach($array as $get)
$$get = (!empty($_GET[$get])) ? $_GET[$get] : $_POST[$get];
?>

 

 

That would throw a notice if $_POST[$get] was not set.

 

 

Perhaps:

 

 

$$get = (!empty($_GET[$get])) ? $_GET[$get] : ((isset($_POST[$get])) ? $_POST[$get] : '');

 

 

 

 

Instead of using variable variables, you could just use an array by the way:

 

 


$data = array();
$keys = array('age', 'gender', 'rate', 'last_login');

foreach($keys as $key) {
    $data[$key] = (!empty($_GET[$get])) ? $_GET[$get] : ((isset($_POST[$get])) ? $_POST[$get] : '');
};

 

 

I don't much like variable variables though ;p.

Link to comment
Share on other sites

That would throw a notice if $_POST[$get] was not set.

 

Of course, always more tidying to do!  :facepalm:

 

Instead of using variable variables, you could just use an array by the way:

 

 


$data = array();
$keys = array('age', 'gender', 'rate', 'last_login');

foreach($keys as $key) {
    $data[$key] = (!empty($_GET[$get])) ? $_GET[$get] : ((isset($_POST[$get])) ? $_POST[$get] : '');
};

 

 

I don't much like variable variables though ;p.

 

That's what I'd personally do, I'm not a fan of variable variables, just wanted the outcome to be as close to the OP as possible.

Link to comment
Share on other sites

Which, technically could be shortened even more.. it just comes down to how readable you really want it.

foreach(array('age', 'gender', 'rate', 'last_login') as $k) 
    $data[$k] = (!empty($_GET[$k])) ? $_GET[$k] : ((isset($_POST[$k])) ? $_POST[$k] : '');
// or, with variable variables.
foreach(array('age', 'gender', 'rate', 'last_login') as $k) 
    $$k = (!empty($_GET[$k])) ? $_GET[$k] : ((isset($_POST[$k])) ? $_POST[$k] : '');

Link to comment
Share on other sites

I guess this would be the shortest;

 

foreach(array('age', 'gender', 'rate', 'last_login') as $k) $$k = !empty($_GET[$k]) ? $_GET[$k] : isset($_POST[$k]) ? $_POST[$k] : '';

 

But yes, how readable is that....

 

Could be worse  :confused:

Link to comment
Share on other sites

I had this sugguested to me in another forum.

 

function getInput($key)
{
   if(isset($_GET[$key]) && trim($_GET[$key]) !== '')
   {
      return(trim($_GET[$key]));
   }
   elseif(isset($_POST[$key]) && trim($_POST[$key]) !== '')
   {
      return(trim($_POST[$key]));
   }
   return null;
}
$age = getInput('age');
$gender = getInput('gender');
// etc.... 

Link to comment
Share on other sites

Working from your thread title I'd have to say our result is far better. One line rather than a script nearly equal in length to the original.

 

On the other hand, if the question was for a readabale, easy to manage, re-usable script then a function similar to that which you posted is probably more desirable.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.