Introduction to PHP Strings

A string in PHP is a sequence of characters, like text, numbers, or symbols. Strings are used to display messages, store user input, and create dynamic content in web applications. PHP supports multiple ways to define and handle string data.

There are four main ways to define strings in PHP:

  • Single-quoted strings

  • Double-quoted strings

  • Heredoc syntax

  • Nowdoc syntax


Single vs. Double Quoted Strings

Single-quoted strings are defined using ' (single quotes). They treat the content literally. That means special characters like \n (newline) or variables like $name will not be processed.

Double-quoted strings are defined using " (double quotes). These strings interpret special characters and variables.

Example:

$name = "Amit";
echo 'Hello $name';  // Output: Hello $name
echo "Hello $name";  // Output: Hello Amit

Heredoc and Nowdoc Syntax

Heredoc syntax behaves like a double-quoted string. It allows multiline strings and interprets variables and escape sequences.

Example:

$name = "Amit";
$text = <<<EOD
Hello, $name!
Welcome to PHP.
EOD;
echo $text;

Nowdoc syntax is similar to heredoc, but it behaves like a single-quoted string. Variables and escape sequences are not processed.

Example:

$text = <<<'EOD'
Hello, $name!
Welcome to PHP.
EOD;
echo $text;

String Functions in PHP

PHP provides many built-in functions to work with strings. Some of the most common are:

Get Length of a String

Use strlen() to get the number of characters.

Example:

echo strlen("Hello world!");

Output:
12


Count Words in a String

Use str_word_count() to count total words in a string.

Example:

echo str_word_count("Hello world!");

Output:
2


Reverse a String

Use strrev() to reverse the characters of a string.

Example:

echo strrev("Hello world!");

Output:
!dlrow olleH


Search for Text in a String

Use strpos() to find the position of a word.

Example:

echo strpos("Hello world!", "world");

Output:
6


Replace Text in a String

Use str_replace() to replace parts of a string.

Example:

echo str_replace("world", "Dolly", "Hello world!");

Output:
Hello Dolly!


Accessing Characters in a String

In PHP, strings can be accessed like arrays using square brackets.

Example:

$str = "Hello";
echo $str[1];  // Output: e

You can also modify characters this way, but it only replaces the first character of the inserted string.


PHP String Operators

PHP has two operators for string handling:

  • . (Concatenation): Joins two strings

  • .= (Concatenation assignment): Appends to a string

Example:

$a = "Hello";
$b = " World";
echo $a . $b;  // Output: Hello World

$a .= $b;
echo $a;       // Output: Hello World

Advanced String Interpolation

For more complex situations, use curly braces to wrap variables inside strings.

Example:

$fruit = "apple";
echo "I have an {$fruit}s";  // Output: I have an apples

This helps avoid confusion when using variable names next to regular text.


Summary

  • Strings are a basic and powerful datatype in PHP.

  • Single quotes are literal; double quotes support variables and escape characters.

  • Heredoc and Nowdoc help with multiline strings.

  • PHP offers many built-in functions to manipulate strings.

  • Use proper string operators for joining and modifying text.


Â