= 0;$i--){$r.=substr("abcdefg",$i,1);}"."/> = 0;$i--){$r.=substr("abcdefg",$i,1);}".">
Home >Backend Development >PHP Problem >How to reverse string abcdefg in php
Reversal method: 1. Use the strrev() function, the syntax is "strrev("abcdefg")"; 2. Use the for statement and substr(), the syntax "for($i=string length- 1;$i>=0;$i--){$r.=substr("abcdefg",$i,1);}".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php reverse characters String "abcdefg"
Method 1: Use the string reversal function strrev()
PHP provides a large number of methods for processing strings Built-in functions, for example, if you want to reverse a string, you can use the strrev() function.
strrev() function reverses a string and returns the reversed string.
Example: Reverse the string "abcdefg"
<?php header('content-type:text/html;charset=utf-8'); $str="abcdefg"; echo "字符串反转前:".$str."<br>"; echo "字符串反转后:".strrev($str); ?>
##Method 2: Use for statement and substr() to splice
<?php header('content-type:text/html;charset=utf-8'); $str="abcdefg"; echo "字符串反转前:<br>"; var_dump($str); $result = ''; $len = strlen($str);//获取字符串长度 for ($i = $len-1; $i>=0; $i--){ $result.= substr($str,$i,1); } echo "字符串反转后:<br>"; var_dump($result); ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to reverse string abcdefg in php. For more information, please follow other related articles on the PHP Chinese website!