Published Sep 28, 2023
Contribute to Docs

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.



(ord) has only one parameter, $my_string. The first character of the value passed by string will be used to return the relevant integer.


The example below passes the character "e" into the ord() function:

echo ord("e");

This results in the output:


Codebyte Example

The code below is runnable, change the value of $your_string to see the associated value of various characters.


All contributors

Looking to contribute?

Learn PHP on Codecademy