Let's take a look at how null is returned.

Source: Internet
Author: User
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;
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.