PHP Strings (original) (raw)

Last Updated : 11 Apr, 2025

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 (" ").

Declaring Strings in PHP

There are four different ways to define a string literal in PHP:

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 `

`

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 `

`

**2. Double Quotes

Unlike single-quote strings, double-quote strings in PHP are capable of processing special characters.

PHP `

`

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 `

`

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 `

`

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
strlen() Returns length of a string
strrev() Reverses a string
strtoupper() Converts string to uppercase
strtolower() Converts string to lowercase
strpos() Finds position of a substring
str_replace() Replaces text within a string
substr() Extracts part of a string
trim() Trim spaces from both ends
explode() Splits string into an array
implode() Joins array elements into a string

To read more about PHP String Functions read this article - PHP String Functions

Best Practices for Using Strings

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.