Home > Backend Development > PHP Tutorial > Usage of chr (ascii) and ord (string) functions in php_PHP tutorial

Usage of chr (ascii) and ord (string) functions in php_PHP tutorial

PHPz
Release: 2018-11-15 09:07:54
Original
1836 people have browsed it

The functions of these two functions are exactly opposite. The chr function returns the character from the specified ASCII value and the ord() function returns the ASCII value of the first character of the string. If you understand this, you will be able to use this function.

Look at the chr function first

chr() function returns characters from the specified ASCII value.

chr(ascii)

ascii argument can be decimal, octal or hexadecimal. Specify octal by leading 0, specify hexadecimal by leading 0x

Example

The code is as follows Copy code
 代码如下 复制代码

echo chr(52);
echo chr(052);
echo chr(0x52);
?>输出:

4
*
R

echo chr(52);

echo chr (052);
代码如下复制代码
echo chr(13);
echo chr(32);
?>
echo chr(0x52);

?>Output:

4

*

R Isn’t it amazing? In fact, it’s not surprising that I often use chr to operate some invisible codes, such as

Think about this What will be output? The result is


a carriage return and a space

Let’s look at the

ord function
 代码如下 复制代码

ord(string)
例子
echo ord("h");
echo ord("hello");
?>

输出结果:

104
104

ord() function returns the string The ASCII value of a character.

 代码如下 复制代码
$str1=chr(88);
echo $str1; //返回值为X
$str2=chr(ord(X)+1); //
echo $str2; //返回值为 Y
echo "t";
$str3=ord('S');
echo $str3; //返回值为83
?>
From the above point of view, it is exactly the opposite of chr, right?

Syntax

The code is as follows Copy code
ord(string)

Example

echo ord("h");

echo ord("hello");
?>

Output result:

104
104

td>

Okay, now let’s look at a comprehensive example
The code is as follows Copy code
$str1=chr(88); <🎜>echo $str1 ; //The return value is str3=ord('S'); <🎜>echo $str3; //The return value is 83 <🎜>?>
The following is an article about the php ord function and the solution to Chinese garbled charactersFor more details, please check: http://www.bKjia.c0m/ phper/php-function/php-ord.htm http://www.bkjia.com/PHPjc/631668.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/631668.htmlTechArticleThe two functions In contrast, the chr function returns the character from the specified ASCII value and the ord() function returns the ASCII value of the first character of the string. If you understand this, you will be able to use this function. ...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template