Help me to see how the returned null Post was last edited by snowlove in 2013-06-1718: 27: 34 & lt ;? Phpfunction & nbsp; newtripos ($ str, $ findstr, $ count, $ off = 0) {$ pos = stripos ($ str help me see how to return null
This post was last edited by snowlove at 18:27:34
function newtripos($str,$findstr,$count,$off=0){
$pos=stripos($str,$findstr,$off);
$count--;
if($count>0 && $pos!=false){
$pos=newtripos($str,$findstr,$count,$pos+1);
}else{
var_dump($pos);
return $pos;
}
}
$a="456123456455654466";
$b=newtripos($a,'6',4);
var_dump($b);
?>
After Execution, the value of $ B is null. The var_dump ($ pos) executed before the function returns is int (16 ).
Why would var_dump ($ B) be null? Share:
------ Solution --------------------
You use recursion, return $ pos; after recursion, it will act on $ pos = newtripos ($ str, $ findstr, $ count, $ pos + 1 );
You didn't process $ pos in this branch (that is, return the result to the upper-level recursion)
I may not even understand what I said. check the code:
function newtripos($str,$findstr,$count,$off=0){
$pos=stripos($str,$findstr,$off);
$count--;
if($count>0 && $pos!=false){
$pos=newtripos($str,$findstr,$count,$pos+1);
}
return $pos;
}
------ Solution --------------------
Most of the differences, because when else is not removed, it is equivalent
if($count>0 && $pos!=false){
$pos=newtripos($str,$findstr,$count,$pos+1);
return null;
}else{
var_dump($pos);
return $pos;
}
}
?>
After else is removed
if($count>0 && $pos!=false){
$pos=newtripos($str,$findstr,$count,$pos+1);
return $pos;
}else{
var_dump($pos);
return $pos;
}
}
?>