1.2.877. Only First Byte

When assigning a char to a string with an array notation, only the first byte is used.

<?php
    $str = 'xy';

    // first letter is now a
    $str[0] = 'a';

    // second letter is now b, c is ignored
    $str[1] = 'bc';
?>

See also String access and modification by character.

1.2.877.1. Suggestions

  • Remove extra bytes when assigning to a string

  • Use concatenation

  • Use strpos() and substr() functions

  • Use explode(), implode() functions and array manipulations

1.2.877.2. Specs

Short name

Structures/OnlyFirstByte

Rulesets

All, Analyze, Changed Behavior

Exakat since

2.2.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Changed Behavior

PHP 8.0 - More

Precision

High

Available in

Entreprise Edition, Exakat Cloud