Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

symbols - What does ’ (fancy apostrophe?) mean in PHP

I got this example PHP code:

if ( $new_value && ’ == $old_value )
  1. What do I call the character after &&
  2. What does it do
  3. How do I type it on my keyboard, and
  4. Most importantly, what can I use in its place that is human readable and doesn't look like I'm trying to show off my knowledge of obscure code shorthand?

I don't find it here: Reference — What does this symbol mean in PHP? and since Google ignores it, and most other searches treat the same as '(apostrophe) I'm coming up empty.

question from:https://stackoverflow.com/questions/65846246/what-does-fancy-apostrophe-mean-in-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...