Oh Dear is the all-in-one monitoring tool for your entire website. We monitor uptime, SSL certificates, broken links, scheduled tasks and more. You'll get a notifications for us when something's wrong. All that paired with a developer friendly API and kick-ass documentation. O, and you'll also be able to create a public status page under a minute. Start monitoring using our free trial now.

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.

Stay up to date with all things Laravel, PHP, and JavaScript.

You can follow me on these platforms:

On all these platforms, regularly share programming tips, and what I myself have learned in ongoing projects.

Every month I send out a newsletter containing lots of interesting stuff for the modern PHP developer.

Expect quick tips & tricks, interesting tutorials, opinions and packages. Because I work with Laravel every day there is an emphasis on that framework.

Rest assured that I will only use your email address to send you the newsletter and will not use it for any other purposes.

Comments

What are your thoughts on "Using emoji in PHP"?

Comments powered by Laravel Comments
Want to join the conversation? Log in or create an account to post a comment.