mastubbs Posted July 7, 2013 Share Posted July 7, 2013 Hi All, I have a script that relies on a numeric variable from GET. I am trying to figure out how to have a 'default' value if no GET value is passed in the url. I have been trying to get it to work with if() but no joy: $par2 = $_GET['par1']; if(empty($par2)) { $par = 4; } $par = $par2; So if a value for 'par1' is passed in the url then $par will be whatever that value is. However if no value is passed then the default value for $par should be 4. Unfortunately that code doesn't seem to work. If i pass no 'par1' value in the url i jest get a php error. Any tips on where im going wrong here would be much appreciated. Thanks in advance for any help, Matt Quote Link to comment https://forums.phpfreaks.com/topic/279927-if-get-variable-empty-then-default-value/ Share on other sites More sharing options...
thara Posted July 7, 2013 Share Posted July 7, 2013 (edited) I think you have missed else part from your 'if' condition. try this. if(empty($_GET['par1'])) { $par = 4; } else { $par = $_GET['par1']; } In this case you can also use php ternary operator: your code should be something similar to this using Ternary Operator $par = (empty($_GET['par1'])) ? 4 : $_GET['par1']; Edited July 7, 2013 by thara Quote Link to comment https://forums.phpfreaks.com/topic/279927-if-get-variable-empty-then-default-value/#findComment-1439743 Share on other sites More sharing options...
Barand Posted July 7, 2013 Share Posted July 7, 2013 Better to check if it is set also $par = (isset($_GET['par1']) && !empty($_GET['par1']) ) ? $_GET['par1'] : 4; Quote Link to comment https://forums.phpfreaks.com/topic/279927-if-get-variable-empty-then-default-value/#findComment-1439757 Share on other sites More sharing options...
litebearer Posted July 7, 2013 Share Posted July 7, 2013 Being that the OP is looking for the variable to be numeric. wouldn't it be prudent to incorporate a check to ascertain it is numeric? Quote Link to comment https://forums.phpfreaks.com/topic/279927-if-get-variable-empty-then-default-value/#findComment-1439758 Share on other sites More sharing options...
mastubbs Posted July 7, 2013 Author Share Posted July 7, 2013 I think you have missed else part from your 'if' condition. try this. if(empty($_GET['par1'])) { $par = 4; } else { $par = $_GET['par1']; } In this case you can also use php ternary operator: your code should be something similar to this using Ternary Operator $par = (empty($_GET['par1'])) ? 4 : $_GET['par1']; Whops yep i forgot the 'else', thanks. Thanks also for introduction to ternary operator. Matt Quote Link to comment https://forums.phpfreaks.com/topic/279927-if-get-variable-empty-then-default-value/#findComment-1439762 Share on other sites More sharing options...
mastubbs Posted July 7, 2013 Author Share Posted July 7, 2013 Ah... i have noticed a problem here. Sometimes the value of 'par1' will be 0. The problem with the methods above is that they seem to assume that par1 = 0 is the same as empty. Is there a way to get it to recognise 0 as a number, but still default to '4' if empty ? Thanks all for the help, Matt Quote Link to comment https://forums.phpfreaks.com/topic/279927-if-get-variable-empty-then-default-value/#findComment-1439774 Share on other sites More sharing options...
Solution AbraCadaver Posted July 7, 2013 Solution Share Posted July 7, 2013 $par = (isset($_GET['par1']) && is_numeric($_GET['par1'])) ? $_GET['par1'] : 4; //or $par = (isset($_GET['par1']) && $_GET['par1'] != '') ? $_GET['par1'] : 4; Quote Link to comment https://forums.phpfreaks.com/topic/279927-if-get-variable-empty-then-default-value/#findComment-1439801 Share on other sites More sharing options...
mastubbs Posted July 7, 2013 Author Share Posted July 7, 2013 $par = (isset($_GET['par1']) && is_numeric($_GET['par1'])) ? $_GET['par1'] : 4; //or $par = (isset($_GET['par1']) && $_GET['par1'] != '') ? $_GET['par1'] : 4; Perfect, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/279927-if-get-variable-empty-then-default-value/#findComment-1439813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.