php - String inside function -
i have written program in have create function. function expect 3 parameters $string, $start, $end. inside function, have write logic inside function automatically print string depending upon value of $start , $end.
i tried whole function working correctly array length 55 when specify $end=55 returns 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 in negative"; }else if($start>$length || $end>$length){ echo "entered values exceed more string's length"; }else if($start>$end){ echo "start point greater end point, 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 programmer , passionate programming"; string_function($string, $start, $end);
help welcomed.
change else if part to:
else if($end<=$length){ $length <= ($end+1); for($i=$start; $i<$end; $i++){ echo "[$i] => ".$string[$i]."<br>"; } }
since array starts 0th index, end specify length of array. maximum array index 1 less total length. iteration should therefore 1 less end
variable
[as discussed in comments]
Comments
Post a Comment