What is PHP ord() Function?
If you want to get decimal equivalent of the first byte in a string, use ord() function. Example 1, 65, 127 etc. But, the decimal value is limited between 0 to 255. As PHP’s standard string functions like ord() treat each character as 1 byte, so, in other words, you can say that this function returns decimal equivalent of the first character of a string.
Now, you can ask why the function only returns value 0 – 255 (total 256)? The reason is with 2 bits (0 and 1), a computer can create maximum 256 different patterns (2 to the power of 8) in a byte. To identify these patterns as readable characters, we can use a single byte character encoding system like ASCII (American Standard Code for Information Interchange). Each of 0-255 integers is a code position/code point for each pattern and each pattern is associated with one character that you are acquainted with. The code positions from 32 to 126 represents printable characters (letters, numbers, symbols, and punctuation marks).
The table below shows a list of code points of the letters and numbers of ASCII encoding. The code point 65 indicates “A” whose binary pattern is 01000001. Note, the code point is also the decimal equivalent of the associated binary pattern. So, if the first character of the string contains “A”, the function returns 65.
Code point | Symbol | Binary |
48 | 0 | 00110000 |
… | … | … |
57 | 9 | 00111001 |
65 | A | 01000001 |
… | … | … |
90 | Z | 01011010 |
97 | a | 01100001 |
… | … | … |
122 | z | 01111010 |
Syntax:
ord(character)
Parameters:
The Function has 1 parameter which is required-
Parameter (Required): The string whose first byte (or character) to be used to get its decimal equivalent.
Return Values:
The function returns an integer between 0 to 255.
Examples:
Example 1: Simple ord() function –
<?php
echo "The value of the first byte of php is: " . ord("php");
echo "<br />";
echo "The value of the first byte of 49 is: " . ord("49");
?>
Output:
The value of the first byte of php is: 112
The value of the first byte of 49 is: 52
Explanation:
The function converts the first byte of the string “php” which is also “p” here to 112. Similarly, converts the first byte of “49” which is also “4” to 52.
Example 2: Finding value of each byte of a string-
<?php // #2
$arr = mb_str_split("php");
for($i=0; $i<count($arr); $i++){
echo "The value of the byte " . $arr[$i]. " is: " . ord($arr[$i]) . "<br />";
}
?>
Output:
The value of the byte p is: 112
The value of the byte h is: 104
The value of the byte p is: 112
Explanation:
The mb_str_split() function splits “php” into separate characters and create an array. So, each element contains one character and the ord() function converts each character to its equivalent ASCII code point.
Example 3: Finding missing digits-
<?php // #3
$arr = mb_str_split("1245689");
for($i=0; $i<count($arr); $i++){
$numbers[] = ord($arr[$i]);
}
$missingNumbers = "";
for ($i=48; $i<=57; $i++){
if(!in_array($i, $numbers)){
$missingNumbers .= chr($i) . " ";
}
}
echo $missingNumbers;
?>
Output:
0 3 7
Explanation:
Line 2: The mb_str_split() function splits the numeric string “1245689” into separate digit and create an array with each digit.
Line 3: The loop stores the code points of the digits in the $number array.
Line 7: Code point 48 to 57 is for 10 digits (0, 1,…..9).
Line 8: If any code point digit doesn’t match with the code points stored in the array, we add this to the variable $missingNumbers in Line 10. We used chr() function to convert the code point back to numbers.
Example 4: Retrieving numbers from string –
<?php
$arr = mb_str_split("1PHP2PYHTION3RUBY4JAVA1245689");
$multiByteChars = "";
for($i=0; $i<count($arr); $i++){
if( ord($arr[$i])<65 OR ord($arr[$i])>90 ){
$multiByteChars .= $arr[$i];
}
}
echo $multiByteChars;
?>
Output:
12341245689
Explanation:
Line 2: The mb_str_split() function splits the numeric string “1245689” into separate digit and create an array with each digit. Line 5: As the string only contains uppercase characters and number and code points of uppercases are from 65 to 90, we check for code point for any character that is out of this range.
Practical Usages of ord() Function:
There are lots of great usages of this function-
- Missing digits.
- retrieving numbers from a string.
- Sorting characters.
Best practices on ord() Function:
As the function only check the first byte of the string, make sure that it is the part of the intendent string not any other unnecessary characters like white space. So, always sanitize and validate user input at first before running this function.
Notes on ord() Function:
- the function is not aware of any encoding system like ASCII, UTF-8 etc. As it considers only the first byte and single byte encoding system also based on 1 byte, so we use ASCII encoding to get the integer between 0 and 255 quickly.
- The opposite of ord() function is chr() function.
Caution:
- If you try to use a string that uses a multi-byte encoding, you won’t get a correct result. Check the code below. It returns 224 which is not true as Arabic letter is not included in ASCII encoding system and also the Arabic character is 3-byte long. In this case, use mb_ord() function.
<?php echo "The value of the first byte of ࢳ is: " . ord("ࢳ"); ?>
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP ord() Function
PHP ord() function seems simple and useless at first, but, if can apply it effectively, it is a very useful built-in string function for character manipulation, validation etc.