tutorialstuff Posted July 27, 2009 Share Posted July 27, 2009 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 More sharing options...
gevans Posted July 27, 2009 Share Posted July 27, 2009 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]; ?> Link to comment https://forums.phpfreaks.com/topic/167667-can-i-shorten-this/#findComment-884245 Share on other sites More sharing options...
tutorialstuff Posted July 27, 2009 Author Share Posted July 27, 2009 Not a bad idea gevans! Anybody else have some other thoughts? Link to comment https://forums.phpfreaks.com/topic/167667-can-i-shorten-this/#findComment-884248 Share on other sites More sharing options...
corbin Posted July 27, 2009 Share Posted July 27, 2009 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 More sharing options...
gevans Posted July 27, 2009 Share Posted July 27, 2009 That would throw a notice if $_POST[$get] was not set. Of course, always more tidying to do! 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 More sharing options...
Philip Posted July 27, 2009 Share Posted July 27, 2009 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 More sharing options...
gevans Posted July 27, 2009 Share Posted July 27, 2009 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 Link to comment https://forums.phpfreaks.com/topic/167667-can-i-shorten-this/#findComment-884270 Share on other sites More sharing options...
tutorialstuff Posted July 27, 2009 Author Share Posted July 27, 2009 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 More sharing options...
gevans Posted July 27, 2009 Share Posted July 27, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.