PHP String

A PHP string is a sequence of characters i.e. used to store and manipulate text. There are 4 ways to specify string in PHP.

  • single quoted
  • double quoted
  • heredoc syntax
  • newdoc syntax (since PHP 5.3)

Single Quoted PHP String

We can create a string in PHP by enclosing text in a single quote. It is the easiest way to specify string in PHP.

Output:

Hello text within single quote
We can store multiple line text, special characters and escape sequences in a single quoted PHP string.

Output:

Note: In single quoted PHP strings, most escape sequences and variables will not be interpreted. But, we can use single quote through \’ and backslash through \\ inside single quoted PHP strings.

Output:

Double Quoted PHP String

In PHP, we can specify string through enclosing text within double quote also. But escape sequences and variables will be interpreted using double quote PHP strings.

Output:

Now, you can’t use double quote directly inside double quoted string.

Output:

We can store multiple line text, special characters and escape sequences in a double quoted PHP string.

Output:

In double quoted strings, variable will be interpreted.

Output:

Spread the love

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *