-1

Similar to this question here which was intended for javascript, it has spawned off numerous spin-offs for various different languages. I'm curious if the following can ever evaluate to true in PHP:

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

To follow up a bit more, it seems simply setting $a = true will yield the desired result (This was not the case for javascript, due to the way type casting works in both languages). A few answers (in javascript) worked with === as well, so in PHP with typechecking (===), can the following ever yield true?

($a === 1 && $a === 2 && $a === 3)
Goodbye StackExchange
  • 21,680
  • 7
  • 47
  • 83
  • If you could search SO for the javascript explanation, why not search for the php explanation on SO? – mickmackusa Jan 19 '18 at 08:40
  • My dupe link explains php's strict and loose comparisons ad nauseum. Plus the php manual is also a decent reference. I'll spend a little more time searching for something better if that one doesn't do it for you. – mickmackusa Jan 19 '18 at 09:06
  • https://stackoverflow.com/a/5894395/2943403 – mickmackusa Jan 19 '18 at 09:12
  • 5
    @mickmackusa You keep just linking how type casting works...I Get it. The challenge becomes, how do we get a variable to equal 1, 2, and 3. It's not a clear cut solution, and simply slapping a downvote/close vote on this doesn't help. I *highly suggest* you check the linked question where I got my inspiration for this question. – Goodbye StackExchange Jan 19 '18 at 09:14
  • @mickmackusa The answers to that question do not directly answer this question. I don't think It's a duplicate. – All Workers Are Essential Jan 19 '18 at 14:39
  • @cpburnz I don't even think they indirectly answer this question. – Goodbye StackExchange Jan 19 '18 at 15:10
  • @FrankerZ After seeing Taha Paksu's answer and if you squint hard enough you could argue that you could derive his answer from the comparison table for `==` from the other question. But that's quite the stretch because you might as well close questions as duplicates for RTFM then. – All Workers Are Essential Jan 19 '18 at 15:26
  • I don't mean to keep stirring but here is another related link: https://stackoverflow.com/questions/8380452/type-casting-to-boolean The 3 close votes will probably age away. You may as well grant the green tick so that the system deems this question resolved. – mickmackusa Jan 20 '18 at 23:24
  • Possible duplicate of [Can (a ==1 && a== 2 && a==3) ever evaluate to true?](https://stackoverflow.com/questions/48270127/can-a-1-a-2-a-3-ever-evaluate-to-true) – Taylor Sen Feb 01 '18 at 15:35
  • @TaylorSen Not a valid dupe target. It was pretty clear in the Moderator note (72 upvotes), that we should *please also refrain from posting more answers in different languages*. That question is clearly intended for javascript, and this for PHP. – Goodbye StackExchange Feb 01 '18 at 15:42

3 Answers3

8

I just tried this:

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

and it echoed 1.

Because of the type casting and not type checking, 1, 2, 3 will be treated as true when compared to a boolean value.

Answer to the edit: No it can't be done.

Hackish method which @FrankerZ commented about:

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 runs with this character because the name $ var and $var  seems to be valid for the PHP compiler and with the appropiate font, it can be hidden. It can be achieved with Alt + 65279 on Windows.

Taha Paksu
  • 14,293
  • 1
  • 39
  • 67
  • PHP is a funny language. – Goodbye StackExchange Jan 19 '18 at 08:03
  • 2
    @FrankerZ to make it less funnier, there is an `===` operator. – Taha Paksu Jan 19 '18 at 08:04
  • As a PHP programmer, I tend to avoid two things: dynamic type-juggling, and non-typed variables – e_i_pi Jan 19 '18 at 08:05
  • Edited my answer. – Taha Paksu Jan 19 '18 at 08:07
  • Interesting. Because window is the global javascript "object", defining properties on that can yield the results where even `(a === 1 && a === 2 && a === 3)`. It's amazing what javascript can do, when you put your mind to it. – Goodbye StackExchange Jan 19 '18 at 08:14
  • Yep I was trying to set an getter to a variable on PHP and if I succeed, this would be done in PHP too ;) – Taha Paksu Jan 19 '18 at 08:17
  • @TahaPaksu I'm going to give this about a day for other people to see if this is indeed possible. I'll accept this answer if there are no other replies. – Goodbye StackExchange Jan 19 '18 at 08:21
  • I'm a little sad this got so many down-votes, thought this would have been an interesting question to research, to see if this was possible in PHP. Unfortunately, it seems like variables can't be considered getters. I was able to get this working with a true value for: [`($a->a === 1 && $a->a === 2 && $a->a === 3)`](https://www.tehplayground.com/NJwPLrFYIcFFvFzb) – Goodbye StackExchange Jan 19 '18 at 14:07
  • Yes it can also work with `$a() === 1 && $a() === 2 && $a() === 0` but not with variables itself without modifying the core `int` type I guess. – Taha Paksu Jan 19 '18 at 14:09
  • @TahaPaksu It does appear that it may be possible to accomplish this with the Unicode characters such as 0 width spaces, similar to [this answer](https://stackoverflow.com/a/48274520/4875631). – Goodbye StackExchange Jan 19 '18 at 15:11
3

Whilst not strictly in keeping with the question, this can be done if the ints are wrapped in quotes:

<?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';
}
Andy
  • 4,462
  • 5
  • 31
  • 53
2

I can't think of a case where strict comparison would ever yield true. === operator compares the types first, so there's no way to use any magic method wizardry.

For curiosity the closest i could get is to slightly modify the setting and hack the variable in a tick function. Since ticks are only incremeted per statement, we have to break the comparison to multiple statements 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.

Joe
  • 1,488
  • 9
  • 9