Using emoji in PHP original

by Freek Van der Herten – 1 minute read

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.

Join 9,500+ smart developers

Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

"Always fresh, useful tips and articles. Carefully selected community content. My favorite newsletter, which I look forward to every time."

Bert De Swaef — Developer at Vulpo & Youtuber at Code with Burt

No spam. Unsubscribe anytime. You can also follow me on X.

Found something interesting to share? Submit a link to the community section.