PHP ord() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, ord() returns the ASCII value of the first character of a string.

It is the opposite of chr(), which turns a number back into a character.

Syntax

Call ord() with the values you need. Start with this line.

PHP
$code = ord("A");
CallWhat it does
ord("A")Returns 65.
ord("a")Returns 97.
ord("abc")Returns 97. Only the first character is read.

ord() reads a single byte. For multibyte UTF-8 characters, use mb_ord().

Library declarations
PHP
ord(string $character): int

Parameters

ParameterDescription
characterRequired. The input string. Only its first byte is used.

Return Value

Return ValueDescription
intThe value of the first byte, from 0 to 255.

PHP Examples

Example 1 — Get Character Codes

Print the code for a few common characters.

PHP
<?php
echo ord("A"), PHP_EOL;
echo ord("a"), PHP_EOL;
echo ord("0"), PHP_EOL;

How It Works

  1. Pass one letter. Capital A has the code 65.
  2. Lowercase differs. a is 97, which is 32 higher.
  3. Digits have codes too. The character 0 is 48, not the number 0.
  4. Read the result. ord always returns an integer.

Example 2 — Empty, Long, and Multibyte Strings

Check how ord() handles input that is not a single ASCII letter.

PHP
<?php
echo ord(""), PHP_EOL;
echo ord("abc"), PHP_EOL;
echo ord("€"), PHP_EOL;

How It Works

  1. Empty string. There is no byte, so the result is 0.
  2. Longer string. Only the first character a is used.
  3. Euro sign. It is 3 bytes in UTF-8, and 226 is the first byte.
  4. For the true code point. Switch to mb_ord().

Example 3 — Shift Each Letter by One

Combine ord() and chr() to build a tiny cipher.

PHP
<?php
$word = "HAL";
$shifted = "";

for ($i = 0; $i < strlen($word); $i++) {
    $shifted .= chr(ord($word[$i]) + 1);
}

echo $shifted, PHP_EOL;

How It Works

  1. Loop over the letters. $word[$i] reads one character.
  2. Get the code. ord turns the letter into a number.
  3. Add one and convert back. chr makes the next letter.
  4. Join the results. HAL becomes IBM.

Frequently Asked Questions

ord() returns the numeric value of the first byte of a string. For ASCII letters this is the ASCII code, such as 65 for "A".
int ord(string $character). It takes one string and returns an integer from 0 to 255.
Use chr(). For example, chr(65) returns "A".
Only partly. It returns the first byte. Use mb_ord() to get the full Unicode code point.
It returns 0.

Next: parse_str()

parse_str() turns a URL query string into an array.

parse_str() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful