acctman Posted September 6, 2008 Share Posted September 6, 2008 I wrote the coding below and was wondering if it could be simplified a bit. what i'm checking for is to see if the user has selected a value for c_state or u_state (both can not be selected at the same time) which ever one is selected the value is saved to $state. Then IF c_state or u_state are both blank then show an error. I'm trying to improve on my coding and not write messy/long coding all the time $state = $_POST['c_state'] != '' || $_POST['u_state'] != ''; if ($_POST['c_state'] && $_POST['u_state'] == '') { $err = 'select a state<br>'; } Link to comment https://forums.phpfreaks.com/topic/122974-can-this-be-simplified/ Share on other sites More sharing options...
cooldude832 Posted September 6, 2008 Share Posted September 6, 2008 You can rewrite it but still you have to run the checks (this is just getting fancy <?php $i = 0; $fields = array("c_state","u_state") while(empty($state) && $i < count($fields)){ if(!empty($_POST[$fields[$i]])){ $state = $_POST[$fields[$i]]; } $i++; } if(empty($state)){ echo "Error"; } ?> Lotta ways to accomplish it do what ever logicaly makes sense to you. Link to comment https://forums.phpfreaks.com/topic/122974-can-this-be-simplified/#findComment-635064 Share on other sites More sharing options...
genericnumber1 Posted September 6, 2008 Share Posted September 6, 2008 edit: nevermind, misread. Link to comment https://forums.phpfreaks.com/topic/122974-can-this-be-simplified/#findComment-635067 Share on other sites More sharing options...
bretticus Posted September 6, 2008 Share Posted September 6, 2008 With only two variables to consider, I think a straight-forward if block makes the most sense and is more readible: <?php if ( empty($_POST['u_state']) && empty($_POST['c_state'])) { echo "An error occurred! State was not set."; } elseif (!empty($_POST['u_state'])) { $state = $_POST['u_state']; } else { $state = $_POST['c_state']; } ?> Link to comment https://forums.phpfreaks.com/topic/122974-can-this-be-simplified/#findComment-635069 Share on other sites More sharing options...
acctman Posted September 6, 2008 Author Share Posted September 6, 2008 $state = $_POST['c_state'] != '' || $_POST['u_state'] != ''; if ($_POST['c_state'] && $_POST['u_state'] == '') { $err = 'select a state<br>'; } so that coding would be consider bad/too basic compared to the other Link to comment https://forums.phpfreaks.com/topic/122974-can-this-be-simplified/#findComment-635118 Share on other sites More sharing options...
bluejay002 Posted September 6, 2008 Share Posted September 6, 2008 you may write it many different ways but the thing is to keep the readability of your code. keep it simple yet readable, that's the thing. the net has a lot of coding guidelines to provide. Link to comment https://forums.phpfreaks.com/topic/122974-can-this-be-simplified/#findComment-635148 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.