📜 ⬆️ ⬇️

Type casting in PHP == stool with two legs?

image

- In PHP, type casting works fine, you just need to include common sense.
- And what is there, just compare and all ...
- Wow, some kind of glitch in PHP, look, I compare two floats, they should be the same, and he tells me that they are not equal.
- And, well, when you compare a number with a string, you need to sort through what works.

Heard something similar from colleagues or maybe they themselves had similar situations? Then here's a Friday post with examples about casting, how it works with the == operator.
')
I will be brief, then there will be only primerchiki. And to you, dear reader, please. Some answers are hidden under the spoiler. Before you look back in response under the spoiler, try to answer yourself. For each correct answer, deservedly add yourself a score. At the end of the post in the survey do not forget to put down your result. Agreed?
Then let's go.
Let's start with numbers

What can be simpler than comparing numbers, we can compare numbers from kindergarten.

For the order we check the obvious:

11 == 11 // true,  12 == 11 // false,   12 == 0xC // true,   16- 

How about such a comparison?

 12 == "0xC" 

Try to answer
true. Yes, here the value in the string was converted to integer based on the number system, great, right?

And if so try?

 12 == 014 

Chur do not pry, remember?
true. How else? 014 is the same 12 in the octal number system.

Now so:

 12 == "014" 

Result...
false. Those who wanted and here true, moderate your requirements, “0xC” was transformed into 12 and that's enough.

And so:

 14 == "014" 

Equally...
true, quite expected given the previous example.

Here everything seems to be clear:

 014 == "014" 

Correct answer...
false, although purely visually it is difficult to notice immediately, especially if you read someone else's code.

Attention:

 0 == "0,9" 

Answer...
true, what? And, yes, yes, so false: 0 == "0.9". The comma was considered a string character and she and everything after it was thrown.

Compare these values:

 "1e2" == "100" 

We get ...
true, since 1 multiplied by 10 squared equals 100.

 0b11 == 3 // true, 0b -      

And if 0b11 in the line?

 "0b11" == 3 

Compare ...
false, for the hex system it works, and for the binary system, sorry.

Now let's try this:

 2.333 == "2.333petrovich" // true,          . 2.333 == "ivanovich2.333" // false,    ,     . 0 == "ivanovich2.333" // true,   . 


And then try it yourself:
 "2.33a" == "2.33b" 

And we get ...
false, there is no conversion, strings are compared.

 233 == "233w1" // true,     

Almost the same:

 233 == "233e1" 

Answer...
false, yes, who noticed, here 233 * 10.

 "233" == "233w1" // false 233 == "233*1" // true 

And if so calculated?

 233 == "233*2" 

Here ...
True, again true, by analogy with Petrovich.

 0 == "" // true 

Do not peek first yourself:

 "0" == "" 

Trying to turn on common sense ...
false, accept it.

 "1.000000000000000123" == "1.000000000000000456" // false, ,   . 

Also two float lines:

 "2.000000000000000123" == "2.000000000000000456" 

Guess?
true, do not be surprised, since the numbers look different, but 2 == 2, everything is rounded up to 2.

Boolean comparisons

Since everything is simpler with numbers and all answered correctly, here are some simple examples.

At first such a pun ... I remembered the anecdote about the Lieutenant, but perhaps I will refrain.

 "true" == true // true "false" == true // true,   true "false" == false // false,   true "true" == false // false,   true 

Everything is simple and clear.

 true == "0" // false,       ,    true == "00" // true,    ... true == 00 // false true == "00" + 0 // false,      0  

And if so add:

 true == "01" + 0 

That will get ...
true, and here already to 1 addition.

 true == "0x" + 0 // false 

Space before zero:

 true == " 0" 

Checking ...
true, although someone might expect the space to be rejected and there will be a comparison with zero.

 true == 0x0 // false true == "0x0" // true, -,    0,   true == "0.0" // true,    


 true == " " // true true == [] // false,   -  false true == [0] //     - true 

And if so?

 true == [[]] 

Checking ...
true, here the array contains something for itself, an empty array.

Null

Allow a few more comparisons, now with null.

 null == false // true, null == 0 // true, false == "0" // true, 

Try to guess by analogy:

 null == "0" 

We get ...
false, apparently, you just need to remember.

 null == [] // true,     null == [0] // false,    - 


Arrays

Well and for especially inquisitive - comparisons of arrays.

Here I hope, guess yourself, just do not peek:

 [] == 0 

We are checking ...
Similarly, false, although null == 0 and [] == null.

 [0] == 0 // false, 

The documentation states that "$ a == $ b - TRUE if $ a and $ b contain the same key / value pairs".
We will check how this statement works. Moreover, the dock does not say anything about how the keys are compared.

 [1 => 1] == [1 => 1] // true [1 => 1] == ["1" => 1] // true,  : [1 => 1] == ["0x1" => 1] // false,  ,       : array_keys([1 => 1]) == array_keys(["0x1" => 1]) // true : [1 => 1] == [1 => "0x1"] // true 

Riddle

And for dessert a riddle (a riddle not from me, a colleague once gave it to me).
Can the condition $ x == 1 && $ x == 2 ever be fulfilled, if so, when, if not, why?

And what about the resume?

And what is the summary, a stool with two legs can be used for its intended purpose. Moreover, it has its positive aspects, for example, it helps to keep the vestibular apparatus and buttocks in good shape. So we read docks, stuff cones and everything will be fine.

Source: https://habr.com/ru/post/259497/


All Articles