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?

4
  • 4
    Most languages interpret strings starting with 0 as octal representations Commented Sep 2, 2013 at 5:57
  • 1
    Post codes are not numbers, especially if they hold a value that is not purely numeric, like a "leading 0". Commented Sep 2, 2013 at 5:59
  • So what is the fix? @flup Commented Sep 2, 2013 at 6:03
  • you need to add a bit more code, maybe you are assigning a variable wrongly Commented Sep 2, 2013 at 6:15

3 Answers 3

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 ...
Sign up to request clarification or add additional context in comments.

1 Comment

You said it, that is actually my problem, Thanks
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;

3 Comments

Actually you should try to trim on the right too, if this is from a POST/textboxes, sometimes user type in spaces, zeros.
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.
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?
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)  [...]

Comments

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.