monkeybidz Posted October 2, 2007 Share Posted October 2, 2007 Is there anything else that could represent less than 0 or empty besides: <0 I have this to work with: if(empty($miles) || !$miles) { return false; } The problem is that 0 represents an actual good value from my database. Example: if($zip1 == $zip2 || $zip2 == $zip1) { $miles = 0 ; } This is good. Problem: if(empty($miles) || !$miles) { return false; } This takes 0 for empty. Can i do something to make it ignore the 0 and act on stricktly empty? Quote Link to comment https://forums.phpfreaks.com/topic/71492-solved-value-lower-than-0/ Share on other sites More sharing options...
php_tom Posted October 2, 2007 Share Posted October 2, 2007 Is your DB entry an integer? or a string? could you just use the string "NULL" for empty? Quote Link to comment https://forums.phpfreaks.com/topic/71492-solved-value-lower-than-0/#findComment-360011 Share on other sites More sharing options...
Orio Posted October 2, 2007 Share Posted October 2, 2007 You can simply do: if((empty($miles) || !$miles) && $miles !== 0) { return false; } Btw, in your first if you don't need the two conditions, as they are the same. It's enough only one of them. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/71492-solved-value-lower-than-0/#findComment-360032 Share on other sites More sharing options...
monkeybidz Posted October 2, 2007 Author Share Posted October 2, 2007 Did not work. Returns false to empty, 0 or anything above 0. The database info is just lattitude and longitude which are called by the script depending on the users zip code form input result which are then calculated by the script to determine the distance between the two. When $zip1 == $zip2 the result is 0 because it is the same city. When the information does not exsist in database for a certain zip code the script is supposed to catch this, but it is not. Maybe the problem lies in this code. $z = new zipcode_class; $miles = $z->get_distance($zip1, $zip2); if (empty($zip1)|| $zip1 === 'false'.$z){ $msg[] = $error1; } Same code applies to $zip2. Quote Link to comment https://forums.phpfreaks.com/topic/71492-solved-value-lower-than-0/#findComment-360221 Share on other sites More sharing options...
cooldude832 Posted October 2, 2007 Share Posted October 2, 2007 You want it to fail if it is blank, but not if it is numerically =>0 so why not jsut say if(empty($miles) || !$miles >= 0){ return false; } Quote Link to comment https://forums.phpfreaks.com/topic/71492-solved-value-lower-than-0/#findComment-360228 Share on other sites More sharing options...
monkeybidz Posted October 2, 2007 Author Share Posted October 2, 2007 It fails all above 0 using !$miles >=0. I found this article that may be the cause: empty("0") The perhaps most cotroversal change in behavior has happend to the behavior of the empty(). A String containing only the character '0' (zero) is now considered empty while it wasn't in PHP 3.0. This new behavior makes sense in web applications, with all input fields returning strings even if numeric input is requested, and with PHP's capabilities of automatic type conversion. But on the other had it might break your code in a rather subtle way, leading to misbehavior that is hard to track down if you do not know about what to look for. Quote Link to comment https://forums.phpfreaks.com/topic/71492-solved-value-lower-than-0/#findComment-360239 Share on other sites More sharing options...
roopurt18 Posted October 2, 2007 Share Posted October 2, 2007 Convert the value to an integer. if($empty($miles) || ((int)$miles) < 0){ return false; } Quote Link to comment https://forums.phpfreaks.com/topic/71492-solved-value-lower-than-0/#findComment-360252 Share on other sites More sharing options...
monkeybidz Posted October 2, 2007 Author Share Posted October 2, 2007 I managed to trick the script by putting the include datacheck passed the problem point and converting the miles from 0 to 1 before the data check. Now the data check file will read empty or 0 as true empty. I have nothing else that could = 0 anymore. $z = new zipcode_class; $miles = $z->get_distance($zip1, $zip2); $english_format_number = number_format($miles);//Added to round off mileage// if($zip1 == $zip2 || $zip2 == $zip1) { $miles = 1; } include ('datacheck1.php'); // This was at the top of the page before. Just moved it down and changed $miles. All your code would have probably worked had i done this change earlier. Thank guys! Quote Link to comment https://forums.phpfreaks.com/topic/71492-solved-value-lower-than-0/#findComment-360280 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.