Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Unicode codepoint escape syntax in PHP 7
PHP 7 introduced Unicode codepoint escape syntax, allowing you to write Unicode characters directly in double-quoted strings using the \u{xxx} format. This syntax accepts hexadecimal codepoints of varying lengths (2, 4, 6, or more digits) and produces UTF-8 character output.
Syntax
The Unicode escape syntax follows this pattern −
"\u{hexadecimal_codepoint}"
Leading zeros are optional, so \u{aaa} and \u{0000aaa} produce the same result.
Basic Example
Here's how to use Unicode codepoints in PHP 7 −
<?php
echo "\u{aaa}"; // Gujarati letter PA
echo "\u{0000aaa}"; // Same character with leading zeros
echo "\u{9999}"; // CJK ideograph
?>
???
Right-to-Left Text Example
Some languages like Hebrew and Arabic read from right to left. You can use Unicode character U+202E for right-to-left override −
<?php
echo "\u{202E} show reversed text";
echo "\u{202D}"; // Left-to-right override (reset)
?>
txet desrever wohs
Currency Symbols
Unicode escape syntax makes it easy to include currency and special symbols −
<?php
echo "Price: \u{00A3}500"; // Pound symbol
echo "\nEuro: \u{20AC}100"; // Euro symbol
echo "\nYen: \u{00A5}1000"; // Yen symbol
?>
Price: £500 Euro: ?100 Yen: ¥1000
Key Points
- Only works in double-quoted strings and heredoc syntax
- Single-quoted strings do not support Unicode escapes
- Hexadecimal codepoints can be 2-6 digits or more
- Leading zeros are optional
- Improves readability compared to previous PHP versions
Conclusion
Unicode codepoint escape syntax in PHP 7 simplifies character encoding by allowing direct Unicode insertion in strings. This feature eliminates the need for complex escaping methods used in earlier PHP versions.
