1 00:00:00,000 --> 00:00:04,641 [MUSIC] 2 00:00:04,641 --> 00:00:09,327 In the last section we started exploring the two basic pieces of programming, 3 00:00:09,327 --> 00:00:13,161 storage and retrieval of data, and the logic that tells what and 4 00:00:13,161 --> 00:00:16,150 when to do something with that data. 5 00:00:16,150 --> 00:00:20,670 You were introduced to variables and we use integers and float variables 6 00:00:20,670 --> 00:00:26,160 along with arithmetic and assignment operators, to create a unit converter. 7 00:00:26,160 --> 00:00:30,570 In this section we'll continue to expand upon these skills as we explore 8 00:00:30,570 --> 00:00:35,045 more data types and logic to create a daily exercise program. 9 00:00:35,045 --> 00:00:38,770 We'll store each exercise in a string variable, 10 00:00:38,770 --> 00:00:43,240 then we'll use conditionals to control which exercise is displayed. 11 00:00:43,240 --> 00:00:46,030 Let's start by looking at the string variable. 12 00:00:46,030 --> 00:00:51,430 A string is a series of characters typically specified within quotations. 13 00:00:51,430 --> 00:00:55,700 You can use either single or double quotes to create a string but 14 00:00:55,700 --> 00:00:57,430 they work differently. 15 00:00:57,430 --> 00:01:02,170 Single quoted strings, for the most part represent each individual character 16 00:01:02,170 --> 00:01:05,080 exactly as it is stored in the string. 17 00:01:05,080 --> 00:01:09,270 Double quoted strings on the other hand let you place variables inside the string. 18 00:01:09,270 --> 00:01:16,150 PHP will add the value of the variable or expand the variable within the string. 19 00:01:16,150 --> 00:01:18,800 This will make a lot more sense when you see it in action. 20 00:01:18,800 --> 00:01:23,370 So let's take a look at an example of storing and retrieving string variables. 21 00:01:24,910 --> 00:01:29,810 We're starting a new program, so let's create a new file and name it strings.php. 22 00:01:35,676 --> 00:01:39,440 Again, we need to start this file using the opening and closing PHP tags. 23 00:01:41,610 --> 00:01:43,800 Then we're ready to add our variables. 24 00:01:43,800 --> 00:01:47,050 We'll name this variable, string_one. 25 00:01:47,050 --> 00:01:49,190 Again, we'll use the assignment operator, 26 00:01:49,190 --> 00:01:53,640 a single equals sign followed by the value that we want to store. 27 00:01:53,640 --> 00:01:57,010 In this case, it will be a string surrounded by single quotes. 28 00:01:58,730 --> 00:02:05,430 For string one, let's set it equal to hello world. 29 00:02:05,430 --> 00:02:08,750 Now that the variable $string_one contains the value hello world. 30 00:02:08,750 --> 00:02:11,700 Let's retrieve that value using echo string one 31 00:02:15,540 --> 00:02:17,480 and now we can run the script in the console. 32 00:02:19,270 --> 00:02:21,820 Great, you've written your second hello world script. 33 00:02:22,910 --> 00:02:26,370 As I mentioned before, the primary difference between single and 34 00:02:26,370 --> 00:02:31,100 double quoted strings is whether variable names will be expanded. 35 00:02:31,100 --> 00:02:38,900 Let's add a variable called name we'll set this equal to our name. 36 00:02:38,900 --> 00:02:41,270 I'm gonna use my name, Alena. 37 00:02:41,270 --> 00:02:43,020 You can use your name. 38 00:02:43,020 --> 00:02:47,540 Then instead of saying hello world, let's say hello Alena using the name. 39 00:02:47,540 --> 00:02:52,550 Save the file and run the script again. 40 00:02:53,890 --> 00:02:58,680 Notice that the actual variable name is displayed instead of its value. 41 00:02:58,680 --> 00:03:02,860 That's because variables are not expanded within single quotes. 42 00:03:02,860 --> 00:03:05,445 Let's change these to double quotes and run the script again. 43 00:03:12,985 --> 00:03:16,530 Now it works just like I want and shows hello Alena. 44 00:03:17,800 --> 00:03:22,440 By using double quotes the PHP interpreter expands the variable 45 00:03:22,440 --> 00:03:27,810 showing us the value of the name variable instead of the variable name itself. 46 00:03:27,810 --> 00:03:31,140 If you want the value of a variable within your string 47 00:03:31,140 --> 00:03:33,530 make sure that you use double quotes. 48 00:03:33,530 --> 00:03:37,790 There are a few other differences in the way that PHP interprets characters in a string. 49 00:03:37,790 --> 00:03:42,260 In the next video, we'll explore the use of escape sequences.