This character has no particular meaning in PHP, and is probably just a mistake in the code you're reading.
Until PHP 8.0, it doesn't actually produce an error due to the combination of two slightly odd features:
- Any name that would be a valid constant name, but isn't currently defined as a constant, is treated instead as a string, as though you'd defined the constant with a value the same as the name. This feature was removed in PHP 8.0.
- Any byte that is outside of 7-bit ASCII (i.e. values 128 to 255) is allowed in a variable or constant name, even at the start. Since this character isn't in ASCII, it will be rendered in UTF-8 using a bunch of bytes that match this rule - note that PHP doesn't actually care about their meaning in UTF-8, it just sees they're non-ASCII and lets you use them wherever.
The result is that ’ == $old_value
is actually interpreted as '’' == $old_value
, i.e. "does $old_value
match a string containing the character ’
?"
In PHP 8.0, this interpretation is no longer allowed, and you'll get an error at runtime that the constant isn't defined. However, you can actually define this constant, and the code will run on all PHP versions from 4.3 up to and including 8.0 without any errors or warnings at all:
<?php
// Define our oddly-named constant to have a value of 'hello'
define('’', 'hello');
// Compare it against a normal string variable
$foo = 'hello';
if ( ’ == $foo ) {
echo 'It works!';
}
Just to be clear, this isn't particularly useful, unless you're trying to play a prank on someone, it just explains the surprising fact that this isn't a syntax error.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…