Escape Sequence4:28 with Alena Holligan
Escape sequences are used within strings to denote special meaning. begin with the escaping character backslash (\) and are used to signify that the character after the escape character should be treated specially. There are two types of escape sequences. Both begin with the escaping character backslash (\), and are distinguished based on the proceeding character.
Escape Sequences begin with the escaping character backslash () and are used to signify that the character after the escape character should be treated specially. There are two types of escape sequences, distinguished based on the character following the escape character "backslash".
If the backslash is followed by an alphanumeric character, the entire escape sequence, including the backslash, is given a meaning. For example: \n will give you a new line, and \t will give you a tab.
If the backslash is followed by a special character, then the character following the backslash is parsed as it is in that location. This is the one we'll be using to show a dollar sign or a quote. Let's jump into workspaces and see how this affects our string.
Escaped Characters: if the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters:
We've looked at how PHP interprets variables, within a string. 0:00 If we use double quotes, the value will be displayed instead of the variable name. 0:04 What happens if we want to add a dollar sign symbol to our string as well? 0:09 Can we do that with double quotes? 0:14 And speaking of quotes. 0:16 How do we add quotes to our string? 0:17 We'll be using something known as an escape sequence 0:19 because they break out of the way PHP would normally interpret the string. 0:22 Escape sequences begin with the escape character backslash. 0:28 And are used to signify that the character after the escape character should be 0:32 treated specially. 0:36 There are two types of escape sequences. 0:39 If the backslash is followed by an alphanumeric character. 0:41 The entire escape sequence including the backslash is given a meaning. 0:45 For example, \n will give you a new line and \t will give you a tab. 0:50 If the backslash is followed by a special character, 0:57 then the character following the backslash is parsed as it is in that location. 1:00 This is the one we'll be using to show a dollar sign or a quote. 1:06 Let's jump into work spaces and see how this affects our string. 1:10 Currently, our string is surrounded by double quotes. 1:14 So, the variable is expanded and we see the value instead of the variable name. 1:17 The first thing we'll look at, is what happens when I add the escape character 1:23 backslash before the dollar sign. 1:26 And now run the script again. 1:31 Once again, the actual variable name is displayed instead of its value. 1:34 That's because the escape character tells the PHP interpreter 1:39 that the next character should be treated specially. 1:43 Because the next character is a special character, 1:46 that special character should be parsed as it is in that location. 1:48 So instead of treating the dollar sign as the start of a variable, 1:53 the PHP interpreter parses it as an actual dollar sign. 1:57 Now let's say instead of just Hello Alina, I wanna give a little more context and say 2:02 learning to display hello Alina to the screen. 2:07 Let's also remove this escape character, now let's run the script. 2:16 I get an error because PHP reaches the second quote and 2:23 assumes that this is the end of the string. 2:26 I could surround that string with a single quote on the outside. 2:29 And this time when I run it, the string displays. 2:36 The script will run because the quotes are nested properly. 2:40 Meaning the single quotes are on the outside and 2:43 the double quotes are on the inside. 2:46 However, this doesn't work like I want because I see the variable name 2:48 instead of the value again. 2:52 Even though my variable is within these inner double quotes. 2:55 How the string is parsed is determined by the outside quotes, 2:58 so I need my outer quotes to be double quotes. 3:03 Then I use the escape character before these double quotes. 3:08 And this tells the PHP interpreter that the next 3:12 special character should be parsed as it is in that location. 3:15 Now let's run the script again. 3:21 It works like I want with the correct quotations and the variable value. 3:24 Before I escape, let me demonstrate using an alphanumeric escape sequence. 3:30 This line in the console would be much easier to read, 3:36 if it didn't run into the next command prompt. 3:39 I'd like to add a new line to the end of my string, 3:42 I can do that by adding the escape sequence /n. 3:45 /n stands for a new line and now when I run my script. 3:49 The command prompt is on the next line in my string is much easier to read. 3:55 Escape sequences are primarily only for double quoted strings, 4:01 since single quoted strings interpret each character individually. 4:06 The one place you would use an escape sequence 4:10 is to include a single quote within a single quoted string. 4:13 Like adding apostrophe s to the end of a word. 4:16 There are many more escape sequences and other ways to create a string. 4:20 So, be sure to check the teacher's notes for more information. 4:24
You need to sign up for Treehouse in order to download course files.Sign up