Character flip is a piece of cake for php processing. php's string function strrev can be used for processing, for example, echostrrev (& quot; HelloWorld! & Quot;); // The output result is & quot ;! DlroWolleH & quot...
Character flip is a piece of cake for php processing. php's string function strrev can handle the following:
Echo strrev ("Hello World! "); // The output result is "! DlroW olleH"
However, sometimes during interviews, we often need to write a function to achieve the same effect as strrev. In fact, this is not difficult, for example:
/**
* String flip function
* @ Param string $ str the string to be processed
* @ Return string that is successfully flipped
*/
Function reverse ($ str ){
If ($ str = ''){
Return null;
}
If (strlen ($ str) = 1 ){
Return $ str;
} Else {
$ String = "";
For ($ I = 1; $ I <= strlen ($ str); $ I ++ ){
$ String. = substr ($ str,-$ I, 1 );
}
Return $ string;
}
}
Echo reverse ("Hello World! "); // The output result is "! DlroW olleH"
From: Shine's holy heaven