← Back to PHP Course | Chapter 6: Strings | Lesson 3 of 8

PHP String Functions

String functions are like tools in a text toolbox: some make letters big or small, others measure length or trim spaces. They let you change and inspect words without typing them out again.
Syntax
php
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
<?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");
?>

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
<?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) . "]";
?>

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
<?php
// Print `strrev("hello")` to the output
echo strrev("hello");
?>

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
<?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);
?>

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
<?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);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Ignoring the return value, as in trim($s);, when string functions return a new string and do not change the original.
  2. Using strtolower() on non-English text, which does not handle multi-byte characters; use mb_strtolower().
  3. Using strrev() on multi-byte text, which reverses bytes and corrupts the characters.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.