Providing a string that is not one byte long is deprecated. Use ord($str[0]) instead

Description

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. To make the intent explicit, use the $str[0] syntax to extract the first byte before passing it to ord().

Example

<?php

    $str = '我';

    echo ord($str);

?>

Solutions

  • Use the $str[0] rather than the whole string.

  • Use substr($str, 0, 1) on the string, as long as the string is not multi-byte.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.