函数内的字符串

I have Written a program in which you have to create a function. The function will expect 3 parameters $string, $start, $end. Inside the function, you have to write the logic inside the function to automatically print the string depending upon the value of $start and $end.

I tried this Whole function is working correctly but the array length is 55 but when i specify $end=55 then it returns the error Ending Value Exceeded String length:

function string_function($string,$start,$end){

    $length= strlen($string);

    if($start<0 || $end<0){
        echo "Start or end of string cannot be in negative";    
    }else if($start>$length || $end>$length){
        echo "Entered values exceed more than String's Length";
    }else if($start>$end){
        echo "Start point is greater than end point, Which is Invalid";
    }else if($end<$length){
        $length <= ($end+1);
        for($i=$start; $i<=$end; $i++){
            echo "[$i] => ".$string[$i]."<br>";
        }
        }else{

            echo "Ending Value Exceeded String length";
    }
}
$start = 22;
$end =54;
$string = "I am a programmer and i am passionate about programming";
string_function($string, $start, $end);

Help would be welcomed.

Change your else if part to:

else if($end<=$length){
    $length <= ($end+1);
    for($i=$start; $i<$end; $i++){
        echo "[$i] => ".$string[$i]."<br>";
    }
}

Since your array starts from 0th index, so the end that you specify is length of the array. But the maximum array index is 1 less than the total length. Your iteration should therefore be 1 less than your end variable

[As discussed in comments]

You just have to change '<' to '<=' when you compare $end and $lenght.