Unicode codepoint escape syntax in PHP 7

PHPServer Side ProgrammingProgramming

PHP 7 takes a Unicode codepoint in hexadecimal form as input and produces the outputs in UTF-8 character format within a double-quoted string. It could be any combination of hexadecimal digits, 2, 4, 6, and above. We can write Unicode characters by using a double-quoted or here docstring, without calling a function. Leading zero is optional in any hexadecimal form.

<html>
<head>
<title>UTF-8 Character </title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
</html>

Note: We can construct the full Unicode characters using “\u{xxx}” syntax.

Some languages, for example, Hebrew and Arabic are read from right to left instead of left to right. We can reverse the text using the U+202E Unicode character for the right to left override.

Example

Live Demo

<?php
   echo "\u{202E} show reversed text";
   echo "\u{202D}";
?>

Output

It will show the text in reverse form like

txet desrever wohs

Example: Unicode codepoint escape syntax

Live Demo

<html>
<head>
<title>“Tutorialpoint : Unicode codepoint escape syntax:”;</title>
</head>
<body>
<?php
   echo "\u{aaa}";
   echo "\u{0000aaa}";
   echo "\u{9999}";
?>
</body>
</html>

Output

The output for the above program will be

પપ香

Character escaping is now much easier in PHP 7 than before. Including the Unicode within a string without any difficulty.

Example

Live Demo

<?php
   echo 'You owe me £500.';
?>

Output

You owe me £500.

In the above example, Standard A to Z and 0 to 9 characters are used. It also holds a special character the Pound symbol (£). In the old version of PHP, it was necessary to escape these types of characters, or else, it would produce its character code 163 within the string. So, the output could look like 163300 or something to that effect.In the older versions of PHP, the Pound symbol had to escape, otherwise, there would have been an issue.

raja
Published on 13-Mar-2021 11:39:03
Advertisements