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.

3 Answers3

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.

Philipp
  • 2,787
  • 2
  • 25
  • 27
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.

Max Shaian
  • 418
  • 4
  • 11
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.

Leo Lenori
  • 11
  • 3