ord()
The ord()
function takes a string as its only parameter and returns an integer between 0 and 255 based on the first character in that string. This is the inverse of the chr()
function, which returns a single-byte string when passed an integer between 0 and 255.
The integer returned is the decimal equivalent of the binary representation of the character. For most common printable characters (such as letters and numbers), this binary representation conforms to standard single-byte encodings (ASCII, ISO-8859, Windows 1252). ord()
can therefore be used to return a character’s ASCII (or other single-byte encoding) value.
Syntax
ord($my_string)
(ord)
has only one parameter, $my_string
. The first character of the value passed by string
will be used to return the relevant integer.
Example
The example below passes the character "e"
into the ord()
function:
<?phpecho ord("e");?>
This results in the output:
101
Codebyte Example
The code below is runnable, change the value of $your_string
to see the associated value of various characters.
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.