Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Using emoji in PHP

Using plain PHP it’s kinda hard to display emoji characters.

In PHP 5 those characters could be generated by using json_encode.

echo json_decode('"\uD83D\uDE00"'); //displays ?

I bet no one can type this code by heart. In PHP 7 it’s a little bit easier. The hot new version of PHP allows unicode characters to be specified inside a string. This is what that looks like:

echo "\u{1F600}"; //displays ?

A little bit better than the PHP 5 version but still pretty hard to type.

For a project I needed to use some emoji characters. That’s why I made a little PHP 7 package that makes working with emoji a lot easier. This is how you can use it:

echo Emoji::grinningFace(); //displays ?

All characters currently listed on unicode.org’s list of emoji’s are supported. Take a look at the emoji-package on GitHub to learn all the options.

EDIT: It turns out that emoji characters can be displayed in PHP 5 / PHP 7 just by… echo’ing them:

echo "?"; //displays ?

The package above may still be of use when your used IDE or font can’t display emoji characters correctly in source code.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.
  • myran1178

    Hello! Can use this if I only use many different editors online in a web browser instead of a downloaded editor? If yes then let me know how to make it usable in a online editor! If no then let me know how to install it and use it in a downloaded editor in a easier way than the composer!