PHP String
Last updated onA PHP string is a list of characters that are located inside quotes. The maximum number of characters inside it should not exceed 256. Moreover, PHP does not support native Unicode. Also, the string can be enclosed in either double quotes or single quotes.
Additionally, you can put anything in a PHP string, such as letters, numbers, spaces, and symbols.
As you may know, PHP supports four types of strings, such as single-quoted, double-quoted, heredoc, and nowdoc. Let’s focus on each one in-depth.
PHP Single-Quoted Strings
A single quote starts with a punctuation mark called an apostrophe (‘) and ends with another one. Inside it, you can put your sequence of characters.
Here is an example:
$single_quote = 'Welcome to CodedTag Tutorials';
There is a special case when you use the single-quoted string with a backslash (\) character. Now, observe this sentence — “Hello, I’m an editor at CodedTag!”. However, you can’t print this text within a single-quoted string. Let’s see why.
$single_quote_error = 'Hello, I'm an editor at CodedTag!';
// Output: Parse error: syntax error, unexpected 'm'
The PHP interpreter will parse an error, specifically a syntax error, because we used three single quotes, with only one having no closing pair. And to fix this issue, we have to use the backslash beside the single quote that is located in the middle, like this:
$correct_string = 'Hello, I\'m an editor at CodedTag!';
// Output: Hello, I'm an editor at CodedTag!
Moreover, single quotes strip PHP variables. So, when including a PHP variable inside text within single quotes, it will not print the variable value. Let’s see an example.
$variable = 'Hello world!';
echo 'PHP interpreter will not print the $variable value within Single quote';
// Output: PHP interpreter will not print the $variable value within Single quote
One more thing, the PHP interpreter also ignores the escaped characters within single-quoted strings, such as \n, \r, \t, and \v. So, when you try to break down a string line into the following line, it will print the character without executing it. For example:
echo 'This line will not break \n into the following line.';
// Output: This line will not break \n into the following line.
Anyway, let’s move into the following section to learn more about the double-quoted string type.
PHP Double-Quoted Strings
A double-quoted string starts with two double inverted commas (“”) at the beginning of the string and ends with another pair of two double inverted commas. Between them is a set of characters.
Here is a quick example:
echo "Hello, I am a programmer!";
Moreover, a double-quoted string can include the value of a variable. For example:
$variable = "scripting language";
echo "PHP is $variable!";
// Output: PHP is scripting language!
Additionally, you can also use escape characters within a double-quoted string. Let’s see an example.
$line= "This line will break down\n into the following line.";
echo $line;
/*
Output: This line will break down
into the following line.
*/
In the following part, we will focus on the heredoc syntax in PHP strings. Let’s move forward.
Heredoc Syntax
Usually, the heredoc syntax starts with —“<<<” followed by the identifier as an open tag. And then it should be closed with the same identifier name.
Inside it, you can put the characters of a string. For example:
$var = <<<IDENTIFIER
This is a CodedTag Tutorial!
IDENTIFIER;
echo $var;
The string content of this syntax should be on a new line or in the middle of the opening and closing identifiers. Also, no indentation is needed before the string; otherwise, it will show you an error.
Additionally, the identifier name can be any name you like, but it must be the same in the closing tag. The “Invalid indentation” error occurs in PHP 7.3 and prior versions.
Let’s move on to the latest type in PHP strings, which is the Nowdoc syntax.
Nowdoc Syntax
In Nowdoc syntax, the identifier should be enclosed in single quotes with no indentation, or it can be a string in the same line as the identifier.
For example:
$variable = <<<'CODEDTAG_OUT'
Welcome to CodedTag! Let's dive into PHP tutorials.
CODEDTAG_OUT;
echo $variable;
// Output: Welcome to CodedTag! Let's dive into PHP tutorials.
Inside it, you can use tabs and spaces or use double or single quotes without any problems. However, it’s important to note that for the closing identifier name, there should be no tabs or spaces before it; otherwise, it will show you an error within the syntax.
Anyway, let’s summarize it in a few paragraphs.
Wrapping Up
In this tutorial, you understood four types of PHP strings with examples, including the basic definition of each one.
Starting with the main definition of a PHP string, which is a sequence of characters located inside quotes, and its limitations.
Then, in the following part, we discussed the first type of string, which is the single-quoted string. We observed how it interprets characters quite literally. Additionally, we explored its limitations and saw how it does not work effectively with escaped characters and PHP variables
And then, we learned how double-quoted strings work, representing a step up in complexity. These strings are more dynamic, enabling variable interpolation and allowing the escape characters to work inside.
In the following part, we also discussed how to use heredoc syntax. It is a unique structure, starting with “<<<” and an identifier. Here, we can handle large blocks of text.
In the latest part, you learned how to use the nowdoc syntax with its identifier rules.
Thank you for reading. Happy Coding!