Home > Article > Backend Development > How to reverse string and array in php
In PHP, you can use the strrev() and array_reverse() functions to achieve string reversal and array reversal. strrev() is used to reverse a string, the syntax is "strrev($string)". array_reverse() is used to reverse an array, the syntax is "array_reverse($array,$preserve)"; the parameter "$preserve" can be omitted, the default value is "false", if the value is set to TRUE, the numeric keys will be retained.
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
php has two built-in functions. Implement string reversal and array reversal
strrev() function can reverse string
strrev($string) Function to reverse string
$string .
<?php header("Content-type:text/html;charset=utf-8"); $str="Hello World!"; echo "原字符串:".$str."<br>"; echo "反转字符串:".strrev($str); ?>
array_reverse() can achieve array reversal
The array_reverse() function can reverse an array, returning the array in reverse order. Syntax format:array_reverse($array,$preserve)
Description | |
---|---|
Required. Specifies an array. | |
Optional. Specifies whether to retain the original array key names. | If set to TRUE, numeric keys will be preserved. Non-numeric keys are not affected by this setting and will always be retained.Possible values: true
|
Recommended learning: "PHP Video Tutorial
"The above is the detailed content of How to reverse string and array in php. For more information, please follow other related articles on the PHP Chinese website!