PHP ord()

noahpgordon's avatar
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.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours

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:

<?php
echo 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.

Code
Output

All contributors

Contribute to Docs

Learn PHP on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours