W3docs

Unicode character in PHP string

You can use Unicode characters in a PHP string by including the character directly in the string, or by using the \u escape sequence followed by the Unicode code point of the character in hexadecimal.

You can use Unicode characters in a PHP string by including the character directly in the string, or by using the \u escape sequence followed by the Unicode code point of the character in hexadecimal.

Here is an example of using a Unicode character directly in a string:

Example of using Unicode characters in a PHP string

$str = 'I ❤️ Unicode';

Here is an example of using the \u escape sequence to include a Unicode character in a string:

Example of using the "\u" escape sequence to include a Unicode character in a PHP string

<?php

$str = "\u{1F497}";  // This is the Unicode code point for the "Growing Heart" emoji

echo $str;

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Learn object oriented PHP</div>

Note that you may need to set the default character encoding to UTF-8 in order to use Unicode characters in your PHP strings. This can be done by including the following line at the top of your PHP script:

Example of setting the encoding of the script to UTF-8 in order to use a PHP string

ini_set('default_charset', 'UTF-8');

Additionally, ensure your PHP source file is saved with UTF-8 encoding. Note that the \u{...} escape sequence requires PHP 7.0 or higher.

You can also use the html_entity_decode function to convert HTML entities (such as &#x1F497;) to Unicode characters in a PHP string.