Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Providing A String That Is Not One Byte Long To ord() Is Deprecated

The ord() function returns the ordinal value of the first byte of a string. When the provided string is longer than one byte, only the first byte is used and the remaining bytes are silently discarded. This implicit behavior is a source of confusion, especially when working with multi-byte encodings such as UTF-8, where a single character may span several bytes. Since PHP 8.5, calling ord() with a string that is not exactly one byte long is deprecated: use the $str[0] syntax to extract the first byte explicitly before passing it to ord().

PHP code

<?php

$str = '我';

echo ord($str);

?>

Before

230

After

PHP Deprecated:  Providing a string that is not one byte long is deprecated. Use ord($str[0]) instead in /codes/ordNonSingleByteString.php on line 5

Deprecated: Providing a string that is not one byte long is deprecated. Use ord($str[0]) instead in /codes/ordNonSingleByteString.php on line 5
230

PHP version change

This behavior was deprecated in 8.5.

This behavior changed in 8.5.

Error Messages

Extension

© 2023-2026, Damien Seguy - Exakat