Strings in PHP: Functions & Manipulation
Strings in PHP
A string is a sequence of characters, such as a name, a sentence, or an email address. PHP gives you flexible ways to create strings and a large set of built-in functions to search, change, and format text. Strings are everywhere in web work, from form data to database results.
Creating strings: single vs double quotes
Both single and double quotes create strings, but they behave differently.
<?php
$name = "Divya";
// Double quotes expand variables and escape sequences.
echo "Hello $name\n"; // Hello Divya (with a newline)
// Single quotes are literal - no variable expansion.
echo 'Hello $name'; // Hello $name
?>
Use double quotes when you want to insert variables; use single quotes for plain, literal text.
Concatenation
Join strings with the dot (.) operator.
<?php
$first = "Web";
$second = "Dev";
$combined = $first . " " . $second;
echo $combined; // Web Dev
?>
String interpolation
Inside double quotes, you can drop variables straight into the text. Use curly braces for clarity, especially with array values.
<?php
$user = ["name" => "Farhan"];
echo "Welcome, {$user['name']}!"; // Welcome, Farhan!
?>
Measuring and searching strings
<?php
$text = "Learn PHP";
echo strlen($text); // 9 -> number of characters
// Find the position of a substring (0-based). Returns false if not found.
$pos = strpos($text, "PHP"); // 6
// Check if a string contains another (PHP 8).
var_dump(str_contains($text, "PHP")); // true
?>
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesChanging case and trimming
<?php
$name = " priya kabra ";
echo strtoupper($name); // " PRIYA KABRA "
echo strtolower("HELLO"); // hello
echo ucwords("priya kabra"); // Priya Kabra (capitalise each word)
echo trim($name); // "priya kabra" -> removes outer spaces
?>
Replacing and extracting
<?php
$sentence = "I like tea";
// Replace one substring with another.
echo str_replace("tea", "coffee", $sentence); // I like coffee
// Extract part of a string: substr(string, start, length).
echo substr("Jalgaon", 0, 3); // Jal
?>
Splitting and joining
<?php
$csv = "PHP,MySQL,HTML";
// Split a string into an array on a separator.
$parts = explode(",", $csv); // ["PHP", "MySQL", "HTML"]
// Join an array back into a string.
$joined = implode(" | ", $parts); // "PHP | MySQL | HTML"
echo $joined;
?>
Formatting numbers into strings
number_format() and sprintf() help present values neatly.
<?php
echo number_format(1234567.891, 2); // 1,234,567.89
// sprintf builds a formatted string without printing immediately.
$label = sprintf("Score: %d/%d", 8, 10);
echo $label; // Score: 8/10
?>
Common mistakes
- Expecting single quotes to expand variables. Only double quotes do.
- Treating
strpos()returning 0 as "not found". Index 0 is a valid match; check with=== false. - Forgetting strings are zero-indexed when using
substr(). - Confusing
explode()andimplode().explodesplits a string into an array;implodejoins an array into a string.
FAQ
When do I use single vs double quotes? Use double quotes when you want variables inside the string. Use single quotes for literal text; they are marginally faster and avoid accidental expansion.
Why does strpos() sometimes give wrong results?
If the match is at the very start, it returns 0, which looks falsy. Always compare with === false to test for "not found".
How do I check if a string contains another string?
Use str_contains($haystack, $needle), added in PHP 8, which returns a clear boolean.
Keep learning
Back to the PHP & MySQL hub, revisit Functions in PHP, or continue to Object-Oriented Programming in PHP.
Practise text processing on real projects. Join the waitlist for the PHP course at Infoplanet in Jalgaon.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in PHP: Indexed, Associative & Multidimensional
Understand PHP arrays - indexed, associative, and multidimensional - and the most useful array functions, with examples for adding, reading, and looping over data.
Control Flow in PHP: if, switch & Loops
Make PHP code decide and repeat with control flow: if/else, switch, the match expression, and the for, while, and foreach loops, with clear examples.
Handling Forms in PHP (GET & POST)
Handle HTML forms in PHP: the difference between GET and POST, reading input from the superglobals, validating data, and escaping output to prevent XSS.
