davo Posted June 12, 2014 Share Posted June 12, 2014 I'm using the following code to test if average flying time is less than 6 hours and is not blank. This seems to work until I test it with an integer of 0. Surely, 0 is less than 6? $farflung = ""; $airport = $page->DMC_select->DMCstats_Airports->first(); $flying_time = $page->DMC_select->DMC_Average_Flying_Time_hours; if($page->DMC_select->DMC_Average_Flying_Time_hours > 1) { $plural = "s";} if(($page->DMC_select->DMC_Average_Flying_Time_hours < 6) && ($page->DMC_select->DMC_Average_Flying_Time_hours != "")) { $flying_message .= "With the average flight from a UK airport to {$airport->title} ({$country}) at just {$flying_time} hour{$plural} this is an ideal short haul destination."; }else{ $farflung = "far flung {$page->DMC_select->DMC_Average_Flying_Time_hours}";} Link to comment Share on other sites More sharing options...
adrian Posted June 12, 2014 Share Posted June 12, 2014 You need to change your: != "" to: !== "" EDIT: It's a PHP zero empty thing Google "php empty zero" or something else along those lines. 1 Link to comment Share on other sites More sharing options...
davo Posted June 12, 2014 Author Share Posted June 12, 2014 ah yes... Makes sense now I think. Because != is not equal to and !== is not equal and not the same type, and 0 is effectively empty. ( I think)! Cheers This latest project is a real learning curve; steep but a fun climb! Link to comment Share on other sites More sharing options...
gRegor Posted June 12, 2014 Share Posted June 12, 2014 Personally, I'd prefer casting it explicitly to an integer. That way if it's empty, it will become a zero. Also, since you set the average flying time to a shorter variable, might as well re-use it. $farflung = ''; $airport = $page->DMC_select->DMCstats_Airports->first(); $flying_time = (int) $page->DMC_select->DMC_Average_Flying_Time_hours; if ( $flying_time > 1 ) { $plural = 's'; } if ( $flying_time > 0 && $flying_time < 6 ) { $flying_message .= "With the average flight from a UK airport to {$airport->title} ({$country}) at just {$flying_time} hour{$plural} this is an ideal short haul destination."; } else { $farflung = "far flung {$flying_time}"; } Link to comment Share on other sites More sharing options...
davo Posted June 12, 2014 Author Share Posted June 12, 2014 Personally, I'd prefer casting it explicitly to an integer. That way if it's empty, it will become a zero. Also, since you set the average flying time to a shorter variable, might as well re-use it. $farflung = ''; $airport = $page->DMC_select->DMCstats_Airports->first(); $flying_time = (int) $page->DMC_select->DMC_Average_Flying_Time_hours; if ( $flying_time > 1 ) { $plural = 's'; } if ( $flying_time > 0 && $flying_time < 6 ) { $flying_message .= "With the average flight from a UK airport to {$airport->title} ({$country}) at just {$flying_time} hour{$plural} this is an ideal short haul destination."; } else { $farflung = "far flung {$flying_time}"; } Good point about the shortened variable. I noticed it myself after I'd posted and questioned my own daft coding. Unfortunately nulls can't be zeros because I'm not the person inputting page and not all fields on all pages are complete. Link to comment Share on other sites More sharing options...
gRegor Posted June 13, 2014 Share Posted June 13, 2014 $var = (int) NULL; Will result in zero, unless there is some weird PHP setup I'm not aware of. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now