Where you can (and can't) use Emoji in PHP


I was noodling around in PHP the other day and discovered that this works:

PHP PHP<?php
$🍞 = "bread";
echo "Some delicious " . $🍞;

I mean, there's no reason why it shouldn't work. An emoji is just a Unicode character (OK, not just a character - but we'll get on to that), so it should be fine to use anywhere.

Emoji work perfectly well as function names:

PHP PHPfunction 😺🐢() {
   echo "catdog!";
}
😺🐢();

Definitions:

PHP PHPdefine( "❓", "huh?" );
echo ❓;

And, well, pretty much everywhere:

PHP PHPclass 🦜
{
    public int $🐦;
    public ?string $πŸ¦ƒ;

    public function __construct(int $🐦, ?string $πŸ¦ƒ)
    {
        $this->🐦 = $🐦;
        $this->πŸ¦ƒ = $πŸ¦ƒ;
    }
}
$πŸ“ = new 🦜(1234, "birb");
echo $πŸ“->🐦;

How about namespaces? Yup!

PHP PHPnamespace 😜;
class πŸ˜‰ {
    public function 😘() {
        echo "Wink!";
    }
}

use 😜\πŸ˜‰;
$😊 = new πŸ˜‰();
$😊->😘();

Even moderately complex Unicode sequences work:

PHP PHPecho <<<πŸ³οΈβ€πŸŒˆ
Unicode is magic!
πŸ³οΈβ€πŸŒˆ;

I've written before about the Quirks and Limitations of Emoji Flags. The humble πŸ³οΈβ€πŸŒˆ is actually the sequence U+1F3F3 (white flag), U+FE0F (Variation Selector 16), U+200D (Zero Width Joiner), U+1F308 (Rainbow).

Take a complex emoji like "Female Astronaut with Medium Dark Skin Tone" - πŸ§‘πŸΎβ€πŸš€ - that also works!

PHP PHP$πŸ§‘πŸΎβ€πŸš€ = 1;
$πŸ‘·πŸ»β€β™‚οΈ = 2;
echo $πŸ§‘πŸΎβ€πŸš€ + $πŸ‘·πŸ»β€β™‚οΈ;

Probable the most complex emoji has 10 different codepoints! It looks like this - πŸ§‘πŸΎβ€β€οΈβ€πŸ’‹β€πŸ§‘πŸ»

And it works!

PHP PHP$πŸ§‘πŸΎβ€β€οΈβ€πŸ’‹β€πŸ§‘πŸ» = "Kiss Kiss. Bang Bang!";
echo $πŸ§‘πŸΎβ€β€οΈβ€πŸ’‹β€πŸ§‘πŸ»[-1];

There are some emoji which don't work;

PHP PHP$5️⃣ = "five";

The 5️⃣ emoji is U+0035 (Digit Five), U+FE0F (Variation Selector 16), U+20E3 (Combining Enclosing Keycap). PHP doesn't allow variables to start with digits, so it craps out with PHP Parse error: syntax error, unexpected integer "5", expecting variable or "{" or "$" in php shell code on line 1

You also can't use "punctuation" emoji as though they were normal characters:

PHP PHPecho 5 ❗= 6;

And, while not strictly emoji, you can't use mathematical symbols:

PHP PHPecho 5 ≀ 6;

So, there you have it. Is this useful? Well, probably. It is easy to get lost in a sea of text - so little pictograms can make it easier to see what you're doing. If the basic ASCII characters aren't part of your native language, perhaps it is useful to make use of the full range of Unicode.

Does your favourite programming language support Emoji?


Share this post on…

  • Mastodon
  • Facebook
  • LinkedIn
  • BlueSky
  • Threads
  • Reddit
  • HackerNews
  • Lobsters
  • WhatsApp
  • Telegram

4 thoughts on “Where you can (and can't) use Emoji in PHP”

  1. nicopap says:

    In rust you can use Unicode characters for variable names, but only in the Unicode Standard Annex #31 (aka UAX#31). Basically just "character-like" and "numeric-like" symbols. You can use Cyrillic, Chinese, etc, but no emoji. (well, it does have a special error message if you try to use the crab emoji)

    Now it's also how Python works. The PEP 3131 explains the decision, worth a look, gives a rundown of upsides and downsides, plus it's short.

    Python PEP 3131: https://peps.python.org/pep-3131/

    Rust reference: https://doc.rust-lang.org/reference/identifiers.html

    Annex #31: https://www.unicode.org/reports/tr31/tr31-37.html

    Reply

What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <p> <pre> <br> <img src="" alt="" title="" srcset="">