Introduction

A string is a series of characters that may consist of letters, digits, or symbols. Strings are fundamental to any programming language.

This tutorial walk you through how to create and view the output of strings, how to concatenate them, how to store strings in variables, how to use escape sequences, and the rules of using quotes, apostrophes, and newlines within strings in PHP.

Single and Double-Quoted Strings

You can create a string in PHP by enclosing a series of characters in either single or double quotes. PHP will interpret the following strings differently:

                        
                            echo 'This is a example of single quotes string.';
                        
                    
                        
                            echo "This is a example of double quotes string.";
                        
                    

Before output, PHP will evaluate and parse any variables or escape sequences within the double-quoted string. Single-quoted strings will output each character exactly as specified.

If you were to echo this string in PHP:

                        
                            echo '"This string\'s in single quotes." To output apostrophes (\') it required a backslash (\) before the apostrophes like (\\\'), but do not use (\") to output the double quotes.';
                        
                    

It will return following output:

                        
                            Output
                            "This string's in single quotes." To output apostrophes (') it required a backslash (\) before the apostrophes like (\'), but do not use (\") to output the double quotes.
                        
                    

If you don’t include a backslash before the apostrophe(') in the single-quoted string, PHP will end the string at that point, which will cause an error. Since you’re using single quotes to create our string, you can include double quotes within it to be part of the final string that PHP outputs.

If you want to render the \' sequence, you must use three backslashes (\\\'). First \\ to render the backslash itself, and then \' to render the apostrophe('). The sequence \" is rendered exactly as specified.

                        
                            echo "\"This string's in double quotes.\" To output double quotes it requires a backslash (\) before the double quotes (\"), but you MUST NOT add a backslash before the apostrophe (\').";
                        
                    
                        
                            Output
                            "This string's in double quotes." To output double quotes it requires a backslash (\) before the double quotes ("), but you MUST NOT add a backslash before the apostrophe (\').
                        
                    

As with the single-quoted string, if a backslash is not included before the double quotes in the double-quoted string, PHP will end the string at that point, which will cause an error. Since the double-quoted string is not ended with a single quote, you add the apostrophe(') directly to a double-quoted string. A double-quoted string will output \' with either a single or double backslash used with the apostrophe.

To output the \" sequence, you must use three backslashes. First \\ to render the backslash itself, and then \" to render the double quote. The sequence \' is rendered exactly as specified.

The \ is known as an escape character. Combined with a secondary character, it makes up an escape sequence. Now that you have an understanding of strings, let’s review escape sequences.

Escape Sequences

An escape sequence tells the program to stop the normal operating procedure and evaluate the following characters differently.

In PHP, an escape sequence starts with a backslash \. Escape sequences apply to double-quoted strings. A single-quoted string only uses the escape sequences for a single quote or a backslash.

Here are some common escape sequences for double-quoted strings:

Lets see how you can use these sequences in a string:

                        
                            echo "\"Today I spent \$50 on books\"\n\tand Car!";
                        
                    
                        
                            Output
                            "Today I spent $50 on books"
                                and Car!
                        
                    

Creating and Viewing the Output of Strings

The most important feature of double-quoted strings is the fact that variable names will be expanded, giving you the value of the variable. You can use a variable to stand in for a string or use a string directly. You output the string by calling the echo function:

                        
                            $name = "James";
                            echo 'Name is specified using the variable $name.';
                            echo "\n"; // escape sequence for newline character
                            echo "Hello, my name is $name. It's stored in the variable \$name.";
                        
                    

The $name variable is created on the first line. On the second line, the echo function is used to output a string in single quotes. Using the $name variable within this single-quoted string displays the characters exactly as they are written, so we will see the variable name instead of its value.

On the fourth line, we use the echo function again, but we are using double quotes this time. This time the variable is expanded to show the value in the first sentence. In the next sentence, there is a \ before the $ to explicitly tell the string to display a $ character and not expand the variable.

                        
                            Output
                            Name is specified using the variable $name.
                            Hello, my name is James. It's stored in the variable $name.
                        
                    

Note: When string evaluation is not a concern, you may choose to use either single quotes or double quotes, but whichever you decide on, you should be consistent within a program. Single quotes may be marginally faster.

String Concatenation

Concatenation means joining strings together, end-to-end, to build a new string. In PHP, there are two main ways to concatenate a string.

The first is to include a string variable within a double-quoted string. This was shown in the previous step and in the following:

                        
                            $name = "James wisely.";
                            echo "Hello my name is $name";
                        
                    

Running this code will combine the string and the $name variable, which is set to James wisely.:

                        
                            Output
                            Hello my name is James wisely.
                        
                    

A second way to concatenate strings is to use the dot(.) operator.

Let’s combine the strings "James" and "wisely" together with concatenation through an echo statement:

                        
                            echo "James" . "wisely";
                        
                    

This code uses the dot(.) operator to combine the "James" string and the "wisely" string without a space in between.

                        
                            Output
                            Jameswisely
                        
                    

If you would like whitespace between the two strings, you must include the whitespace within a string, like after the word James:

                        
                            echo "James " . "wisely";
                            //or by concating space between strings
                            echo "James"." "."wisely";
                        
                    
                        
                            Output
                            James wisely
                        
                    

Conclusion

you learned how to create and view the output of strings, how to use escape sequences, how to concatenate strings, how to store strings in variables, and the rules of using quotes, apostrophes, and newlines within strings in PHP.

As you continue to work with strings, keep in mind these three aspects:

  1. Pay special attention to quotes within your strings.
  2. Use concatenation to combine your strings.
  3. Use variables to make your strings reusable.