Strings3:43 with Alena Holligan
A string is a series of characters, typically specified within quotations. Explore the differences between single and double quoted strings and using other variables within a string.
Learn more about Strings
Single Quoted Strings are the simplest way to specify a string. Enclose the string in single quotes (the character ').
To specify a literal single quote, escape it with a backslash (\'). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.
Note: variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Double Quoted Strings are enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters. As in single quoted strings, escaping any other character will result in the backslash being printed too.
The most important feature of double-quoted strings is the fact that variable names will be expanded.
Although quotes are the primary way to specify a string, there is another option: the heredoc syntax (<<<). After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
Heredoc syntax allows people to easily write large amounts of text from within PHP, but without the need to constantly escape things. Put simply, it allows you to define your own string limiter so that you can make it something other than a double or single quote. So, for example, we could use the string "EOT" (end of text) for our delimiter, meaning that we can use double quotes and single quotes freely within the body of the text - the string only ends when we type EOT.
$name = 'Alena'; $myString = <<<EOT Example of string spanning multiple lines using spaces and including a $name variable using heredoc syntax. EOT;
Example of string spanning multiple lines using spaces and including a Alena variable using heredoc syntax.
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.
A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.
$name = 'Alena'; $str = <<<'EOT' Example of string spanning multiple lines using spaces and including a $name variable using nowdoc syntax. EOT;
Example of string spanning multiple lines using spaces and including a $name variable using nowdoc syntax.
[MUSIC] 0:00 In the last section we started exploring the two basic pieces of programming, 0:04 storage and retrieval of data, and the logic that tells what and 0:09 when to do something with that data. 0:13 You were introduced to variables and we use integers and float variables 0:16 along with arithmetic and assignment operators, to create a unit converter. 0:20 In this section we'll continue to expand upon these skills as we explore 0:26 more data types and logic to create a daily exercise program. 0:30 We'll store each exercise in a string variable, 0:35 then we'll use conditionals to control which exercise is displayed. 0:38 Let's start by looking at the string variable. 0:43 A string is a series of characters typically specified within quotations. 0:46 You can use either single or double quotes to create a string but 0:51 they work differently. 0:55 Single quoted strings, for the most part represent each individual character 0:57 exactly as it is stored in the string. 1:02 Double quoted strings on the other hand let you place variables inside the string. 1:05 PHP will add the value of the variable or expand the variable within the string. 1:09 This will make a lot more sense when you see it in action. 1:16 So let's take a look at an example of storing and retrieving string variables. 1:18 We're starting a new program, so let's create a new file and name it strings.php. 1:24 Again, we need to start this file using the opening and closing PHP tags. 1:35 Then we're ready to add our variables. 1:41 We'll name this variable, string_one. 1:43 Again, we'll use the assignment operator, 1:47 a single equals sign followed by the value that we want to store. 1:49 In this case, it will be a string surrounded by single quotes. 1:53 For string one, let's set the sequel to hello world. 1:58 Now that the variable string one contains the value hello world. 2:05 Let's retrieve that value using echo string one 2:08 and now we can run the script in the console. 2:15 Great, you've written your second hello world script. 2:19 As I mentioned before, the primary difference between single and 2:22 double quoted strings is whether variable names will be expanded. 2:26 Let's add a variable called name we'll set this equal to our name. 2:31 I'm gonna use my name, Alina. 2:38 You can use your name. 2:41 Then instead of saying hello world, let's say hello Alina using the name. 2:43 Save the file and run the script again. 2:47 Notice that the actual variable name is displayed instead of its value. 2:53 That's because variables are not expanded within single quotes. 2:58 Let's change these to double quotes and run the script again. 3:02 Now it works just like I want and shows hello Alena. 3:12 By using double quotes the PHP interpreter expands the variable 3:17 showing us the value of the name variable instead of the variable name itself. 3:22 If you want the value of a variable within your string 3:27 make sure that you use double quotes. 3:31 There are a few other differences in the way that PHP interprets characters in 3:33 a In the next video will explore the use of escape sequences. 3:37
You need to sign up for Treehouse in order to download course files.Sign up