ord()
Our article is about the PHP function ord(), which is used to return the ASCII value of the first character of a string. This function is useful for working
The PHP ord() function returns the byte value (0–255) of the first character of a string. For plain ASCII text this is the character's ASCII code — for example, 'A' becomes 65. It is the inverse of chr(), which turns a number back into a character.
This page covers the syntax of ord(), how it behaves with the strings you will actually pass it, the multibyte gotcha that trips most people up, and practical patterns where converting a character to its numeric code is genuinely useful.
Syntax
ord(string $character): intord() takes a single argument, the string to inspect, and returns an integer. Only the first character is examined — the rest of the string is ignored.
| Parameter | Description |
|---|---|
$character | The string whose first character (byte) you want the numeric value of. |
Basic example
Output:
7272 is the code for "H", the first character of "Hello". Because only the first character matters, ord('ABC') and ord('A') both return 65.
Common return values
These are the codes you will meet most often when working with text:
<?php
echo ord('A'), "\n"; // 65 — start of uppercase letters
echo ord('a'), "\n"; // 97 — start of lowercase letters
echo ord('0'), "\n"; // 48 — start of digit characters
echo ord(' '), "\n"; // 32 — space
echo ord("\n"), "\n"; // 10 — newline (line feed)
echo ord(''), "\n"; // 0 — empty string yields 0
?>Output:
65
97
48
32
10
0Note the last line: passing an empty string does not raise an error — ord('') simply returns 0.
The multibyte gotcha
ord() works on bytes, not on Unicode characters. A character outside the basic ASCII range (such as é, €, or any emoji) is stored as several bytes in UTF-8, and ord() returns only the value of the first byte:
<?php
echo ord('é'); // 195, not the Unicode code point 233
?>Output:
195If you need the real Unicode code point of a multibyte character, use mb_ord() (from the mbstring extension) instead:
<?php
echo mb_ord('é', 'UTF-8'); // 233
?>Why you would use ord()
Converting a character to a number lets you do arithmetic and comparisons that are awkward on the character itself.
Change case manually. Uppercase and lowercase letters sit exactly 32 apart in ASCII, so you can switch case by adding or subtracting 32 with ord() and chr():
<?php
$char = 'A';
echo chr(ord($char) + 32); // a
?>Output:
aInspect each character in a string. Loop with strlen() and index into the string to see the code of every character — handy for debugging encoding issues or building a simple cipher:
<?php
$s = 'PHP';
for ($i = 0; $i < strlen($s); $i++) {
echo $s[$i] . '=' . ord($s[$i]) . ' ';
}
?>Output:
P=80 H=72 P=80 Validate input. Because letters and digits occupy known ranges, you can test a character's category with a numeric comparison, for example ord($c) >= 48 && ord($c) <= 57 to check that $c is a digit.
Related functions
chr()— the inverse: returns the character for a given ASCII/byte value.strlen()— the number of bytes in a string, useful for looping over characters.str_split()— split a string into an array of characters.substr()— extract part of a string.bin2hex()— view the raw bytes of a string as hexadecimal.