PHP Strings
In PHP, strings are one of the most commonly used data types. A string is a sequence of characters used to represent text, such as words and sentences. Strings are enclosed in either single quotes (‘ ‘) or double quotes (” “).
- You can create a string using single quotes (‘ ‘) or double quotes (” “).
- PHP supports special syntax like heredoc and nowdoc for multiline strings.
- You can join (concatenate) two or more strings using the dot (.) operator.
Declaring Strings in PHP
There are four different ways to define a string literal in PHP:
- single quoted
- double quoted
- heredoc syntax
- nowdoc syntax
1. Single Quotes
Single quotes are used to define simple strings in PHP. The text within single quotes is treated literally, meaning special characters and variables are not interpreted.
<?php
$site = 'Welcome to GeeksforGeeks';
echo $site;
?>
Output
Welcome to GeeksforGeeks
The above program compiles correctly. We have created a string, ‘Welcome to GeeksforGeeks’ and stored it in a variable and printed it using an statement.
Let us now look at the program below:
<?php
$site = 'GeeksforGeeks';
echo 'Welcome to $site';
?>
Output
Welcome to $site
- In the above program, the echo statement prints the variable name rather than the contents of the variables.
- This is because single-quoted strings in PHP do not process special characters.
- Hence, the string is unable to identify the ‘$’ sign as the start of a variable name.
2. Double Quotes
Unlike single-quote strings, double-quote strings in PHP are capable of processing special characters.
<?php
echo "Welcome to GeeksforGeeks \n";
$site = "GeeksforGeeks";
echo "Welcome to $site";
?>
Output
Welcome to GeeksforGeeks Welcome to GeeksforGeeks
Some important and frequently used special characters that are used with double-quoted strings are explained below:
The character begins with a backslash(“\”) and is treated as escape sequences and is replaced with special characters. Here are a few important escape sequences.
Escape Sequence | Replaced By |
---|---|
\n | New line |
\t | Tab space |
\$ | Dollar sign ($ ) |
\r | Carriage return |
\\ | Backslash (\ ) |
\" | Double quote (" ) |
\' | Single quote (' ) |
Single Quotes vs Double Quotes
Single Quotes (‘) | Double Quotes (“) |
---|---|
Shows the text exactly as you write it. | Reads the variable inside the text and shows its value. |
Special characters like \n don’t work. | Special characters like \n (new line) do work. |
Use \’ if you want to add a single quote inside. | Use \” if you want to add a double quote inside. |
3. Heredoc Syntax
The syntax of Heredoc (<<<) is another way to delimit PHP strings. An identifier is given after the heredoc (<<< ) operator, after which any text can be written as a new line is started. To close the syntax, the same identifier is given without any tab or space.
<?php
$input = <<<testHeredoc
Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.
I am enjoying this.
testHeredoc;
echo $input;
?>
Output
Welcome to GeeksforGeeks. Started content writing in GeeksforGeeks!. I am enjoying this.
Note: Heredoc syntax is similar to the double-quoted string, without the quotes.
4. Nowdoc Syntax
Nowdoc is very much similar to the heredoc other than the parsing done in heredoc. The syntax is similar to the heredoc syntax with symbol <<< followed by an identifier enclosed in single quotes. The rule for nowdoc is the same as heredoc.
<?php
$input = <<<'testNowdoc'
Welcome to GeeksforGeeks.
Started content writing in GeeksforGeeks!.
testNowdoc;
echo $input;
// Directly printing string
// without any variable
echo <<<'Nowdoc'
Welcome to GFG .
Learning PHP is fun in GFG.
Nowdoc;
?>
Output
Welcome to GeeksforGeeks. Started content writing in GeeksforGeeks!. Welcome to GFG . Learning PHP is fun in GFG.
Note: Nowdoc syntax is similar to the single-quoted string.
Commonly Used PHP String Functions
Function | Description |
---|---|
Returns length of a string | |
Reverses a string | |
Converts string to uppercase | |
Converts string to lowercase | |
Finds position of a substring | |
Replaces text within a string | |
Extracts part of a string | |
Trim spaces from both ends | |
Splits string into an array | |
Joins array elements into a string |
To read more about PHP String Functions read this article – PHP String Functions
Best Practices for Using Strings
- Use single quotes when variable parsing is not needed.
- Use trim() to clean user input.
- Escape quotes using \’ or \” when necessary.
- Validate string data before using it (especially user input).
- Use explode() and implode() to handle CSV-like or list data.
Conclusion
Strings are a fundamental part of PHP programming, used everywhere from user input to dynamic content generation. PHP provides a rich set of functions to work with strings, making it easy to manipulate and format text efficiently. By mastering PHP string handling, you’ll be able to create powerful, flexible, and user-friendly web applications.