2
function is_zipcode_valid($zipcode){ ... }

if I call that function with is_zipcode_valid(08004); my parameter 08004 gets in as 8004, basically it removed all precending 0's.

How can I get around that problem?

thanks

1
  • 1
    Zipcodes and postal codes are not numbers. They are identifiers that happen to contain numerical digits. Always handle them as strings. Commented Apr 23, 2010 at 1:17

4 Answers 4

3

Pass it as a string and modify your function to expect a string instead:

is_zipcode_valid('08004');

It's not possible to preserve leading zeros when using integers. If an integer value has a leading zero, it is interpreted in base 8. Here is an example:

echo 010; // Output is 8.
Sign up to request clarification or add additional context in comments.

6 Comments

I store the zipcode in a variable $zipcode; how can I tell php that its a string and not integer? Ideally I would like my function to be bulletproof and be able to handle both strings and integers, im sure there is a way.
@vick - Where do you get the variable's value from? If it's from a form submission, then it's already a string.
its used in numerous places..in other functions, sometimes I fetch it from mysql and use that function, its widely used in a big system
You can always cast it to as string: $val = (string) $val2;
@vik - Just make sure it's always handled as a string. If a ZIP code has leading zeros, the zeros may be irreversibly lost once you convert it to an integer.
|
0

PHP treats integers as base8 numbers if a leading zero is present. Pass your number as a string

Comments

0

As everyone says pass it in as a string.

if php5 you can make sure it is string going in by casting

$zipcode = (string) $zipcode; 

or

$zipcode = "$zipcode"; 




  function is_zipcode_valid( $zipcode){ 

   is_string($zipcode) //returns true
 }

See Php.Net

Note: Instead of casting a variable to a string , it is also possible to enclose the variable in double quotes.

//create zipcode from number function

    function form_zipcode($zipcode,$desiredLength){ 
              if(str_len($zipcode) != $desiredLength)
                  {
                       return str_pad($zipcode,$desiredLength,"0",STR_PAD_LEFT);
                  }
                else
                 {
                  return $zipcode;
                  }
       }

Using form_zipcode(55,5) would return "00055";

5 Comments

ok thanks!! but I want that to happen in the function.. I want this to work even is_zipcode_valid(55); that should get converted to 00055 within the function.
I would say it is not a is_zipcode_valid() function that you want it is a form_zipcode() function To do what you want you could just use str_pad I will edit above
that does not work when I try form_zipcde(01002); it returns 00514
?? 1. You need to pass in both params
01002 is treated as an octal number, which is 514 decimal
0

Sorry typo last night (GMT here)

    <?php
echo form_zipcode("55",5);

function form_zipcode($zipcode,$desiredLength){ 
              if(strlen($zipcode) != $desiredLength)
                  {
                       return str_pad($zipcode,$desiredLength,"0",STR_PAD_LEFT);
                  }
                else
                 {
                  return $zipcode;
                  }
       }
 ?>

Works as expected returns 00055 That not what you require??

1 Comment

Alternatively, sprintf('%05d', $zipcode) would work as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.