A string is an ordered sequence of characters, such as "Hello World" or "Veewom Educational Tutorial". Strings represent the most frequently manipulated data type in web applications, used for user names, emails, article paragraphs, and HTML templates. In this guide, you will master string creation, the string concatenation operator, essential built-in string functions, and advanced multi-line Heredoc and Nowdoc syntax.
You can define strings using four distinct mechanisms in PHP:
'...'): High performance, treats escape sequences and variable names literally."..."): Supports variable expansion (interpolation) and special escape sequences like \n (newline) and \t (tab).<<<IDENTIFIER ... IDENTIFIER;): Perfect for large multi-line blocks of HTML/text with variable interpolation.<<<'IDENTIFIER' ... IDENTIFIER;): Multi-line literal text without variable parsing..) and Append (.=)Unlike languages like JavaScript or Python that use the plus symbol (+) for string joining, PHP uses the dot (.) operator. Using + in PHP will attempt to perform arithmetic addition!
<?php
$firstName = "Arun";
$lastName = "Kumar";
// Concatenation with dot operator
$fullName = $firstName . " " . $lastName;
echo "Full Name: " . $fullName . "<br>"; // Arun Kumar
// Concatenating and assigning with .=
$greeting = "Good Morning, ";
$greeting .= $fullName;
$greeting .= "! Welcome to PHP class.";
echo $greeting;
?>
| Function | Description | Example & Output |
|---|---|---|
strlen($str) |
Returns string length in bytes/characters. | strlen("Veewom") → 6 |
str_word_count($str) |
Counts total number of words. | str_word_count("PHP is great") → 3 |
strrev($str) |
Reverses the string characters. | strrev("Hello") → "olleH" |
strpos($str, $search) |
Returns 0-based index position of first match. | strpos("Veewom", "wom") → 3 |
str_replace($f, $r, $str) |
Replaces all occurrences of search string. | str_replace("World", "PHP", "Hello World") → "Hello PHP" |
strtolower($str) |
Converts all letters to lowercase. | strtolower("VEEWOM") → "veewom" |
strtoupper($str) |
Converts all letters to uppercase. | strtoupper("veewom") → "VEEWOM" |
trim($str) |
Strips whitespace from start and end. | trim(" clean ") → "clean" |
substr($str, $start, $len) |
Extracts a substring slice. | substr("Veewom", 0, 3) → "Vee" |
explode($del, $str) |
Splits string into array by delimiter. | explode(",", "red,blue,green") → ["red", "blue", "green"] |
When generating complex HTML emails or database SQL queries, Heredoc and Nowdoc avoid messy quote escaping:
<?php
$student = "Sunil";
$examDate = "December 15, 2026";
// HEREDOC: Parses variables ($student, $examDate)
$emailBody = <<<EMAIL
<div class="notice">
<h3>Exam Notification for $student</h3>
<p>Your semester examination is scheduled on $examDate.</p>
</div>
EMAIL;
echo $emailBody;
// NOWDOC: Treats text literally (enclosed in single quotes)
$codeSnippet = <<<'CODE'
The variables $student and $examDate will NOT be replaced here.
CODE;
echo $codeSnippet;
?>
High-Frequency Exam Question:
Q: Why must you check strpos() with strict comparison (=== false)?
A: If the matching substring is located at the very start of the string (index 0), an unstrict comparison like if (strpos(...) == false) will evaluate index 0 to false, wrongly indicating that the match was not found! Always check: if (strpos(...) !== false).
Exercise 7.1: Write a PHP script that checks whether an input string is a Palindrome (reads the same backwards and forwards, e.g. "madam" or "radar") using strrev().
Exercise 7.2: Write a script that cleans an email input: trims whitespace, converts it to lowercase, and verifies it contains the "@" character using strpos().