array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'en', ), 'this' => array ( 0 => 'function.ord.php', 1 => 'ord', ), 'up' => array ( 0 => 'ref.strings.php', 1 => 'String Functions', ), 'prev' => array ( 0 => 'function.number-format.php', 1 => 'number_format', ), 'next' => array ( 0 => 'function.parse-str.php', 1 => 'parse_str', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/strings/functions/ord.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4, PHP 5, PHP 7, PHP 8)
ord — Convert the first byte of a string to a value between 0 and 255
Interprets the binary value of the first byte of
character
as an unsigned integer between 0 and 255.
If the string is in a single-byte encoding, such as ASCII, ISO-8859, or Windows 1252, this is equivalent to returning the position of a character in the character set's mapping table. However, note that this function is not aware of any string encoding, and in particular will never identify a Unicode code point in a multi-byte encoding such as UTF-8 or UTF-16.
This function complements chr().
character
A character.
An integer between 0 and 255.
Example #1 ord() example
<?php
$str = "\n";
if (ord($str) == 10) {
echo "The first character of \$str is a line feed.\n";
}
?>
Example #2 Examining the individual bytes of a UTF-8 string
<?php
declare(encoding='UTF-8');
$str = "🐘";
for ( $pos=0; $pos < strlen($str); $pos ++ ) {
$byte = substr($str, $pos);
echo 'Byte ' . $pos . ' of $str has value ' . ord($byte) . PHP_EOL;
}
?>
The above example will output: