Home  >  Article  >  Backend Development  >  PHP string flip example code_PHP tutorial

PHP string flip example code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:45:481049browse

Character flipping is a piece of cake for PHP to process. PHP's string function strrev can handle it. For example:

echo strrev("Hello World!"); //The output result is "!dlroW olleH"

But sometimes during interviews we often need to write a function ourselves to achieve the same effect as strrev. In fact, this is not difficult, for example:

/**

* Function to implement string flipping

* @param string $str The string to be processed

* @return string The successfully flipped string

​*/

function reverse($str){

If($str == ''){

        return null;

}

If(strlen($str) == 1){

         return $str;

}else{

          $string = "";

for($i=1;$i<=strlen($str);$i++){

                $string .=substr($str,-$i,1);

}

          return $string;

}

}

echo reverse("Hello World!"); //The output result is "!dlroW olleH"

Excerpted from: Shine’s Holy Paradise

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478625.htmlTechArticle Character flipping is a piece of cake for PHP processing. PHP’s string function strrev can handle it. For example : echo strrev(Hello World!); //The output result is !dlroW olleH But sometimes interviews...
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