PHP 8 reference · String Functions
PHP ord() Function
Return the value (0 to 255) of the first byte of a string, such as 65 for "A". The reverse of chr().
PHP 8 Since 4.0
Syntax
ord(string $character): int
Parameters
| Parameter | Description |
|---|---|
character |
A string; only its first byte is read. |
Return value
An integer from 0 to 255.
Examples
<?php
echo ord("A"), " ", ord("a"), " ", ord("0"), "\n";
Output
65 97 48
<?php
echo ord("é"), "\n"; // the first of two UTF-8 bytes
echo mb_ord("é"), "\n"; // the Unicode code point
Output
195 233
Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
In other languages
ord()· Pythonstring.charCodeAt()· JavaScriptstring.codePointAt()· JavaScript
Related
chr()· Return the one-byte character for a value from 0 to 255, such as "A" for 65. The reverse of ord().bin2hex()· Convert binary data to a hexadecimal string, two hex digits per byte. Often used to print random bytes as a token.- PHP Strings · tutorial