codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

25 / 56 topics
24Strings in PHP25String Functions26Regular Expressions in PHP
Tutorials/PHP/String Functions
🐘PHP

String Functions

Updated 2026-05-15
10 min read

String Functions

Introduction

In PHP, strings are sequences of characters used to represent text. Manipulating strings is a common task in web development and scripting. PHP provides a rich set of built-in functions that allow you to perform various operations on strings, such as searching, replacing, splitting, joining, and more.

This tutorial will cover some of the most commonly used string functions in PHP, providing both theoretical explanations and practical examples to help you understand how to use them effectively.

Concept

PHP offers a wide range of string manipulation functions. Here are some of the essential ones:

  1. Concatenation: Combining two or more strings.
  2. Length Calculation: Determining the number of characters in a string.
  3. Searching and Replacing: Finding substrings within a string and replacing them.
  4. Splitting and Joining: Breaking down a string into an array or combining an array into a string.
  5. Case Conversion: Changing the case of strings (e.g., to uppercase or lowercase).
  6. Trimming: Removing whitespace from the beginning, end, or both ends of a string.

Examples

1. Concatenation

You can concatenate strings using the . operator.

php
1$str1 = "Hello";
2$str2 = "World";
3echo $str1 . " " . $str2; // Outputs: Hello World

2. Length Calculation

The strlen() function returns the length of a string.

php
1$str = "Hello, World!";
2echo strlen($str); // Outputs: 13

3. Searching and Replacing

  • strpos(): Finds the position of the first occurrence of a substring.
  • str_replace(): Replaces all occurrences of a search string with a replacement string.
php
1$str = "Hello, World!";
2$search = "World";
3$replace = "PHP";
4
5// Find position
6$pos = strpos($str, $search);
7echo $pos; // Outputs: 7
8
9// Replace substring
10$newStr = str_replace($search, $replace, $str);
11echo $newStr; // Outputs: Hello, PHP!

4. Splitting and Joining

  • explode(): Splits a string into an array using a delimiter.
  • implode(): Joins elements of an array into a single string.
php
1$str = "apple,banana,cherry";
2$fruits = explode(",", $str);
3print_r($fruits); // Outputs: Array ( [0] => apple [1] => banana [2] => cherry )
4
5$newStr = implode(" and ", $fruits);
6echo $newStr; // Outputs: apple and banana and cherry

5. Case Conversion

  • strtoupper(): Converts a string to uppercase.
  • strtolower(): Converts a string to lowercase.
php
1$str = "Hello, World!";
2echo strtoupper($str); // Outputs: HELLO, WORLD!
3echo strtolower($str); // Outputs: hello, world!

6. Trimming

  • trim(): Removes whitespace from both ends of a string.
  • ltrim(): Removes whitespace from the left end of a string.
  • rtrim(): Removes whitespace from the right end of a string.
php
1$str = " Hello, World! ";
2echo trim($str); // Outputs: Hello, World!
3echo ltrim($str); // Outputs: Hello, World!
4echo rtrim($str); // Outputs: Hello, World!

What's Next?

After mastering string functions, you can explore more advanced topics such as Regular Expressions in PHP. Regular expressions provide a powerful way to search and manipulate strings based on patterns.

Stay tuned for the next tutorial where we will dive into regular expressions and how they can be used to perform complex string operations in PHP!


PreviousStrings in PHPNext Regular Expressions in PHP

Recommended Gear

Strings in PHPRegular Expressions in PHP