Hey.

Question on PHP theory on comparison operators. These are <,>, <=,> =, ==! ...

To understand what will happen as a result of the operation, you need to understand TWO things :

  1. What type of operands the interpreter EXPECTS to see when using the comparison operator .

  2. How the interpreter converts one data type to another .

There are a total of 8 data types in the PHP language. The question is what data type the interpreter expects to see with different combinations of operands and operators, for example, comparing a string with integer, strings with a boolean type, integers with a boolean type, real numbers with a boolean type, a number and NULL , NULL and an array ...

If I compare the two lines to each other, then the ambiguity is obtained. How to resolve ambiguity? enter image description here

    2 answers 2

    if operand 1 is null or string, and the second operand is string, then NULL is converted to "", numeric or lexical comparison. I do not understand

    This means that the following expression

    NULL == "строка" 

    will be converted to

     "" == "строка" 

    and further comparison will follow the rules for two lines.

    Quote from the same page, just above:

    In case you compare a number with a string or two strings containing numbers, each string will be converted to a number , and they will be compared as numbers.

    That is, a numerical comparison will be made:

     "0" > "-1" // да "55" > "221" // нет "44" > "22" // да 44 > "88" // нет 44 > "abc" // да 

    If none of the strings is converted into a number, then a lexical comparison will be made:

     "a" > "b" // нет "abc" > "acb" // нет "def" > "acb" // да "44" > "abc" // нет 
    • appreciated. Thanks for the reply - Dimon
    • got a question. I added a picture to the question. if I compare a string with a string, then the table produces two different comparison options. ambiguity - Dimon
    • @Dimon, there is no ambiguity there. The creators of the table assume that the person looking at it will guess: we are talking about all possible combinations ("string-resource", "number-array", etc), the enumeration of all variants of which would take too much space. And the string-string comparison (as well as the number-number, and the array-array) always follow the algorithms that are listed in the table separately. - PinkTux

    The answer to the question is quite volume, moreover, it is described in detail in various manuals, for example: http://php.net/manual/ru/language.operators.comparison.php

    In short, there is a certain algorithm that the PHP interpreter uses when it encounters various types of data in comparison. It is described in the article.

    • one
      In principle, 4 scrolls, it is quite possible to move here, referring ;-) - Alexey Shimansky
    • there is a table. I saw this table before, I do not understand how the table works. for example, take string 1. write, if operand 1 is null or string, and the second operand is string, then NULL is converted to "", numeric or lexical comparison. I do not understand, "NULL is converted to" ", numeric or lexical comparison" - Dimon
    • @Dimon if you try to compare null with a string, then nul will be converted to a string of zero length, and then a string comparison will be performed. “Numeric or lexical” means that the lines will be compared by letter, but if there is a number in one of the lines, each line will be converted to a number (the empty one will be converted to 0) and they will be compared as numbers. - Crantisz