PHP String Functions
In this page:
strtolower($string);
strtoupper($string);
trim($string); // ltrim() / rtrim() for one side
strrev($string);
strlen($string);
Changing String Case
strtolower() and strtoupper() normalize a string's letter case in one call, which is essential for comparisons where Yes, yes, and YES should all be treated as the same user input rather than three different strings.
Example: Changing String Case
<?php
// Declare `$input`, set to "YES"
$input = "YES";
// Print `strtolower($input) . "\n"` to the output
echo strtolower($input) . "\n";
// Print `strtoupper("no")` to the output
echo strtoupper("no");
?>
Login to try C/C++/Java/PHP code in the editor
Trimming Whitespace
trim() removes whitespace from both ends of a string; ltrim() and rtrim() restrict that removal to just the left or right side respectively.
Trimming user-submitted text before storing or comparing it avoids bugs caused by an invisible stray space at the start or end.
Example: Trimming Whitespace
<?php
// Declare `$text`, set to " hello "
$text = " hello ";
// Print "[" . trim($text) . "]\n" to the output
echo "[" . trim($text) . "]\n";
// Print "[" . ltrim($text) . "]\n" to the output
echo "[" . ltrim($text) . "]\n";
// Print "[" . rtrim($text) . "]" to the output
echo "[" . rtrim($text) . "]";
?>
Login to try C/C++/Java/PHP code in the editor
Reversing Strings
strrev() reverses a string's character order completely — hello becomes olleh. Beyond palindrome checks and simple text puzzles, it's mostly a demonstration function rather than something reached for in everyday application code.
Example: Reversing Strings
<?php
// Print `strrev("hello")` to the output
echo strrev("hello");
?>
Login to try C/C++/Java/PHP code in the editor
Repeating and Padding Strings
str_repeat() builds a string by repeating a given piece a set number of times, and str_pad() extends a string out to a specific target length using a chosen fill character — both are common when formatting fixed-width columns of output.
Example: Repeating and Padding Strings
<?php
// Print `str_repeat("ab", 3) . "\n"` to the output
echo str_repeat("ab", 3) . "\n";
// Print `str_pad("5", 3, "0", STR_PAD_LEFT)` to the output
echo str_pad("5", 3, "0", STR_PAD_LEFT);
?>
Login to try C/C++/Java/PHP code in the editor
Explode and Implode
explode() splits one string into an array of pieces wherever a chosen delimiter appears — turning 'a,b,c' into [a, b, c] on a comma.
implode() performs the exact reverse, joining an array's elements back into a single string with a chosen separator between each.
Example: Explode and Implode
<?php
// Declare `$parts`, set to `explode(",", "a,b,c")`
$parts = explode(",", "a,b,c");
// Print a human-readable dump of `$parts`
print_r($parts);
// Print `implode("-", $parts)` to the output
echo implode("-", $parts);
?>
Login to try C/C++/Java/PHP code in the editor
- Ignoring the return value, as in
trim($s);, when string functions return a new string and do not change the original. - Using
strtolower()on non-English text, which does not handle multi-byte characters; usemb_strtolower(). - Using
strrev()on multi-byte text, which reverses bytes and corrupts the characters.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: