ord
CONVERT the first byte of a string to a value between 0 and 255.
This function interprets the binary value of the first byte of $string 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.
<?php
int ord ( str $string )
where,
$string = A character
?>
$string
The STRING to get the length.
EXERCISE
<?php
$strord01 = 'I';
$ord01 = ord($strord01);
echo $ord01;
?>
RESULT
The ord character of "I" is 73.
EXERCISE
<?php
$strord02 = 'Independece';
$ord02 = ord($strord02);
echo $ord02;
?>
EXERCISE
<?php
$strord03 = 'Ã';
$ord03 = ord($strord03);
echo $ord03;
?>
EXERCISE
<?php
echo "Testing ord() : basic functionality:<br><br><br>";
var_dump(ord("a"));
echo '<br><br>';
var_dump(ord("z"));
echo '<br><br>';
var_dump(ord("0"));
echo '<br><br>';
var_dump(ord("9"));
echo '<br><br>';
var_dump(ord("!"));
echo '<br><br>';
var_dump(ord("*"));
echo '<br><br>';
var_dump(ord("@"));
echo '<br><br>';
var_dump(ord("\n"));
echo '<br><br>';
var_dump(ord("\x0A"));
echo '<br><br>';
var_dump(ord("\xFF"));
echo '<br><br>';
var_dump(ord("Hello"));
// Make sure all valid ascii chars round trip
for ($i = 0; $i < 255; $i++) {
if (ord(chr($i)) != $i) {
exit("TEST FAILED: $i does not round trip<br>");
}
}