tags:

views:

65

answers:

4
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

+3  A: 

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.
Ayman Hourieh
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
@vick - Where do you get the variable's value from? If it's from a form submission, then it's already a string.
Ayman Hourieh
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
vick
You can always cast it to as string:$val = (string) $val2;
Will Shaver
@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.
Ayman Hourieh
@Will - Not really. Once the leading zeros are lost, converting to a string won't recover them.
Ayman Hourieh
A: 

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

erenon
A: 

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";

Shaun Hare
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.
vick
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_padI will edit above
Shaun Hare
that does not work when I try form_zipcde(01002); it returns 00514
vick
?? 1. You need to pass in both params
Shaun Hare
2 and first param is string
Shaun Hare
01002 is treated as an octal number, which is 514 decimal
Marc B
A: 

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??

Shaun Hare
Alternatively, `sprintf('%05d', $zipcode)` would work as well
Marc B
neat much more compact
Shaun Hare