rubing Posted July 23, 2008 Share Posted July 23, 2008 Hey all, I am trying to limit the number of results presented to a client by allowing a limit variable passed in to a GET statement (e.g http://www.example.com/service.php?limit=4"&q=banana) If they don't set a limit (or an improper one) I want the number of results to default to a high number. So, I wrote the following code. This code does not work when limit=0 ? if(!empty($_GET['limit']) && is_numeric($_GET['limit'])) { $limit=(int) $_GET['limit']; } //no limit, so set to ridiculously high number else { $limit=1000000; } Strangely, however when this code executes the limit Quote Link to comment https://forums.phpfreaks.com/topic/116262-else-loop-executing-when-should-not/ Share on other sites More sharing options...
.josh Posted July 23, 2008 Share Posted July 23, 2008 It works the way it's supposed to. 0 is a valid number so you're setting the limit to 0 and therefore no results are being returned. if(!empty($_GET['limit']) && is_numeric($_GET['limit']) && ($_GET['limit'] > 0)) Quote Link to comment https://forums.phpfreaks.com/topic/116262-else-loop-executing-when-should-not/#findComment-597801 Share on other sites More sharing options...
rubing Posted July 23, 2008 Author Share Posted July 23, 2008 no. if i set limit=0 in the URL, then my script assigns $limit the value 1000000, as if 0 was not a number. Meaning if i set limit=0 in the url I get results, when i should not. Quote Link to comment https://forums.phpfreaks.com/topic/116262-else-loop-executing-when-should-not/#findComment-597906 Share on other sites More sharing options...
wildteen88 Posted July 23, 2008 Share Posted July 23, 2008 You have to check $_GET['limit'] is greater than 0 too as CV suggested earlier. Otherwise the else statement will never execute. Quote Link to comment https://forums.phpfreaks.com/topic/116262-else-loop-executing-when-should-not/#findComment-597909 Share on other sites More sharing options...
rubing Posted July 23, 2008 Author Share Posted July 23, 2008 ,but the ELSE statement IS what's executing and it should not. when I say limit=1 or some other number everything is fine...$limit is assigned that number. however, when I say limit=0, the script is executing the else statement, and then setting $limit=1000000. There is no reason why it should do this!! In order to get 0 results I have to actually set limit=-1 So, basically what i'm saying is that my script is ONLY malfunctioning when I set limit=0 Quote Link to comment https://forums.phpfreaks.com/topic/116262-else-loop-executing-when-should-not/#findComment-597948 Share on other sites More sharing options...
CaptainChainsaw Posted July 23, 2008 Share Posted July 23, 2008 Could it be the double quote in your URL? This would pass "4%22" to your script and it wouldn't be numeric so you end up the the else: http://www.example.com/service.php?limit=4%22&q=banana Quote Link to comment https://forums.phpfreaks.com/topic/116262-else-loop-executing-when-should-not/#findComment-597966 Share on other sites More sharing options...
rubing Posted July 24, 2008 Author Share Posted July 24, 2008 no that's just a typo. what i'm saying is that if i type the url: example.com/service.php?limit=0 then the script jumps to execute the else statement Quote Link to comment https://forums.phpfreaks.com/topic/116262-else-loop-executing-when-should-not/#findComment-598165 Share on other sites More sharing options...
trq Posted July 24, 2008 Share Posted July 24, 2008 0 is considered empty, therefore example.com/service.php?limit=0 will execute your else. I would use isset() instead of empty(). Quote Link to comment https://forums.phpfreaks.com/topic/116262-else-loop-executing-when-should-not/#findComment-598171 Share on other sites More sharing options...
rubing Posted July 24, 2008 Author Share Posted July 24, 2008 thank you thorpe. Quote Link to comment https://forums.phpfreaks.com/topic/116262-else-loop-executing-when-should-not/#findComment-598185 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.