A string is a sequence of characters. Strings are used to store text such as names, addresses, messages, and descriptions in PHP.
In PHP, strings are commonly written inside single quotes or double quotes.
<?php
$name = "Rahul";
$city = 'Patna';
echo $name;
echo "<br>";
echo $city;
?>
Rahul
Patna
Both single-quoted and double-quoted strings can store text, but they handle variable interpolation and escape sequences differently.
Double-quoted strings can interpolate variables. Single-quoted strings generally display variable names as literal text.
<?php
$name = "Amit";
echo 'Hello $name';
echo "<br>";
echo "Hello $name";
?>
Hello $name
Hello Amit
Use single quotes when you do not need variable interpolation. Use double quotes when you want PHP to interpret variables or supported escape sequences.
PHP uses the dot (.) operator to join
strings. This process is called concatenation.
<?php
$firstName = "Rahul";
$lastName = "Kumar";
$fullName = $firstName . " " . $lastName;
echo $fullName;
?>
Rahul Kumar
The dot operator joins the first name, a space, and the last name into one string.
Another example:
<?php
$a = "Welcome";
$b = "to PHP";
echo $a . " " . $b;
?>
Welcome to PHP
String interpolation means inserting a variable's value directly into a double-quoted string.
<?php
$name = "Priya";
$age = 21;
echo "My name is $name.";
echo "<br>";
echo "I am $age years old.";
?>
My name is Priya.
I am 21 years old.
Curly braces can make the variable boundary clearer, especially when additional characters follow it.
<?php
$language = "PHP";
echo "I am learning {$language} programming.";
?>
I am learning PHP programming.
Escape sequences represent special characters inside double-quoted strings.
| Escape Sequence | Meaning |
|---|---|
\n |
New line in the string |
\t |
Horizontal tab |
\" |
Double quote |
\\ |
Backslash |
\$ |
Literal dollar sign |
Example:
<?php
echo "Hello\nWorld";
echo "\n";
echo "Name:\tRahul";
echo "\n";
echo "He said, \"Hello!\"";
echo "\n";
echo "Price: \$500";
?>
Note: These escape sequences affect
the string content. In an HTML page, newlines and tabs
are usually collapsed visually unless displayed in
a suitable element, such as <pre>.
The strlen() function returns the number
of bytes in a string.
<?php
$text = "Hello PHP";
echo strlen($text);
?>
9
The string contains nine ASCII characters, so its byte length is 9.
Example with a variable:
<?php
$name = "Soopro";
$length = strlen($name);
echo "Name: " . $name . "<br>";
echo "Length: " . $length;
?>
Name: Soopro
Length: 6
For UTF-8 text containing characters such as Hindi
letters or many emoji, strlen() counts
bytes rather than visible characters. Use
mb_strlen() with an appropriate encoding
when you need a character count.
PHP allows access to a string character using square brackets and a zero-based offset.
<?php
$text = "HELLO";
echo $text[0];
echo "<br>";
echo $text[1];
echo "<br>";
echo $text[4];
?>
H
E
O
Index 0 refers to the first byte, index 1 to the second byte, and so on. For ASCII text, these offsets correspond to individual characters.
Example: Print each character
<?php
$text = "PHP";
for ($i = 0; $i < strlen($text); $i++) {
echo $text[$i] . "<br>";
}
?>
P
H
P
Strings can be compared using operators such as
== and ===.
For comparing string contents exactly, PHP also
provides strcmp().
<?php
$a = "PHP";
$b = "PHP";
$c = "Java";
var_dump($a === $b);
echo "<br>";
var_dump($a === $c);
?>
bool(true)
bool(false)
The strict comparison operator
=== checks both value and type.
Using strcmp():
<?php
$a = "Apple";
$b = "Banana";
$result = strcmp($a, $b);
if ($result == 0) {
echo "Strings are equal";
} else {
echo "Strings are different";
}
?>
Strings are different
strcmp() returns 0 when the strings are
equal, a negative number when the first string compares
lower, and a positive number when it compares higher.
String comparison is case-sensitive.
We can combine variables and strings to display information about a student.
<?php
$name = "Amit Kumar";
$course = "PHP Programming";
$city = "Aurangabad";
$fee = 5000;
echo "<h3>Student Information</h3>";
echo "Name: " . $name . "<br>";
echo "Course: " . $course . "<br>";
echo "City: " . $city . "<br>";
echo "Course Fee: Rs. " . $fee;
?>
Student Information
Name: Amit Kumar
Course: PHP Programming
City: Aurangabad
Course Fee: Rs. 5000
1. Which operator joins strings in PHP?
2. Which function returns the byte length of a string?
3. What does the expression $text[0] access?