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
https://forums.phpfreaks.com/topic/167667-can-i-shorten-this/
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
https://forums.phpfreaks.com/topic/167667-can-i-shorten-this/#findComment-884257
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
https://forums.phpfreaks.com/topic/167667-can-i-shorten-this/#findComment-884262
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
https://forums.phpfreaks.com/topic/167667-can-i-shorten-this/#findComment-884265
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
https://forums.phpfreaks.com/topic/167667-can-i-shorten-this/#findComment-884282
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
https://forums.phpfreaks.com/topic/167667-can-i-shorten-this/#findComment-884284
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.