← Back to Chapters

PHP strrev() & str_shuffle()

? PHP strrev() & str_shuffle()

? Quick Overview

PHP provides useful string manipulation functions like strrev() and str_shuffle() that allow you to reverse a string and shuffle its characters. These functions are commonly used in password generation, text transformations, and learning string behavior.

?️ Key Concepts

  • strrev() reverses the entire string character by character.
  • str_shuffle() randomly rearranges all characters.

? Syntax / Theory

  • strrev(string) → returns reversed string
  • str_shuffle(string) → returns shuffled string

? Code Example — strrev()

? View Code Example
// Reverse a string using strrev()
<?php
$string = "Hello";
$reversed = strrev($string);
echo $reversed;
?>

? Output

olleH

? Code Example — str_shuffle()

? View Code Example
// Shuffle characters randomly using str_shuffle()
<?php
$string = "Hello";
$shuffled = str_shuffle($string);
echo $shuffled;
?>

? Output

Random output such as olHle or lHoel

? Interactive Visual Example

Hello olleH Visualizing string reversal

? Use Cases

  • Password and token randomization
  • Palindrome checking
  • Text obfuscation
  • Learning string manipulation logic

✅ Tips & Best Practices

  • strrev() reverses everything including spaces and symbols.
  • str_shuffle() is non-deterministic; results vary each run.
  • Do not rely on str_shuffle() for cryptographic security.

? Try It Yourself

  • Reverse a sentence entered by the user.
  • Shuffle a word multiple times in a loop.
  • Combine both functions in a single script.