📜  PHP | ord()函数(1)

📅  最后修改于: 2023-12-03 15:18:24.915000             🧑  作者: Mango

PHP | ord()函数

简介

ord()函数用于获取指定字符串中第一个字符的 ASCII 值。

语法
ord(string $string): int
参数
  • string: 必需。 指定字符串。
返回值

一个整数类型的 ASCII 值,如果字符串为空,返回 0。

示例代码
<?php
    $str = "Hello World!";
    $ascii_value = ord($str[0]);
    echo "The ASCII value of the first character of the string is: ".$ascii_value;
?>
输出
The ASCII value of the first character of the string is: 72
特别说明
  • ord() 函数只能处理串的第一个字符,如果尝试传递一个多字符字符串来处理,则 ord() 只会返回字符串的第一个字符的 ASCII 值。

  • 如果输入的字符串是一个空串,则返回 0。

总结

ord() 函数方便开发人员快速获得字符串中的第一个字符的 ASCII 值。它适用于需要处理 ASCII 字符串的情形,例如字符串加密和解密,字符校验等。