Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For循环中的PHP错误_Php - Fatal编程技术网

For循环中的PHP错误

For循环中的PHP错误,php,Php,代码工作正常,但我收到以下错误: 注意:未初始化的字符串偏移量:第8行的/Applications/XAMPP/xamppfiles/htdocs/kwame.php中的3 有人能解释为什么会这样吗。多谢各位 <? $string ='Lt4'; $getl = strlen($string); for($i=0; $i<=$getl; $i++){ echo $string[$i]; } ?> 因为字符串没有那么多索引!索引以0开头。只需更改,因为字符串没有那么多索

代码工作正常,但我收到以下错误:

注意:未初始化的字符串偏移量:第8行的/Applications/XAMPP/xamppfiles/htdocs/kwame.php中的3

有人能解释为什么会这样吗。多谢各位

<?

$string ='Lt4';
$getl = strlen($string);

for($i=0; $i<=$getl; $i++){

echo $string[$i];
}

?>

因为字符串没有那么多索引!索引以0开头。只需更改
,因为字符串没有那么多索引!索引以0开头。只需更改

索引始终从0开始


索引始终从0开始

应该是
$iCharacter'L'在字符串中表示索引0,字符'4'表示索引2,但是for循环上升到索引3。应该是
$iCharacter'L'在字符串中表示索引0,字符'4'表示索引2,但是for循环上升到索引3。
<?php

    $string = "Lt4";
             //^^^-Index 2
             //||-Index 1
             //|-Index 0

    $getl = strlen($string);  //Length: 3

    for($i = 0; $i < $getl; $i++) {  //i -> 0, 1, 2 
        echo $string[$i];  //L, t, 4            
    }

?>
                  Variables            Condition                 Output 

              |  $i  |  $getl    |    $i < $getl  = ?     |    $string[$i] 
-----------------------------------------------------------------------------
Start:        |  0   |    3      |     
Iteration  1: |  0   |    3      |     0 < 3      = TRUE  |         L (Index 0)
Iteration  2: |  1   |    3      |     1 < 3      = TRUE  |         t (Index 1)
Iteration  3: |  2   |    3      |     2 < 3      = TRUE  |         4 (Index 2)
Iteration  4: |  3   |    3      |     3 < 3      = FALSE |      [OFFSET]
              |      |           |                        |
End           |  3   |    3      |                        |
              |      |           |                        |
Lt4
<?php
$string ='Lt4';
$getl = strlen($string);
for($i=0; $i<$getl; $i++){
echo $string[$i];
}
?>