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

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

Strings in PHP

Updated 2026-05-15
10 min read

Strings in PHP

Introduction

In PHP, a string is a sequence of characters. Strings are one of the most commonly used data types and are essential for any application that involves text manipulation or output. This tutorial will cover how to create, manipulate, and work with strings in PHP.

Concept

Strings in PHP can be defined using either single quotes (') or double quotes ("). The choice between them affects how variables and special characters within the string are interpreted:

  • Single Quotes: Variables and special characters (except for \' to escape a single quote) are not parsed.
  • Double Quotes: Variables and most special characters (including \n, \t, etc.) are parsed.

PHP also supports heredoc and nowdoc syntax, which provide an alternative way to define multi-line strings.

Examples

Creating Strings

You can create strings using single quotes or double quotes:

php
1$singleQuoted = 'Hello, World!';
2$doubleQuoted = "Hello, $name!";

In the example above, $doubleQuoted will output Hello, John! if $name is set to 'John'.

String Concatenation

You can concatenate strings using the . operator:

php
1$greeting = 'Hello';
2$name = 'Alice';
3$message = $greeting . ', ' . $name . '!';

The $message variable will contain 'Hello, Alice!'.

String Length

To find the length of a string, use the strlen() function:

php
1$length = strlen('Hello, World!'); // $length is 13

Substrings

You can extract parts of a string using substr(). The function takes three parameters: the original string, the starting position, and the length of the substring.

php
1$substring = substr('Hello, World!', 7, 5); // $substring is 'World'

String Replacement

To replace parts of a string, use str_replace():

php
1$originalString = 'Hello, World!';
2$newString = str_replace('World', 'PHP', $originalString);
3// $newString is 'Hello, PHP!'

String Splitting

To split a string into an array of substrings, use explode():

php
1$string = 'apple,banana,cherry';
2$array = explode(',', $string); // $array is ['apple', 'banana', 'cherry']

String Joining

To join an array of strings into a single string, use implode():

php
1$array = ['apple', 'banana', 'cherry'];
2$string = implode(',', $array); // $string is 'apple,banana,cherry'

String Case Conversion

PHP provides functions to convert strings to uppercase or lowercase:

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

String Trimming

To remove whitespace from the beginning and end of a string, use trim():

php
1$string = " Hello, World! ";
2$trimmedString = trim($string); // $trimmedString is 'Hello, World!'

String Checking

You can check if a substring exists within a string using strpos(). It returns the position of the first occurrence or false if not found.

php
1$position = strpos('Hello, World!', 'World'); // $position is 7

What's Next?

In the next section, we will explore more advanced string functions and techniques in PHP. Understanding these functions will greatly enhance your ability to manipulate text effectively in your applications.

Stay tuned for more tutorials on PHP!


PreviousArray FunctionsNext String Functions

Recommended Gear

Array FunctionsString Functions