Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP String Functions
PHP Intermediate FREE

String Functions

Lesson 1 of 17 Intermediate Interactive

PHP provides a powerful set of built-in string functions for manipulating text. Strings are one of the most commonly used data types in PHP.

Essential String Functions

  • strlen() — Returns the length of a string
  • substr() — Returns part of a string
  • strpos() — Find the position of the first occurrence of a substring
  • str_replace() — Replace all occurrences of a search string
  • strtolower() — Convert string to lowercase
  • strtoupper() — Convert string to uppercase
  • trim() — Remove whitespace from both ends
  • explode() — Split a string into an array
  • implode() — Join array elements into a string
  • sprintf() — Format a string with placeholders

Tip: String functions are non-destructive — they return a new string and do not modify the original.

Syntax

PHP
<?php
$text = "Hello, SukhNexus!";

strlen($text);          // 16

substr($text, 0, 5);   // "Hello"

strpos($text, "Sukh"); // 7

str_replace("SukhNexus", "World", $text); // "Hello, World!"

strtolower($text);      // "hello, sukhnexus!"

strtoupper($text);      // "HELLO, SUKHNEXUS!"

trim("  hello  ");     // "hello"

explode(", ", "a, b, c"); // ["a", "b", "c"]

implode(" - ", ["PHP", "Laravel"]); // "PHP - Laravel"

sprintf("Hello, %s! You are %d.", "Ali", 25);
?>
String Methods
PHP
<?php
$str = "Hello, SukhNexus!";

echo "Length: " . strlen($str) . "
";
echo "Uppercase: " . strtoupper($str) . "
";
echo "Lowercase: " . strtolower($str) . "
";
echo "Substring: " . substr($str, 0, 5) . "
";
echo "Replace: " . str_replace("Hello", "Hi", $str) . "
";
echo "Position: " . strpos($str, "SukhNexus") . "
";
echo "Trim: " . trim("  hello  ") . "
";
?>

Practice

1
Exercise

Given $text = "Hello World", use string functions to convert it to uppercase and get its length.

Answer
<?php
$text = "Hello World";
echo strtoupper($text) . "<br>";
echo strlen($text);
?>
2
Exercise

Use explode() to split "PHP,JavaScript,Python" into an array and display each language.

Answer
<?php
$languages = explode(",", "PHP,JavaScript,Python");
foreach ($languages as $lang) {
    echo "$lang<br>";
}
?>

Quick Quiz

1

What does strlen("SukhNexus") return?

strlen() returns the number of characters. "SukhNexus" has 9 characters.

2

Which function replaces text in a string?

str_replace() replaces all occurrences of a search string with a replacement.

3

What does explode() return?

explode() splits a string by a delimiter and returns an array of substrings.

Interview Questions

strlen() counts bytes, not characters. For multibyte characters like Arabic, Hindi, or Chinese, strlen() may give incorrect results. mb_strlen() (multibyte string length) counts actual characters regardless of encoding. Always use mb_strlen() for UTF-8 content.

sprintf() is preferred when formatting numbers, building complex strings, or when you need to reuse a template. It's more readable, easier to maintain, and can handle number formatting (decimals, currency) that concatenation cannot do cleanly.