![]() |
VOOZH | about |
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 (" ").
There are four different ways to define a string literal in PHP:
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.
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:
Welcome to $site
Unlike single-quote strings, double-quote strings in PHP are capable of processing special characters.
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 (') | 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. |
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.
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.
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.
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.
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
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.