Home  >  Article  >  Backend Development  >  php lookandsay

php lookandsay

WBOY
WBOYOriginal
2016-08-10 08:48:421263browse
<?php
function lookandsay($str){
        $r = '';
        $m = $str[0];
        $n = 1;

        for($i=1; $i<strlen($str); $i++){
                if($str[$i] == $m){//如果这个字符与上个字符相同则数目加1
                        $n++;
                }else{
                        $r .= $n.$m;//反之,把数目和这个字符追加到返回值
                        $m = $str[$i];//赋值下一个字符
                        $n  = 1;
                }
        }
        return $r.$n.$m;
}

for($i = 0,$str = 1; $i < 9; $i++){
        $str = lookandsay($str);
        print $s."<br/>";
}
?>

The above introduces PHP lookandsay, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn