1

This is my code

        $preference = '151000';
        $range = 'above';
        if($preference <= $range){
            echo "Yes"; die;
        }else{
            echo "No"; die;
        }

This provides 'Yes', i want to know why.

0

3 Answers 3

2

You can see it in the php manual. https://www.php.net/manual/en/language.operators.comparison.php

When comparing a string, number or resource with another string, number or resource:

Translate strings and resources to numbers, usual math

Btw: '151000' is a string, not a number. 15100 would be a number.

Sign up to request clarification or add additional context in comments.

Comments

1

Here you basically compare two strings and php uses their ASCII codes to compare them. The first symbol 1 is lower than 'a'.

If you want to compare two strings properly, use function:

strcmp()

If you want compare different types, you can read about PHP type comparison tables.

Comments

0

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

Read more here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.