Jump to content

integer < 6 what's going on with 0 then?


davo
 Share

Recommended Posts

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...