Can ($ a == 1 && $ a == 2 && $ a == 3) ever evaluate to true?

Like this question here , which was intended for javascript, it has generated numerous side effects for different languages. I am curious if the following grade can ever be evaluated in PHP:

($a == 1 && $a == 2 && $a == 3) 

To continue a little, it seems that just setting $a = true will give the desired result (this is not the case for javascript, due to the type type casting works in both languages). Several answers (in javascript) also worked with === , so in PHP with the typechecking method ( === ) can the following value match true?

 ($a === 1 && $a === 2 && $a === 3) 
+1
source share
4 answers

I just tried this:

 $a = true; echo ($a == 1 && $a == 2 && $a == 3); 

and he repeated 1 .

Due to type checking, not type checking, 1, 2, 3 will be considered true compared to a boolean value.

Editing Reply: No, this cannot be done.

Hackish method , which @FrankerZ commented:

Zero Byte Character = 0xFEFF

http://shapecatcher.com/unicode/info/65279

http://www.unicodemap.org/details/0xFEFF/index.html

 $var = "1"; $var = "2"; $ var = "3"; echo ($var === "1" && $var === "2" && $ var === "3") ? "true" : "false"; 

This code works with this symbol, because the name $ var and $var appears to be valid for the PHP compiler and with a suitable font, it can be hidden. This can be achieved with Alt + 6 5 2 7 9 on Windows.

+7
source

While not strictly according to the question, this can be done if the ints are enclosed in quotation marks:

 <?php class A { private static $i = 1; public function __toString() { return (string)self::$i++; } } $a = new A(); if($a == '1' && $a == '2' && $a == '3') { echo 'yep'; } else { echo 'nope'; } 
+3
source

I cannot think of a case where a rigorous comparison would ever give the truth. === the operator first compares the types, so there is no way to use magical wizardry.

For curiosity, I could get the closest one to change the setting a bit and crack the variable in the checkmark function. Since ticks only increase by each operator, we must split the comparison into several operators for this to work.

 $a = 1; register_tick_function(function () use (&$a) { ++$a; }); declare(ticks = 1) { $a === 1 or exit(1); $a === 2 or exit(1); $a === 3 or exit(1); } echo "a = $a\n"; 

Try it online .

+2
source
 $a = true; echo ($a == 1 && $a == 2 && $a == 3); 

it's just about casting if you tried this

 echo ($a === 1 && $a === 2 && $a === 3); 

then you will get false as the operator === for type checking. and in your script, he is clearly typing.

luck

-ten
source

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


All Articles