PHP Strings are a sequence of characters surrounded by single or double quotes. Learn how to create a PHP String and explore the various PHP String methods with examples:
In this tutorial, you will learn what a PHP string is, how to create a string, PHP string functions, and frequently asked questions (FAQs).
Please note that we have used PHP version 7 in all examples.
Let’s begin!!
=> Series of Simple PHP Tutorials
Table of Contents:
Introduction to PHP Strings

A PHP string is a series of characters such as ‘Hello world!’. The characters can be letters, numbers, special characters, or a combination of all. As we discussed in one of our previous tutorials, the string is a data type used in PHP.
How to Create a PHP String
There are four methods to create a string as listed below:
- Using single-quotes (‘ ‘)
- Using double-quotes (” “)
- Using heredoc (<<<)
- Using nowdoc (<<< ‘EOT’)
Let’s discuss each of the above methods in detail.
Single-quotes
The simplest and easiest method to create a string is to enclose the text in single quotes (‘ ‘).
Examples:
#1) A string can be a word or multiple words (including numbers and special characters): ‘Hello’, ‘Hello world!’, ‘the year 2020’
#2) It can be just a number or a letter: ‘A’, ‘7’, ‘102530’
#3) It can be a date: ‘2021/01/31′
#4) It can be a sentence or a paragraph:
‘Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut commodo, lectus varius tempor pellentesque, diam justo porttitor dolor, non luctus metus lacus ut dolor. Sed tempor in arcu eu consectetur.’
#5) It can contain multiple sentences or paragraphs, too.
‘Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a sapien aliquet, sollicitudin eros sed, finibus velit. Duis in consectetur lacus. Morbi vitae ante in elit pharetra sollicitudin. Quisque gravida mi eu nisl consequat aliquet. Mauris lacinia ante tortor, ut malesuada lectus finibus in. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque porttitor diam est, eget mattis elit faucibus in. Donec sodales ut turpis eget aliquet. Vestibulum nulla magna, eleifend vel aliquam in, ultrices eu quam.
Nunc lobortis egestas odio, ut feugiat velit commodo pharetra. Duis nec metus in dolor dictum feugiat. Curabitur sit amet lacus eget enim pharetra faucibus. Ut mollis nec justo in dignissim. Ut libero mauris, imperdiet in malesuada ut, feugiat a eros. Aliquam ut ultrices purus, sed euismod quam. Etiam sed metus ac leo porta ornare vel a nibh. Phasellus ut elit non mauris accumsan bibendum. Curabitur ut feugiat elit, nec efficitur justo. Integer gravida vestibulum magna id commodo.’
#6) Further, a string can be empty too. It is known as an empty string as it does not contain any characters: ”
Double-quotes
Another simple and easy method to create a string is to enclose the text in double quotes (” “).
Examples:
#1) “Hello”, “Hello world!”, “the year 2020”
#2) “A”, “7”, “102530”
#3) “2021/01/31”
#4) “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut commodo, lectus varius tempor pellentesque, diam justo porttitor dolor, non luctus metus lacus ut dolor. Sed tempor in arcu eu consectetur.”
#5) “Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla a sapien aliquet, sollicitudin eros sed, finibus velit. Duis in consectetur lacus. Morbi vitae ante in elit pharetra sollicitudin. Quisque gravida mi eu nisl consequat aliquet. Mauris lacinia ante tortor, ut malesuada lectus finibus in. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque porttitor diam est, eget mattis elit faucibus in. Donec sodales ut turpis eget aliquet. Vestibulum nulla magna, eleifend vel aliquam in, ultrices eu quam.
Nunc lobortis egestas odio, ut feugiat velit commodo pharetra. Duis nec metus in dolor dictum feugiat. Curabitur sit amet lacus eget enim pharetra faucibus. Ut mollis nec justo in dignissim. Ut libero mauris, imperdiet in malesuada ut, feugiat a eros. Aliquam ut ultrices purus, sed euismod quam. Etiam sed metus ac leo porta ornare vel a nibh. Phasellus ut elit non mauris accumsan bibendum. Curabitur ut feugiat elit, nec efficitur justo. Integer gravida vestibulum magna id commodo.”
#6) “”
Advanced Example:
Let’s consider the two code segments given below, code A and code B. Code A and code B contain single-quoted and double-quoted strings (echo statements), respectively.
Single-quoted strings are treated literally, while double-quoted strings replace variables with their values.
You can practice this example by running the following programming code:
Code A:
<?php
$name = 'Alex';
echo 'My name is $name'; // single-quoted
?>
Output:
My name is $name
Code B:
<?php
$name = 'Alex';
echo "My name is $name"; // double-quoted
?>
Output:
My name is Alex
Escaped Characters
If a string is enclosed in double quotation marks, PHP uses the following replacements for special characters:
- \n is replaced by a linefeed.
- \r is replaced by a carriage return.
- \t is replaced by a horizontal tab.
- \v is replaced by a vertical tab.
- \e is replaced by an escape.
- \f is replaced by a form feed.
- \\ is replaced by a backslash (\).
- \$ is replaced by a dollar sign ($).
- \’ is replaced by a single quote (‘).
- \” is replaced by a double quote (“).
An example is shown below. You can practice this example by running the following programming code:
<?php
echo "John\tDoe"; // double-quoted
echo "<br/>";
echo 'John\tDoe'; // single-quoted
?>
Output:
John Doe John\tDoe
Heredoc
The heredoc syntax is <<<. Heredoc is similar to a double-quoted string, but putting the double quotes is not required.
The line with the closing identifier must not contain other characters, except a semicolon (;). Therefore, the identifier cannot be indented (whitespace or tab is not allowed).
Let’s see a few examples. You can practice these examples by running the following programming code.
- Valid Example:
<?php
$str = <<<test
Software Testing Help
test;
echo $str;
?>
Output:
Software Testing Help
- Invalid Example:
<?php
$str = <<<test
Software Testing Help
test; //invalid as the identifier is indented (whitespace or tab is not allowed)
echo $str;
?>
Output:
Parse error: Invalid body indentation level (expecting an indentation level of at least 4) in C:\xampp\htdocs\test.php on line 5
Nowdoc
The nowdoc syntax is <<< ‘EOT’. It is similar to the heredoc syntax but specifies the identifier in single quotes.
Nowdoc is similar to a single-quoted string. Unlike in heredoc, quotation marks (single quotes) are required in nowdoc. It follows the same rules as heredoc.
Let’s see an example. You can practice this example by running the following programming code:
<?php
$str = <<<'test'
Software Testing Help
test;
echo $str;
?>
Output:
Software Testing Help
PHP String Functions
There are over 100 string functions in PHP. Due to the space limit, we have only shown some frequently used ones in the list below.
- PHP explode() – Split a string into an array.
- PHP implode() – Combine array elements to form a string.
- PHP str_contains() – Determine if a PHP string contains a given substring.
- PHP str_replace() – Replace the characters of a PHP search string with the characters of a replacement string.
- PHP str_ireplace() – Case-insensitive version of the PHP string replace function.
- PHP str_starts_with() – Check if a PHP string starts with a given substring.
- PHP strlen() – Return the PHP string length.
- PHP strpos() – Find the position of the first occurrence of a substring in another string.
- PHP substr_count() – Return the number of PHP substring occurrences.
- PHP trim() – Remove whitespace or other predefined characters from both sides of a string.
- PHP strcmp – Binary safe string comparison.
- PHP strstr – Find the first occurrence of a string.
You can practice the below examples by running the following programming code:
Example 1:
<?php
echo strlen("Software Testing Help!!!");
echo "<br/>";
?>
Output:
24
Explanation:
The strlen() function shows the total length of the string.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| S | o | f | t | w | a | r | e | T | e | s | t | i | n | g | H | e | l | p | ! | ! | ! |
Example 2:
<?php
echo strpos("Software Testing Help","Testing");
echo "<br/>";
?>
Output:
9
Explanation:
The strops() function shows the position of the first occurrence of a given substring. The important point to remember is that the string position starts from zero, not 1.
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| S | o | f | t | w | a | r | e | T | e | s | t | i | n | g | H | e | l | p |
Example 3:
<?php
echo strpos("Software Testing Help","testing");
echo "<br/>";
?>
Output:
(No Output)
Explanation:
Example 3 is similar to Example 2. The only difference is no matching content, as the matching is case-sensitive. Hence, there is no output.
Example 4:
<?php
$my_str = "Software Testing Help";
print_r (explode(" ",$my_str));
echo "<br/>";
?>
Output:
Array ( [0] => Software [1] => Testing [2] => Help )
Explanation:
The explode() function splits a string into an array.
| 0 | 1 | 2 |
|---|---|---|
| Software | Testing | Help |
Example 5:
<?php
$my_str = array('Software','Testing','Help');
print_r (implode(" ",$my_str));
?>
Output:
Software Testing Help
Explanation:
The implode() function combines array elements to form a string.
Frequently Asked Questions
1. What is a PHP string?
It is a collection of characters such as ‘Hello world!’ or “Hello world!”.
2. How can we create a string in PHP?
The easiest way to create a string is to enclose the text in single or double quotes.
3. What is the use of string functions?
They are used to manipulate strings or query information about strings.
4. What is string syntax?
The basic string syntax is a sequence of characters enclosed in single or double quotes.
5. Is a string an array?
No. Even though both strings and arrays look similar, they are not the same.
6. What is the difference between an array and string?
Both are a collection of characters, but different in many ways. Usually, the size of an array is fixed, while strings can have a variable number of elements.
7. What is the difference between the PHP implode and PHP explode functions?
The implode() function is used to convert a PHP array to string. On the other hand, the explode() function is used to split a string into an array.
8. What does the PHP trim string function do?
The trim() function is a built-in function that removes whitespaces and predefined characters from both sides of a string.
Conclusion
PHP string is a series of characters. There are four methods to create a PHP string; single quotes, double quotes, heredoc, and nowdoc.
Nowdocs are similar to single-quoted strings, while heredocs are similar to double-quoted strings. Furthermore, PHP has built-in functions to deal with strings.






