Home > Backend Development > PHP Tutorial > Implement string flip in php

Implement string flip in php

高洛峰
Release: 2023-03-06 06:20:02
Original
1921 people have browsed it

String:$str = "abcdefg";

Method 1 (directly use PHP’s built-in function strrev($str))

print_r(strrev($str));

Use for loop, str_split($str)

$newArrOne = [];//初始化一个新的数组
 $newStrOne = '';//初始化一个新的字符串
 $newArrOne = str_split($str);
 $arrCount = count($newArrOne);
 for ($i=0; $i < $arrCount; $i++) {
 $newStrOne.=$newArrOne[$i];
 }
 echo "<pre class="brush:php;toolbar:false">";
 print_r($newStrOne);
 echo "
";
Copy after login

Use for loop, strlen($substr)

$newStrTwo = &#39;&#39;;//初始化一个新的字符串
 $arrCountTwo = strlen($str);
 for ($i=1; $i <= $arrCountTwo; $i++) {
 $newStrTwo.=substr($str, -$i, 1);
 }
 echo "<pre class="brush:php;toolbar:false">";
 print_r($newStrTwo)."\n";
 echo "
";
Copy after login

Use for loop method, strlen($substr)

$newStrThree = &#39;&#39;;//初始化一个新的字符串
$arrCountThree = strlen($str);
for ($i = $arrCountThree; $i>=0;$i--) {
 @$newStrThree.=$str[$i];
}
echo "<pre class="brush:php;toolbar:false">";
print_r($newStrThree)."\n";
echo "
"; 
Copy after login

For more articles related to string flipping in PHP, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template