John Davidson

php - elseif value is less than getting value or greater than

0 comments
Message:


I am trying to show echo 2 but its not working


$zipcode1 = 07300-011;
$zipcode= str_replace("-","",$zipcode1);
$zipcode = 07300011;
if ($zipcode >= 20000000 && $zipcode <= 26600999) {
echo '1';
}
elseif ($zipcode >= 07000001 && $zipcode <= 07399999) {

echo '2';
}
else {
echo 'no value';
}


Please let me know where i am doing wrong. Thank you for the help
Result = 2



My Answer:
The issue in your code is with the way you are assigning the value to the $zipcode variable. You are using a leading zero in the value (07300011), which is causing PHP to interpret it as an octal number. Octal numbers in PHP are represented with a leading zero, and in this case, 07300011 is equivalent to 2500009 in decimal.

To fix this issue, you should remove the leading zero from the value assigned to $zipcode. Here is the corrected code:

php
$zipcode1 = '07300-011';
$zipcode = str_replace('-', '', $zipcode1);

if ($zipcode >= 20000000 && $zipcode <= 26600999) {
echo '1';
} elseif ($zipcode >= 7000001 && $zipcode <= 7399999) {
echo '2';
} else {
echo 'no value';
}


With this correction, the code should now correctly output '2' when the $zipcode value is within the specified range.

Rate this post

5 of 5 based on 2790 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us