Lesson 19 of 50 – PHP Strings
38%

PHP Strings

A string is a sequence of characters. Strings are used to store text such as names, addresses, messages, and descriptions in PHP.

1. What Is a String?

In PHP, strings are commonly written inside single quotes or double quotes.

<?php
$name = "Rahul";
$city = 'Patna';

echo $name;
echo "<br>";
echo $city;
?>
Output:
Rahul
Patna

Both single-quoted and double-quoted strings can store text, but they handle variable interpolation and escape sequences differently.

2. Single Quotes and Double Quotes

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";
?>
Output:
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.

3. Concatenating Strings

PHP uses the dot (.) operator to join strings. This process is called concatenation.

<?php
$firstName = "Rahul";
$lastName = "Kumar";

$fullName = $firstName . " " . $lastName;

echo $fullName;
?>
Output:
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;
?>
Output:
Welcome to PHP
4. String Interpolation

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.";
?>
Output:
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.";
?>
Output:
I am learning PHP programming.
5. Escape Sequences in Strings

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>.

6. Getting the Length of a String

The strlen() function returns the number of bytes in a string.

<?php
$text = "Hello PHP";

echo strlen($text);
?>
Output:
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;
?>
Output:
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.

7. Accessing Characters in a String

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];
?>
Output:
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>";
}
?>
Output:
P
H
P
8. Comparing Strings

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);
?>
Output:
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";
}
?>
Output:
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.

9. Practical Example: Student Information

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;
?>
Output:
Student Information
Name: Amit Kumar
Course: PHP Programming
City: Aurangabad
Course Fee: Rs. 5000
Practice Assignments
  1. Create a variable containing your full name and display it.
  2. Store your first name and last name in separate variables and join them.
  3. Display a message using double quotes and variable interpolation.
  4. Find the length of the string "Computer Education".
  5. Print each character of the word "PROGRAMMING" on a new line.
  6. Compare two strings and display whether they are equal.
  7. Create a student information display using at least four string variables.

🧠 Quick Quiz

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?