Jump to content

Empty or missing variable


monkeybidz

Recommended Posts

Heres the deal i am working with:

 

if(empty($miles) || !$miles) {

      $msg[] = $error8;

}

 

This code is in a form results page. But when i get the results for $miles as empty, it will ignore the error. This is used in a zip code distance calculator that i am working on. When two zip codes are entered, the miles between both are displayed based on latitude and longitude of the two. Some new zip codes are not present in the database since the database is old. When the zip is present in database, but latitude and longitude are not they return nothing(blank).

 

The question here is what do i use for question:

 

if $miles does not exsist return error:

 

is it !$miles?

Link to comment
Share on other sites

Ok, thought it was solved, but guess not. This is for a mileage calculator based on zip codes.

If you are calculating the distance between two zip codes that are the same i get 0 miles which is fine, but when the zip does not exsist in database it comes back empty. By me adding this message for empty, i also get it when miles = 0.

 

The code is reading 0 as empty, but i need the 0 to not be interpreted as empty, but as an actual result.

 

Is there a way i can say

 

if miles (another word for empty) then use error.

Can it be done?

 

if (empty($miles)) {
      $msg[] = $error8;
}

Link to comment
Share on other sites

Ok. I found this article on the net and it seems to be my problem. I am using a newer version of PHP.:

 

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.

 

Is there anything i can do beside downgrade PHP?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.