Home > php教程 > php手册 > body text

php的chr和ord函数实现字符加减乘除运算实现代码

WBOY
Release: 2016-06-06 20:39:31
Original
1515 people have browsed it

这两个函数到底有什么用呢? 用来做字符加减运算最合适了. 普通的字符是无法做加减运算指向下一个字符的. 而转成ASCII后就可以做加减乘除了. 处理好后再转成字符就可以了. 目前的很多字符串加密,解密都用到此功能!

chr函数用于将ASCII码转换为字符
ord函数是用来字符转换为ASCII码

ASCII码是计算机所能显示字符的编码,它的取值范围是0-255,其中包括标点、字母、数字、汉字等。在编程过程中,经常把指定的字符转化为ASCII码进行比较。

下面是PHP提供的转换ASCII码和字符的函数。
1.chr()函数
该函数用于将ASCII码值转化为字符串。其函数声明如下:
string chr (int ascii);
2.ord()函数
该函数用于将字符串转化为ASCII码值。其函数声明如下:
int ord(string str);
例子:
使用chr()函数和ord()函数进行字符串与ASCII码之间的转换运算,程序代码如下:
代码如下:
$str1=chr(88);
echo $str1; //返回值为X
$str2=chr(ord(X)+1); //
echo $str2; //返回值为 Y
echo "\t";
$str3=ord('S');
echo $str3; //返回值为83
?>

运行结果:X Y 83
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!