Introduction
String manipulation is one of the most important topics in PHP and programming in general. Among the most common string operations is reversing a string, which is frequently asked in interviews and used in real-world applications.
PHP provides a built-in function strrev() to reverse strings easily. However, in many situations—especially in interviews—you may be asked to reverse a string without using any predefined function.
What is String Reverse?
String reverse means reversing the order of characters in a string.
Example:
Input: "John"
Output: "nhoJ"
Each character is rearranged from last to first.
Syntax (General Logic)
To reverse a string without using built-in functions:
- Start from the last character
- Loop backward
- Print or store each character
Example
Example 1: Reverse a String (Character by Character)
<?php
$string = "John";
$length = strlen($string);
for ($i = $length - 1; $i >= 0; $i--) {
echo $string[$i];
}
?>
Output:
Explanation:
- strlen() finds the string length
- The loop starts from the last index
- Prints characters in reverse order
Example 2: Using Array Approach
<?php
$string = "John";
$arr_str = str_split($string);
$length = count($arr_str);
for ($i = $length - 1; $i >= 0; $i--) {
echo $arr_str[$i];
}
?>
Example 3: Reverse Words Individually
Input: “John working in TCS”
Output: “nhoJ gnikrow ni SCT”
<?php
$string = "John working in TCS";
$words = explode(" ", $string);
foreach ($words as $word) {
$length = strlen($word);
for ($i = $length - 1; $i >= 0; $i--) {
echo $word[$i];
}
echo " ";
}
?>
Output:
Example 5: Store Reversed String in Variable
<?php
$string = "Hello";
$reverse = "";
for ($i = strlen($string) - 1; $i >= 0; $i--) {
$reverse .= $string[$i];
}
echo $reverse;
?>
Real-Life Example
Scenario 1 : Palindrome Check
<?php
$string = "madam";
$reverse = "";
for ($i = strlen($string) - 1; $i >= 0; $i--) {
$reverse .= $string[$i];
}
if ($string == $reverse) {
echo "Palindrome";
} else {
echo "Not Palindrome";
}
?>
Why this is useful:
- Used in validation systems
- Common in coding tests
- Helps understand string comparison
Scenario 2: Password Masking Logic
<?php
$password = "abc123";
$masked = "";
for ($i = strlen($password) - 1; $i >= 0; $i--) {
$masked .= "*";
}
echo $masked;
?>
Note: Demonstrates string manipulation techniques.
Common Mistakes
1. Using Wrong Loop Condition
for ($i = 0; $i < strlen($string); $i--)
Causes an infinite loop.
2. Forgetting String Indexing
echo $string;
Note: Must use $string[$i].
3. Not Initializing Variable
$reverse;
Note: Should initialize with an empty string.
4. Using Built-in Function
❌ Avoid:
strrev($string);
Note: Not allowed in interview-based questions.
5. Incorrect Word Splitting
explode("", $string);
Note: Should use ” ” for words.
Conclusion
Reversing a string without using built-in functions is a fundamental programming exercise that helps you understand loops, indexing, and string manipulation deeply. It is widely used in interviews and real-world applications.