Skip to content
Advertisement

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.

Advertisement

Answer

This character has no particular meaning in PHP, and is probably just a mistake in the code you’re reading. For instance, it may have been intended as '' (an empty string) but got messed up by some markdown-style formatting system.

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.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement