Home Backend Development PHP Tutorial php的chr和ord函数实现字符加减乘除运算实现代码_php技巧

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

May 17, 2016 am 09:14 AM
Addition, subtraction, multiplication and division

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
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles