php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52907 "is" operator -- test whether two lvalues are references to each other
Submitted: 2010-09-22 17:18 UTC Modified: 2016-03-26 23:57 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:3 (100.0%)
From: a_different_name at hotmail dot com Assigned:
Status: Duplicate Package: Variables related
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2010-09-22 17:18 UTC] a_different_name at hotmail dot com
Description:
------------
Currently, PHP has "==", which loosely checks for equality, and "===", which 
strictly checks for equality.  However, for strings and booleans and such, both 
only test for equality -- there's no way to know (without altering the variable's 
contents) whether two variables are identical references unless they refer to an 
object.

If PHP allows the creation of references, there should be some safe and built-in 
way of testing whether that's been done, so values don't get unexpectedly mangled.

Test script:
---------------
$a = 'test';
$b = "$a";  // Interpolated, so it should not be pooled or anything if PHP does that
$c =& $a;   

// These all print
if ($a == $b) print "\$a == \$b\n";
if ($b == $c) print "\$b == \$c\n";
if ($a == $c) print "\$a == \$c\n";

// These all print too, as expected
if ($a === $b) print "\$a === \$b\n";
if ($b === $c) print "\$b === \$c\n";
if ($a === $c) print "\$a === \$c\n";

// I use "is" here, because other languages use it
// if they don't already use /==+/ for this purpose.
if ($a is $b) print "\$a is \$b\n"; // should not print
if ($b is $c) print "\$b is \$c\n"; // should not print
if ($a is $c) print "\$a is \$c\n"; // should run

Expected result:
----------------
$a == $b
$b == $c
$a == $c
$a === $b
$b === $c
$a === $c
$a is $c

Actual result:
--------------
Parse error.  (There is no "is" operator yet, and no reasonable replacement.)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-22 17:27 UTC] a_different_name at hotmail dot com
The second line of the test script would actually be better as

$b = $a;

as it would test whether ($a is $b) is broken if you simply assign one variable to 
another.  (It should be false if changing $a won't also change $b, but my 
understanding is that PHP does some copy-on-write thing when assigning -- meaning 
that $a and $b could *appear* identical even though they're not.)
 [2016-03-26 23:57 UTC] nikic@php.net
-Status: Open +Status: Duplicate
 [2016-03-26 23:57 UTC] nikic@php.net
Duplicate of bug #40051.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat May 25 00:01:27 2019 UTC