反转字符串是最基本的字符串操作之一,开发人员和程序员经常使用它。PHP带有内置函数来反转字符串。
所述strrev()函数是在PHP内置功能和用于反向的字符串。此函数不会对作为参数传递给它的原始字符串进行任何更改。
Synatx:
1 | string strrev($ inpString) |
参数:此函数接受单个参数$ inpString。此参数是一个字符串,它指定我们要反转的字符串。如果将数字传递给它而不是字符串,它也将反转该数字。
返回值: strrev()函数返回反转的字符串或数字。它不会对参数中传递的原始字符串或数字进行任何更改。
例子:
1 2 3 4 5 | <span>输入:$ string =“geeks” </span><span></span><span> 输出:skeeg </span><span></span> <span></span><span> 输入:$ string = 143 </span><span></span><span> 输出:341 </span><span></span> |
下面的程序说明了PHP中的strrev()函数:
程序1: PHP程序,用于在传递字符串时演示strrev()函数。
1 2 3 4 5 6 7 8 9 | <?php // PHP program to demonstrate the // strrev() function when a string is passed $str = "geeks"; // prints the reversed string echo strrev($str); ?> |
输出:
1 2 | <span>skeeg </span> |
程序2: PHP程序,用于在传递数字时演示strrev()函数。
1 2 3 4 5 6 7 8 9 10 | <?php // PHP program to demonstrate the // strrev() function when a number is passed // passing number instead of string $num = 134; // prints the reversed number echo strrev($num); ?> |
输出:
1 2 | <span>431 </span> |