不推荐使用:不建议使用带有花括号的数组和字符串偏移量访问语法

问题描述 投票:0回答:1

我知道自PHP 7.4.0起不推荐使用此方法,它会生成警告,但我不知道如何更改它以表明它是正确的。

public function hex_lighter( $color, $factor = 30 ) {
            $color = str_replace( '#', '', $color );

            $base['R'] = hexdec( $color{0} . $color{1} );
            $base['G'] = hexdec( $color{2} . $color{3} );
            $base['B'] = hexdec( $color{4} . $color{5} );

            $color = '#';

            foreach ( $base as $k => $v ) {
                $amount      = 255 - $v;
                $amount      = $amount / 100;
                $amount      = round( $amount * $factor );
                $new_decimal = $v + $amount;

                $new_hex_component = dechex( $new_decimal );

                if ( strlen( $new_hex_component ) < 2 ) {
                    $new_hex_component = "0" . $new_hex_component;
                }

                $color .= $new_hex_component;
            }
php
1个回答
0
投票

只需将$color{0}替换为$color[0],这将起作用。为每个颜色变量执行此操作

© www.soinside.com 2019 - 2024. All rights reserved.