Php function integer parameter problem
4 answers
As everyone says, pass it as a string.
if php5 you can make sure it is a string by typing
$zipcode = (string) $zipcode;
or
$zipcode = "$zipcode";
function is_zipcode_valid( $zipcode){
is_string($zipcode) //returns true
}
See Php.Net
Note. Instead of casting the variable to a string, you can also enclose the variable in double quotes.
// create zipcode from numeric 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) will return "00055";
0
a source to share
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. 00055 Isn't that what you want?
0
a source to share