Home  >  Article  >  Backend Development  >  How to implement string flip in php

How to implement string flip in php

王林
王林Original
2019-09-20 17:53:133380browse

How to implement string flip in php

String:$str = "abcdefg";

Directly use PHP’s built-in function strrev($str)

print_r(strrev($str));

Use the for loop method, 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 "
";

Use the for loop method, 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 "
";

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 "
"; 

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to implement string flip in php. For more information, please follow other related articles on the PHP Chinese website!

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