0

It seems to be a very simple question, I use "0888" as my input value example here. I have researched for a while and tried different ways, such as

(int) "0888";
intval("0888");
floatval("0888");
settype("0888", "integer");

Unfortunately none of them seems to work. Reference 1 Reference 2 Reference 3

$num = $_POST['postcode'];

if((1000 <= $num && $num <= 1999) || (2000 <= $num && $num <= 2599) || (2619 <= $num && $num <= 2898) || (2921 <= $num && $num <= 2999)){
    $state = "NSW";
}
elseif((0200 <= $num && $num <= 0299) || (2600 <= $num && $num <= 2618) || (2900 <= $num && $num <= 2920)){
    $state = "ACT";
}
elseif((3000 <= $num && $num <= 3999) || (8000 <= $num && $num <= 8999)){
    $state = "VIC";
}
elseif((4000 <= $num && $num <= 4999) || (9000 <= $num && $num <= 9999)){
    $state = "QLD";
}
elseif((5000 <= $num && $num <= 5799) || (5800 <= $num && $num <= 5999)){
    $state = "SA";
}
elseif((6000 <= $num && $num <= 6797) || (6800 <= $num && $num <= 6999)){
    $state = "WA";
}
elseif((7000 <= $num && $num <= 7799) || (7800 <= $num && $num <= 7999)){
    $state = "TAS";
}
elseif((0800 <= $num && $num <= 0899) || (0900 <= $num && $num <= 0999)){
    $state = "NT";
}
else {
    $state = "Can not find this postcode record";
}

If I echo $stateI expect to see NT but actually I see "Can not find this postcode record" instead. Can anyone tell me what the problem is?

Community
  • 1
  • 1
Andrew
  • 5
  • 4

3 Answers3

1

0299 is the octal notation of a number, which is very different from the decimal 299. In fact, 9 is invalid in octal and 0299 just has the value 2. Try echo 0299;, then echo 0123;.

If you're treating postcodes as numbers at all, you should strip leading 0s from it and don't use leading 0s in companions:

$code = ltrim($_POST['postcode'], '0');

... $code <= 299 ...
deceze
  • 510,633
  • 85
  • 743
  • 889
0

Use trim then ltrim for zeros, the second parameter allows using any characters you want to trim.

<?php
$num = $_POST['postcode'];
$num = ltrim( trim($_POST['postcode'], ' ') , '0'); //trim SPACES or ZERO

If you are sure the number comes in the left you can use ltrim(

// Just to be sure, finally reconvert it to (int)
$num = (int) $num;
fedmich
  • 5,343
  • 3
  • 37
  • 52
  • Actually you should try to trim on the right too, if this is from a POST/textboxes, sometimes user type in spaces, zeros. – fedmich Sep 02 '13 at 07:23
  • Actually you should try to trim spaces on the right too, if this is from a POST/textboxes, sometimes user type in spaces, zeros. When we examined a large db before, a lot of users had inserted spaces then, probably by mistake. – fedmich Sep 02 '13 at 07:25
  • 1
    If someone entered 9000 and you trimmed all the zeroes to the right it would then become 9 which is essentially 8991 less than 9000, how is that the same? – Hanky Panky Sep 02 '13 at 07:25
0

intval will by default parse your strings base 10 so $num = intval("0888"); will correctly parse the string to 888. No need to trim.

The integer literals in your if statements, however, get interpreted as octal numbers. Remove the leading zeros there:

[...] elseif((800 <= $num && $num <= 899)  [...]
flup
  • 26,937
  • 7
  • 52
  • 74