1 00:00:00,000 --> 00:00:02,000 [Section 8] [Less Comfortable] 2 00:00:02,000 --> 00:00:04,000 [Nate Hardison] [Harvard University] 3 00:00:04,000 --> 00:00:08,000 [This is CS50.] [CS50.TV] 4 00:00:08,000 --> 00:00:11,000 >> Welcome to our second to last section. 5 00:00:11,000 --> 00:00:13,000 In this week we're going to talk about PHP, and then next week 6 00:00:13,000 --> 00:00:17,000 we'll do a little review for Quiz 1. 7 00:00:17,000 --> 00:00:20,000 We're going to start on page 3 of the problem sets pack, 8 00:00:20,000 --> 00:00:28,000 and we'll work through the section of questions fairly quickly. 9 00:00:28,000 --> 00:00:34,000 On page 3, we start talking about this difference between C and PHP 10 00:00:34,000 --> 00:00:38,000 in the sense that PHP is a dynamically typed language, 11 00:00:38,000 --> 00:00:42,000 whereas C is statically typed, 12 00:00:42,000 --> 00:00:46,000 and has anybody really quickly done any coding in something like PHP 13 00:00:46,000 --> 00:00:51,000 or Python or Ruby before, JavaScript? 14 00:00:51,000 --> 00:00:53,000 Totally new? Okay. 15 00:00:53,000 --> 00:00:56,000 >> All of these languages that I just mentioned 16 00:00:56,000 --> 00:01:01,000 are very different from languages like C and C++ and Java 17 00:01:01,000 --> 00:01:04,000 in the sense that when you declare a variable you don't have to declare 18 00:01:04,000 --> 00:01:09,000 what kind of variable it is, so in C we were stuck having to always say 19 00:01:09,000 --> 00:01:13,000 this is going to be an int variable, or this is going to be a char * variable, 20 00:01:13,000 --> 00:01:16,000 whereas with PHP, with Python, with Ruby, 21 00:01:16,000 --> 00:01:20,000 with a lot of these more modern languages you don't have to do that at all. 22 00:01:20,000 --> 00:01:24,000 You just declare your variable, and in the case of PHP 23 00:01:24,000 --> 00:01:28,000 you declare that it's a variable by prefixing it with a $ sign, 24 00:01:28,000 --> 00:01:35,000 and then the type of the variable is determined by whatever value it's holding, 25 00:01:35,000 --> 00:01:38,000 which is kind of cool. 26 00:01:38,000 --> 00:01:44,000 To play around with this a little bit, we have sample PHP code right here 27 00:01:44,000 --> 00:01:47,000 in between the start and end tags. 28 00:01:47,000 --> 00:01:53,000 We're using this kind of like HTML in the sense that 29 00:01:53,000 --> 00:01:56,000 we've got these angle brackets here to open and close. 30 00:01:56,000 --> 00:01:58,000 You'll see this with HTML. 31 00:01:58,000 --> 00:02:03,000 You'll see the same sort of syntax with XML too. 32 00:02:03,000 --> 00:02:06,000 >> This indicates the beginning of a PHP block. 33 00:02:06,000 --> 00:02:09,000 This ?php is specific to PHP. 34 00:02:09,000 --> 00:02:16,000 We close everything with a ?> as well. 35 00:02:16,000 --> 00:02:22,000 Let's open up a file in our appliance using whatever you'd like. 36 00:02:22,000 --> 00:02:25,000 In this packet it says to call dynamic.php. 37 00:02:25,000 --> 00:02:29,000 In truth, you can call it whatever you'd like, but the file name will come up 38 00:02:29,000 --> 00:02:34,000 and be important later, so just remember whatever you call it. 39 00:02:34,000 --> 00:02:36,000 I'm going to switch over to the appliance, 40 00:02:36,000 --> 00:02:43,000 and I'm going to make a directory for 41 00:02:43,000 --> 00:02:48,000 section 9 I believe we're at now. 42 00:02:48,000 --> 00:02:51,000 And we'll go into section 9, 43 00:02:51,000 --> 00:02:54,000 and I'm going to use gedit. 44 00:02:54,000 --> 00:02:59,000 Let's see, gedit, and I'm going to call mine dynamic.php. 45 00:02:59,000 --> 00:03:03,000 Boom. 46 00:03:03,000 --> 00:03:06,000 Within dynamic.php, since this is a PHP file 47 00:03:06,000 --> 00:03:09,000 and I want to run this through the PHP interpreter 48 00:03:09,000 --> 00:03:12,000 I need to start everything off with those open tags, 49 00:03:12,000 --> 00:03:17,000 so I'm going to start those right away, 50 00:03:17,000 --> 00:03:23,000 give myself a little space in between the tags, 51 00:03:23,000 --> 00:03:26,000 and then the point of this exercise was to play around with 52 00:03:26,000 --> 00:03:34,000 this function called gettype, so gettype, this function right here. 53 00:03:34,000 --> 00:03:38,000 >> Notice that there's no underscore or space or hyphen or anything like that 54 00:03:38,000 --> 00:03:42,000 separating the 2 words, get and type, just one word altogether. 55 00:03:42,000 --> 00:03:46,000 But what this will do is if I pass in a variable to gettype, 56 00:03:46,000 --> 00:03:51,000 say $var or $x, 57 00:03:51,000 --> 00:03:55,000 this will tell me kind of in a debugging format 58 00:03:55,000 --> 00:03:59,000 what the name of that type is. 59 00:03:59,000 --> 00:04:03,000 We're going to experiment here. 60 00:04:03,000 --> 00:04:07,000 In particular, if you look back at the pset spec 61 00:04:07,000 --> 00:04:11,000 we've got a few different lines of code here. 62 00:04:11,000 --> 00:04:16,000 In the first line of code we initialize this variable, $var, 63 00:04:16,000 --> 00:04:21,000 to be equal to 7, and then we have this printf line 64 00:04:21,000 --> 00:04:25,000 that says hey, printf var is blank. 65 00:04:25,000 --> 00:04:29,000 Printf is one of the many ways we can print things in PHP. 66 00:04:29,000 --> 00:04:33,000 Often you'll see the echo function used instead of printf, 67 00:04:33,000 --> 00:04:40,000 but printf we're all familiar with having been coding in C for a while. 68 00:04:40,000 --> 00:04:42,000 What you can actually do is let's see if copy and paste works. 69 00:04:42,000 --> 00:04:46,000 I'm going to give this a try. 70 00:04:46,000 --> 00:04:52,000 We'll go back over to the appliance. 71 00:04:52,000 --> 00:05:00,000 Okay, and there we go. 72 00:05:00,000 --> 00:05:05,000 And spacing is a little funky, but here we go. 73 00:05:05,000 --> 00:05:08,000 We've got this code in here. 74 00:05:08,000 --> 00:05:11,000 >> [Student] Is it not good style to tab it? 75 00:05:11,000 --> 00:05:13,000 Sorry? 76 00:05:13,000 --> 00:05:15,000 [Student] Is it conventional to tab it? 77 00:05:15,000 --> 00:05:21,000 Right, so Sam asks if it's convention to tab it or not. 78 00:05:21,000 --> 00:05:23,000 Typically yes. 79 00:05:23,000 --> 00:05:26,000 In this case I have chosen not to. 80 00:05:26,000 --> 00:05:33,000 Honestly, you'll see different things among different programmers. 81 00:05:33,000 --> 00:05:40,000 PHP is typically used often in combination with HTML 82 00:05:40,000 --> 00:05:45,000 and other languages, and so sometimes the printing will look a little funky 83 00:05:45,000 --> 00:05:50,000 if you indent within PHP start and end tags. 84 00:05:50,000 --> 00:05:53,000 It really depends. 85 00:05:53,000 --> 00:05:56,000 In this case the copy and paste didn't paste the tabs in for me, 86 00:05:56,000 --> 00:06:02,000 but it definitely does, like you said, make it clearer. 87 00:06:02,000 --> 00:06:06,000 All right, if you save this code in dynamic.php 88 00:06:06,000 --> 00:06:10,000 then what you can do is come down to your terminal window where I am right down here 89 00:06:10,000 --> 00:06:17,000 in gedit, and to do the equivalent of compile 90 00:06:17,000 --> 00:06:20,000 using make and then running your code in C 91 00:06:20,000 --> 00:06:25,000 all you have to do with the PHP file is start up the PHP interpreter, 92 00:06:25,000 --> 00:06:30,000 which is a little program called PHP, 93 00:06:30,000 --> 00:06:35,000 and the argument you give to the interpreter is the file you want to interpret. 94 00:06:35,000 --> 00:06:40,000 In this case, dynamic.php. 95 00:06:40,000 --> 00:06:43,000 Whoops, where did I put it? 96 00:06:43,000 --> 00:06:52,000 Oh, I put it in section 9, and here I am. That's why. 97 00:06:52,000 --> 00:06:57,000 >> Now if I run that again 98 00:06:57,000 --> 00:07:02,000 we see that in my first gettype function call 99 00:07:02,000 --> 00:07:04,000 var is an integer. 100 00:07:04,000 --> 00:07:12,000 In the next one var is a string, and then in the third one var is a boolean. 101 00:07:12,000 --> 00:07:15,000 What's interesting about this is if we scroll back up we see that 102 00:07:15,000 --> 00:07:18,000 these are types similar to the ones that we had in C. 103 00:07:18,000 --> 00:07:22,000 We had ints in C. 104 00:07:22,000 --> 00:07:26,000 We sort of had strings in C, 105 00:07:26,000 --> 00:07:31,000 so strings weren't a totally legit type in the sense that 106 00:07:31,000 --> 00:07:37,000 our CS50 string was really what? 107 00:07:37,000 --> 00:07:39,000 Sorry? Missy? 108 00:07:39,000 --> 00:07:41,000 [Missy] Char *. 109 00:07:41,000 --> 00:07:43,000 Yeah, it was this char * we used. 110 00:07:43,000 --> 00:07:47,000 At the very beginning of the semester we were using the string type, 111 00:07:47,000 --> 00:07:52,000 but it was really a char * under the hood. 112 00:07:52,000 --> 00:07:58,000 We just type defined it, whereas in PHP these strings are an actual type. 113 00:07:58,000 --> 00:08:07,000 There's no more of this char * type stuff. 114 00:08:07,000 --> 00:08:10,000 >> You'll see that we can do many more things with strings 115 00:08:10,000 --> 00:08:14,000 much more easily in PHP than we could in C, 116 00:08:14,000 --> 00:08:17,000 and then finally, we have this boolean type, and the big difference here is that 117 00:08:17,000 --> 00:08:21,000 if we scroll back down we see that the name of these types 118 00:08:21,000 --> 00:08:28,000 are now integer, string, and boolean instead of int and bool, 119 00:08:28,000 --> 00:08:31,000 and as you pick up different programming languages, 120 00:08:31,000 --> 00:08:36,000 assuming that at some point in your lives you will come into contact with 121 00:08:36,000 --> 00:08:40,000 different ones you'll notice little quirks like this 122 00:08:40,000 --> 00:08:44,000 where C calls integers ints. 123 00:08:44,000 --> 00:08:46,000 Some call it integers. 124 00:08:46,000 --> 00:08:51,000 There are other terms that you'll run into. 125 00:08:51,000 --> 00:08:55,000 Big int we saw today in SQL. 126 00:08:55,000 --> 00:08:59,000 There are also number and then bool, boolean, 127 00:08:59,000 --> 00:09:03,000 all sorts of differences there. 128 00:09:03,000 --> 00:09:07,000 >> This isn't terribly interesting but now I'm going to give you a little bit of time— 129 00:09:07,000 --> 00:09:13,000 if we zoom back out—to work through this exercise at the bottom of page 3 130 00:09:13,000 --> 00:09:20,000 where it asks what types correspond to these values here at the bottom. 131 00:09:20,000 --> 00:09:24,000 We have 3.50. 132 00:09:24,000 --> 00:09:28,000 We have this thing right here, which is interesting. 133 00:09:28,000 --> 00:09:32,000 We haven't seen that before, though if you've been following along in lecture 134 00:09:32,000 --> 00:09:35,000 you probably already know what that is. 135 00:09:35,000 --> 00:09:40,000 Then we have this, which this is kind of funky. 136 00:09:40,000 --> 00:09:42,000 You recognize this. 137 00:09:42,000 --> 00:09:46,000 What is this in C? 138 00:09:46,000 --> 00:09:50,000 Sam? What would this give you in C? 139 00:09:50,000 --> 00:09:54,000 It would open a file called dynamic.php and make it readable. 140 00:09:54,000 --> 00:09:58,000 Yeah, and what would be the type of variable that we would assign this to?>>File star. 141 00:09:58,000 --> 00:10:02,000 Exactly, we would assign this to a file star. 142 00:10:02,000 --> 00:10:08,000 This is legitimate PHP too. 143 00:10:08,000 --> 00:10:12,000 Run that. See what happens when you pass that into gettype. 144 00:10:12,000 --> 00:10:19,000 And then also check out what null is, what that might be in PHP. 145 00:10:19,000 --> 00:10:21,000 I'll give you a couple minutes. 146 00:10:21,000 --> 00:10:24,000 You can literally copy and paste these values in, 147 00:10:24,000 --> 00:10:29,000 and then we'll do a little random call on you 148 00:10:29,000 --> 00:10:34,000 and see what you got. 149 00:10:34,000 --> 00:10:36,000 >> [Student] I have a question.< 00:10:40,000 In one of the lectures David said there's something you can put at the top of your file 151 00:10:40,000 --> 00:10:43,000 so that you don't have to type in PHP every time. 152 00:10:43,000 --> 00:10:45,000 What was that? 153 00:10:45,000 --> 00:10:50,000 He was probably talking about the pound bang, the hash bang. 154 00:10:50,000 --> 00:10:57,000 It's a directive that if you mark your file as executable 155 00:10:57,000 --> 00:11:01,000 then what it does is when you execute the file 156 00:11:01,000 --> 00:11:06,000 it will tell the shell program 157 00:11:06,000 --> 00:11:12,000 to use the PHP interpreter to interpret the contents of the file. 158 00:11:12,000 --> 00:11:16,000 The question being what was this special line 159 00:11:16,000 --> 00:11:20,000 that we could include at the top of our PHP files to not have to include 160 00:11:20,000 --> 00:11:26,000 this PHP line or this PHP command whenever we want to execute the file? 161 00:11:26,000 --> 00:11:31,000 And you can actually do this with a bunch of different files. 162 00:11:31,000 --> 00:11:37,000 I believe it depends on where the PHP binary is stored, 163 00:11:37,000 --> 00:11:43,000 but often usr/bin/php is a good place to start. 164 00:11:43,000 --> 00:11:45,000 You can figure this out exactly by going back down to your appliance 165 00:11:45,000 --> 00:11:50,000 and typing out which php. 166 00:11:50,000 --> 00:11:55,000 The which command figures out when you're executing binary 167 00:11:55,000 --> 00:12:01,000 or a command where the corresponding file is. 168 00:12:01,000 --> 00:12:06,000 This tells me that the PHP is really aliasing this binary file 169 00:12:06,000 --> 00:12:13,000 that's stored in usr/bin, which is where a lot of the system binaries are installed. 170 00:12:13,000 --> 00:12:21,000 For example, if I scroll up to the top, now that I've put this #! line in here 171 00:12:21,000 --> 00:12:27,000 I can scroll down, and I can try to run 172 00:12:27,000 --> 00:12:35,000 dynamic.php, but I get this error about permission being denied, 173 00:12:35,000 --> 00:12:42,000 and what that means is that this file is not currently giving executable permissions. 174 00:12:42,000 --> 00:12:46,000 It's not marked as a file that somebody can run. 175 00:12:46,000 --> 00:12:52,000 >> When you ran Make or Clang and you got an executable from it 176 00:12:52,000 --> 00:12:56,000 the compiler was smart enough to know that hey, I'm building an executable. 177 00:12:56,000 --> 00:13:00,000 You probably want to execute it, so it would automatically configure 178 00:13:00,000 --> 00:13:02,000 the permissions for you. 179 00:13:02,000 --> 00:13:06,000 This isn't the case, though, when you create a normal file from scratch, 180 00:13:06,000 --> 00:13:13,000 the reason being typically you don't want to have any file on your system be executable, 181 00:13:13,000 --> 00:13:15,000 so you actually have to go in and manually do it. 182 00:13:15,000 --> 00:13:22,000 The easiest way to do it is with the chmod command, 183 00:13:22,000 --> 00:13:28,000 and chmod +x says add the executable bit 184 00:13:28,000 --> 00:13:31,000 because there's the read bit, the write bit and the executable bit, 185 00:13:31,000 --> 00:13:35,000 r, w and x, 186 00:13:35,000 --> 00:13:39,000 and saying chmod +x will turn on the executable bit 187 00:13:39,000 --> 00:13:46,000 for then the file that we specify, which in this case will be dynamic.php. 188 00:13:46,000 --> 00:13:49,000 >> [Student] Is that turned on for everyone? 189 00:13:49,000 --> 00:13:52,000 Let's check this out, so how would I check to see the permissions? 190 00:13:52,000 --> 00:13:54,000 Do you know how I'd check to see the permissions of a file? 191 00:13:54,000 --> 00:13:56,000 [inaudible student response] 192 00:13:56,000 --> 00:13:58,000 Sorry?>>[Student] XSD. 193 00:13:58,000 --> 00:14:03,000 XSD gives us the file in hacks. 194 00:14:03,000 --> 00:14:09,000 Listing all the files, -l, will show me all of the files in my directory 195 00:14:09,000 --> 00:14:13,000 kind of in their long, verbose listing, and so here we see that 196 00:14:13,000 --> 00:14:17,000 this dynamic.php file 197 00:14:17,000 --> 00:14:23,000 was last modified November 5th at 4:21 PM. 198 00:14:23,000 --> 00:14:26,000 The owner of this file is jharvard. 199 00:14:26,000 --> 00:14:28,000 That's the user who is logged in. 200 00:14:28,000 --> 00:14:31,000 That's the user I am working as, 201 00:14:31,000 --> 00:14:36,000 and if you're also in the appliance you're also working as user jharvard. 202 00:14:36,000 --> 00:14:38,000 You're in the students group, 203 00:14:38,000 --> 00:14:44,000 and these are things that you'll see often enough. 204 00:14:44,000 --> 00:14:47,000 >> We can go into a lot of detail here, 205 00:14:47,000 --> 00:14:50,000 but for the most part what you want to look at when you're looking to see 206 00:14:50,000 --> 00:14:55,000 whether or not a file is executable is primarily 207 00:14:55,000 --> 00:14:58,000 the user who owns the file, the owner, 208 00:14:58,000 --> 00:15:03,000 so this jharvard, and then we have 209 00:15:03,000 --> 00:15:07,000 the permissions bits over here on the left, 210 00:15:07,000 --> 00:15:13,000 and the way to read this is that the last bit 211 00:15:13,000 --> 00:15:18,000 typically is used for marking whether or not—at least in this case 212 00:15:18,000 --> 00:15:21,000 this last bit will often be set to a d to indicate 213 00:15:21,000 --> 00:15:24,000 that the file is a directory and not just a normal file. 214 00:15:24,000 --> 00:15:28,000 Then the following 3 bits right here 215 00:15:28,000 --> 00:15:34,000 determine the permissions that the owner of the file has, 216 00:15:34,000 --> 00:15:38,000 so in this case jharvard, as the owner of the file, can read and write this file, 217 00:15:38,000 --> 00:15:42,000 but there's a dash saying that jharvard can't execute this file. 218 00:15:42,000 --> 00:15:46,000 The next 3 bits are for the group, so this is the students group, 219 00:15:46,000 --> 00:15:49,000 so if there were multiple users on my appliance, 220 00:15:49,000 --> 00:15:52,000 and we had multiple users as part of the students group, 221 00:15:52,000 --> 00:15:55,000 then they can all read this file, but they can't write it, 222 00:15:55,000 --> 00:15:59,000 and likewise anyone else, the world 223 00:15:59,000 --> 00:16:01,000 can only read this file as well. 224 00:16:01,000 --> 00:16:03,000 There's a good, long writeup about this, 225 00:16:03,000 --> 00:16:05,000 so you can read online. 226 00:16:05,000 --> 00:16:10,000 In the problem set spec we go into this in more detail. 227 00:16:10,000 --> 00:16:12,000 >> [Student] Is the 218 referring to the world? 228 00:16:12,000 --> 00:16:17,000 The 218 is—off the top of my head I'm forgetting, 229 00:16:17,000 --> 00:16:20,000 but no. 230 00:16:20,000 --> 00:16:29,000 Let's see. I am blanking on that right now. 231 00:16:29,000 --> 00:16:32,000 Back to what we were about to do with this chmod +x 232 00:16:32,000 --> 00:16:37,000 where we wanted to give dynamic.php executable permissions, 233 00:16:37,000 --> 00:16:40,000 and the question was whether or not this would give executable permissions to everyone 234 00:16:40,000 --> 00:16:44,000 or to just jharvard, and we can see this by 235 00:16:44,000 --> 00:16:48,000 running the commands and typing ls -l, 236 00:16:48,000 --> 00:16:51,000 and now we see that it's marked as executable. 237 00:16:51,000 --> 00:16:54,000 You see that there was a color change. 238 00:16:54,000 --> 00:16:57,000 Now dynamic.php is listed in green, 239 00:16:57,000 --> 00:17:01,000 and it looks like the answer to the question is what, Charlotte? 240 00:17:01,000 --> 00:17:04,000 [Charlotte] Only jharvard.>>Only jharvard, yeah. 241 00:17:04,000 --> 00:17:08,000 >> If we wanted to turn the executable bit on for everybody 242 00:17:08,000 --> 00:17:10,000 how might we do that? 243 00:17:10,000 --> 00:17:13,000 Any thoughts?>>A + x? 244 00:17:13,000 --> 00:17:15,000 Yeah, exactly. 245 00:17:15,000 --> 00:17:23,000 Charlotte said we can do chmod of a + x of dynamic.php, 246 00:17:23,000 --> 00:17:26,000 and now if we run ls -l we see that the executable bits 247 00:17:26,000 --> 00:17:30,000 are indeed turned on for everyone. 248 00:17:30,000 --> 00:17:37,000 And you can actually do the reverse of this, so you can turn it off for everyone 249 00:17:37,000 --> 00:17:41,000 using minus. 250 00:17:41,000 --> 00:17:46,000 Now it's turned off, and now we can turn it back on for jharvard 251 00:17:46,000 --> 00:17:52,000 so that we can actually run it now, 252 00:17:52,000 --> 00:17:58,000 and now you see when we run the code this special #! line at the top, 253 00:17:58,000 --> 00:18:04,000 #! line, told the shell, 254 00:18:04,000 --> 00:18:08,000 told our terminal hey, when this file is run, 255 00:18:08,000 --> 00:18:14,000 use /usr/bin/php to interpret this file 256 00:18:14,000 --> 00:18:20,000 and then print the output. 257 00:18:20,000 --> 00:18:22,000 [inaudible student question] 258 00:18:22,000 --> 00:18:25,000 Sure, let me scroll back up. Just like this. 259 00:18:25,000 --> 00:18:36,000 You'll see all of these directives start with this pound and then the exclamation point, 260 00:18:36,000 --> 00:18:39,000 sometimes called a shebang, hash bang. 261 00:18:39,000 --> 00:18:48,000 >> [Student] How can we run it with php dynamic.php before we make it executable? 262 00:18:48,000 --> 00:18:55,000 The question was how can we run this using the PHP binary 263 00:18:55,000 --> 00:18:58,000 while dynamic.php is not executable? 264 00:18:58,000 --> 00:19:01,000 This is super important because this is exactly how 265 00:19:01,000 --> 00:19:06,000 it's going to work when you write problem set 7. 266 00:19:06,000 --> 00:19:09,000 Most of the time PHP files are not directly executable. 267 00:19:09,000 --> 00:19:14,000 The way that works is because it's the PHP binary that's executable. 268 00:19:14,000 --> 00:19:16,000 The interpreter is the thing that's being executed, 269 00:19:16,000 --> 00:19:20,000 and so what it's doing is literally slurping in the entire contents 270 00:19:20,000 --> 00:19:25,000 of our dynamic.php file and going line by line 271 00:19:25,000 --> 00:19:29,000 and executing those commands, so it's using our PHP file 272 00:19:29,000 --> 00:19:31,000 as a list of instructions. 273 00:19:31,000 --> 00:19:36,000 It's not directly executing it. 274 00:19:36,000 --> 00:19:40,000 That's where we say that these files are interpreted at run time. 275 00:19:40,000 --> 00:19:46,000 It's a run time language instead of something that's determined at compile time, 276 00:19:46,000 --> 00:19:49,000 not a compiled language like C. 277 00:19:49,000 --> 00:19:54,000 >> Is there a way to get a run time language to act as if it's a compile time language, 278 00:19:54,000 --> 00:19:58,000 like if you have all the running done at the server 279 00:19:58,000 --> 00:20:00,000 rather than—you know what I mean? 280 00:20:00,000 --> 00:20:04,000 Yes, so the question is 281 00:20:04,000 --> 00:20:11,000 is there a way to get run time languages to act more like compile time types of languages? 282 00:20:11,000 --> 00:20:18,000 And there are, I mean, that is an active area of research for a lot of these companies. 283 00:20:18,000 --> 00:20:22,000 I believe Facebook has done a lot of work with PHP 284 00:20:22,000 --> 00:20:25,000 and compiling it down, making it faster, optimizing it 285 00:20:25,000 --> 00:20:29,000 since their site is built on PHP. 286 00:20:29,000 --> 00:20:34,000 If you've been following node.js at all, 287 00:20:34,000 --> 00:20:40,000 which is kind of a JavaScript interpreter 288 00:20:40,000 --> 00:20:44,000 to be able to run JavaScript outside of the browser, outside of your web browser, 289 00:20:44,000 --> 00:20:47,000 because traditionally JavaScript would just run inside of Firefox or Chrome, 290 00:20:47,000 --> 00:20:52,000 and it would be used to make cool animations happen on a web page 291 00:20:52,000 --> 00:20:55,000 and make your web page dynamic. 292 00:20:55,000 --> 00:20:59,000 That's been built on a lot of work that Google has done 293 00:20:59,000 --> 00:21:03,000 to make JavaScript and C++ 294 00:21:03,000 --> 00:21:09,000 bind together, so there's a lot of active research to 295 00:21:09,000 --> 00:21:13,000 get these languages to interact and optimize them, 296 00:21:13,000 --> 00:21:17,000 primarily because so many people can code in PHP and JavaScript 297 00:21:17,000 --> 00:21:19,000 because it's a lot easier. 298 00:21:19,000 --> 00:21:21,000 It's a lot nicer. You don't have pointers. 299 00:21:21,000 --> 00:21:27,000 You don't have types floating around. 300 00:21:27,000 --> 00:21:31,000 They wanted to gain the benefits of these compile time languages 301 00:21:31,000 --> 00:21:36,000 with all the type checking and the speed and the lower memory usage and all of that 302 00:21:36,000 --> 00:21:42,000 while still maintaining the flexibility and ease of use of these newer ones. 303 00:21:42,000 --> 00:21:46,000 >> Back to our original plan of attack. 304 00:21:46,000 --> 00:21:50,000 We've got a few of these questions here in our problem set spec. 305 00:21:50,000 --> 00:21:54,000 Let's go through them really quickly, and we'll go around the room. 306 00:21:54,000 --> 00:21:59,000 Charlotte, what type is 3.50? 307 00:21:59,000 --> 00:22:01,000 [Charlotte] That's a double.>>It's a double. 308 00:22:01,000 --> 00:22:03,000 Jimmy, what is this next one?>>An array. 309 00:22:03,000 --> 00:22:09,000 An array, awesome, and Jared, what is the fopen? 310 00:22:09,000 --> 00:22:11,000 [Jared] It's a resource.>>It's a resource. 311 00:22:11,000 --> 00:22:15,000 That's kind of a new one, not a file star, not a file. 312 00:22:15,000 --> 00:22:19,000 It's a resource is the type in PHP, 313 00:22:19,000 --> 00:22:24,000 and I'm sorry, Ella, the last null is what? 314 00:22:24,000 --> 00:22:26,000 Null. 315 00:22:26,000 --> 00:22:28,000 Null, how is it spelled in PHP?>>The same way. 316 00:22:28,000 --> 00:22:30,000 The same way, capitals?>>Yeah. 317 00:22:30,000 --> 00:22:33,000 Yeah, all right. 318 00:22:33,000 --> 00:22:35,000 Here we go, we've got a double, we've got an array, 319 00:22:35,000 --> 00:22:41,000 we've got a resource, and then we've got null. 320 00:22:41,000 --> 00:22:48,000 Let's see now have you seen— 321 00:22:48,000 --> 00:22:51,000 let's see, so now I guess what I also want to do is pull up 322 00:22:51,000 --> 00:22:56,000 this web page right here, this php.net/manual, 323 00:22:56,000 --> 00:23:01,000 so if you guys copy that and open up a web browser. 324 00:23:01,000 --> 00:23:07,000 I'm going to pull up Chrome, put that in. 325 00:23:07,000 --> 00:23:10,000 I want to show you this not just because 326 00:23:10,000 --> 00:23:13,000 we can talk all day about types and all that fun stuff 327 00:23:13,000 --> 00:23:19,000 but rather because this is the PHP manual, 328 00:23:19,000 --> 00:23:23,000 and there are a lot of PHP programmers out there. 329 00:23:23,000 --> 00:23:26,000 There are a lot of PHP websites out there, 330 00:23:26,000 --> 00:23:31,000 and as a result, there is a lot of documentation on PHP, 331 00:23:31,000 --> 00:23:36,000 and the manual, this php.net, is a really good place to go 332 00:23:36,000 --> 00:23:43,000 whenever you're having questions about what's the best way to do X in PHP 333 00:23:43,000 --> 00:23:45,000 or what does a function look like? 334 00:23:45,000 --> 00:23:49,000 Just kind of getting familiar with what this looks like because 335 00:23:49,000 --> 00:23:54,000 you'll be coming here often, a bit for problem set 7. 336 00:23:54,000 --> 00:23:57,000 >> If you end up doing a final project that uses PHP 337 00:23:57,000 --> 00:24:04,000 this will be a place you'll become very well acquainted with. 338 00:24:04,000 --> 00:24:09,000 Often the way people do this is they use Google to search for the site, 339 00:24:09,000 --> 00:24:16,000 and they don't use the search box that's up here in the top right, which is kind of tiny. 340 00:24:16,000 --> 00:24:19,000 If you're Googling around for something to do with PHP and you see 341 00:24:19,000 --> 00:24:24,000 one of the manual links pop up you can typically rely on that 342 00:24:24,000 --> 00:24:31,000 as a fairly good resource. 343 00:24:31,000 --> 00:24:34,000 Awesome, so out of these types, just out of curiosity, 344 00:24:34,000 --> 00:24:37,000 which ones haven't we seen? 345 00:24:37,000 --> 00:24:40,000 Anything we haven't seen here? 346 00:24:40,000 --> 00:24:44,000 [Student] Resource.>>We saw resource with the fopen call. 347 00:24:44,000 --> 00:24:48,000 [Student] Objects.>>Objects we haven't seen for sure. 348 00:24:48,000 --> 00:24:51,000 Callbacks we haven't seen. 349 00:24:51,000 --> 00:24:54,000 There are some of these pseudo-types. 350 00:24:54,000 --> 00:24:57,000 We definitely only saw integer and double. 351 00:24:57,000 --> 00:25:01,000 We didn't see some of the others, so if we click on integers 352 00:25:01,000 --> 00:25:08,000 we can see are there any other integers that they have here? 353 00:25:08,000 --> 00:25:11,000 Are they all—so integer, integer, integer. 354 00:25:11,000 --> 00:25:14,000 Some languages have bigger integer types, 355 00:25:14,000 --> 00:25:23,000 like we saw on MySQL today there was int and then big int. 356 00:25:23,000 --> 00:25:25,000 Cool. 357 00:25:25,000 --> 00:25:28,000 >> So, PHP manual. 358 00:25:28,000 --> 00:25:37,000 Let's go back to our problem set spec, and we'll now scroll down to page 4. 359 00:25:37,000 --> 00:25:41,000 One of the things that happens when you get these languages 360 00:25:41,000 --> 00:25:49,000 that don't have this static typing, so these languages where you have to declare 361 00:25:49,000 --> 00:25:52,000 the type of a variable up front 362 00:25:52,000 --> 00:26:00,000 is you get cases where you might start having variables of different types 363 00:26:00,000 --> 00:26:04,000 interacting with each other, 364 00:26:04,000 --> 00:26:09,000 and PHP does this thing where it tries to do 365 00:26:09,000 --> 00:26:13,000 what it considers to be the most sensible thing to do 366 00:26:13,000 --> 00:26:18,000 when you have 2 different types interact with each other. 367 00:26:18,000 --> 00:26:24,000 For example, if we look at these lines of code right here 368 00:26:24,000 --> 00:26:29,000 you see that we've got what happens when 369 00:26:29,000 --> 00:26:38,000 we try and add the string 1 to the integer 2. 370 00:26:38,000 --> 00:26:41,000 What happens if we try and add a string that is not a numeric value 371 00:26:41,000 --> 00:26:48,000 but rather actual characters, CS to the number 50? 372 00:26:48,000 --> 00:26:51,000 Then we'll see is there anything different that happens where 373 00:26:51,000 --> 00:26:56,000 instead of adding a string to a number we're adding a number to a string, 374 00:26:56,000 --> 00:27:00,000 and so on and so forth to the point where we're getting 375 00:27:00,000 --> 00:27:05,000 some kind of weird stuff right here where we've got 7 + true. 376 00:27:05,000 --> 00:27:09,000 What the heck does that mean? 377 00:27:09,000 --> 00:27:19,000 >> If you guys go ahead and paste some of this code into your appliance. 378 00:27:19,000 --> 00:27:27,000 You can keep it in dynamic.php. 379 00:27:27,000 --> 00:27:30,000 We'll see what happens. 380 00:27:41,000 --> 00:27:43,000 [Student] Just use print, not printf? 381 00:27:43,000 --> 00:27:52,000 Yeah, so you'll find that print is also a valid function for printing in PHP. 382 00:27:52,000 --> 00:27:54,000 There are many different ways of doing it. 383 00:27:54,000 --> 00:27:58,000 We'll see with a couple of the examples later on once we start talking 384 00:27:58,000 --> 00:28:02,000 about the unique problem that we're going to write 385 00:28:02,000 --> 00:28:06,000 and then the concentrations problem that we're going to write 386 00:28:06,000 --> 00:28:09,000 that even though we have fopen and fclose 387 00:28:09,000 --> 00:28:15,000 that's often not the simplest way to read in the contents of a file. 388 00:28:15,000 --> 00:28:21,000 PHP has a lot of these C-like holdovers. 389 00:28:21,000 --> 00:28:28,000 [Student] When I put in all 6 of those things I only get one number as an output. 390 00:28:28,000 --> 00:28:36,000 [Nate H.] When you put in all 6 of these things. 391 00:28:36,000 --> 00:28:39,000 Let's see is it because it's possibly— 392 00:28:39,000 --> 00:28:46,000 one thing is that these print calls are not terminated at the end with new lines. 393 00:28:46,000 --> 00:28:49,000 There's no new line separating each of these print calls, 394 00:28:49,000 --> 00:28:51,000 so maybe you're getting one large number, 395 00:28:51,000 --> 00:28:58,000 and it's really just amalgam of new line characters. 396 00:28:58,000 --> 00:29:00,000 [Student] Okay, how do I make sure— 397 00:29:00,000 --> 00:29:03,000 Well, there are a bunch of different ways. 398 00:29:03,000 --> 00:29:06,000 You could manually put in a print of a new line character, 399 00:29:06,000 --> 00:29:09,000 an echo of a new line character, 400 00:29:09,000 --> 00:29:12,000 print of new line. 401 00:29:12,000 --> 00:29:16,000 [Student] So echo is the same thing as printf? 402 00:29:16,000 --> 00:29:21,000 >> Printf is like C printf 403 00:29:21,000 --> 00:29:24,000 where you're printing a formatted string. 404 00:29:24,000 --> 00:29:32,000 You're supplying it the format string and then all the placeholder variables. 405 00:29:32,000 --> 00:29:39,000 It's often something that isn't used— 406 00:29:39,000 --> 00:29:42,000 let's see, as a disclaimer, 407 00:29:42,000 --> 00:29:48,000 I am less familiar with PHP than I am with other web languages, 408 00:29:48,000 --> 00:29:53,000 and when I have programmed in PHP I typically don't use printf 409 00:29:53,000 --> 00:29:59,000 because I find it faster to use the string interpolation capabilities that it has, 410 00:29:59,000 --> 00:30:04,000 which we'll go into and I'll show you in just a second, 411 00:30:04,000 --> 00:30:08,000 whereas in C we kind of have to do this hacky thing to get it to print out properly. 412 00:30:08,000 --> 00:30:16,000 You can actually put variables directly into strings in PHP. 413 00:30:16,000 --> 00:30:22,000 Printf is kind of overly long for what I usually do. Yes, Ella. 414 00:30:22,000 --> 00:30:25,000 [Ella] Generally if you get parse error does that mean— 415 00:30:25,000 --> 00:30:30,000 like on C it doesn't tell you exactly where the mistake is and what it is, 416 00:30:30,000 --> 00:30:34,000 so does that mean look through your entire code and figure it out? 417 00:30:34,000 --> 00:30:39,000 It's typically more targeted than that. 418 00:30:39,000 --> 00:30:48,000 I think in that case it was a little off, 419 00:30:48,000 --> 00:30:51,000 so I think in that case we were missing a semicolon. 420 00:30:51,000 --> 00:30:54,000 It was trying to make sense of everything, 421 00:30:54,000 --> 00:30:57,000 so like these interpreted languages, the interpreter is going to try 422 00:30:57,000 --> 00:31:01,000 and do its best to make everything work appropriately. 423 00:31:01,000 --> 00:31:04,000 >> You'll see, for example, in JavaScript 424 00:31:04,000 --> 00:31:10,000 you'll often—line statements end with a semicolon just as they do in PHP, 425 00:31:10,000 --> 00:31:13,000 just as they do in C. 426 00:31:13,000 --> 00:31:15,000 In JavaScript some of the JavaScript interpreters 427 00:31:15,000 --> 00:31:20,000 in a lot of the browsers will put in semicolons for you 428 00:31:20,000 --> 00:31:23,000 if you happen to be missing them. 429 00:31:23,000 --> 00:31:29,000 They'll try and accommodate for some sloppiness on your part. 430 00:31:29,000 --> 00:31:34,000 That's where it might be trying and trying and trying to make things work, 431 00:31:34,000 --> 00:31:36,000 and then finally it will get to a point where it says okay, 432 00:31:36,000 --> 00:31:38,000 I can't make things work on bailing, and that's where you'll get a line number 433 00:31:38,000 --> 00:31:44,000 that might seem a little off from the exact place. 434 00:31:44,000 --> 00:31:47,000 Okay, so let's go through this really quickly. 435 00:31:47,000 --> 00:31:50,000 We left off with Ella, so let's go over to Missy, 436 00:31:50,000 --> 00:31:54,000 and Missy, what does print of string 1 plus the number 2 give you? 437 00:31:54,000 --> 00:31:56,000 [Missy] 3.>>3. 438 00:31:56,000 --> 00:31:59,000 Does that make sense? Sort of? 439 00:31:59,000 --> 00:32:02,000 Does it give you a number? Does it give you a string? 440 00:32:02,000 --> 00:32:05,000 [Missy] A number.>>It's a number. 441 00:32:05,000 --> 00:32:09,000 But it's printing it, so it's going to give you some sort of string. 442 00:32:09,000 --> 00:32:12,000 >> One thing we can do to check this out 443 00:32:12,000 --> 00:32:21,000 is if we do $var = 1 + 2 444 00:32:21,000 --> 00:32:28,000 and then we say echo of gettype, 445 00:32:28,000 --> 00:32:33,000 so we're using a third kind of printing here. 446 00:32:33,000 --> 00:32:39,000 Now we can see what happens here. 447 00:32:39,000 --> 00:32:42,000 Here what we've got is we got an integer out of this. 448 00:32:42,000 --> 00:32:45,000 Even though we were adding this string to a number, 449 00:32:45,000 --> 00:32:48,000 we didn't get a string out of it, just like Missy said. 450 00:32:48,000 --> 00:32:52,000 We were getting an integer. 451 00:32:52,000 --> 00:32:57,000 Okay, so let's see, next up, Kevin. 452 00:32:57,000 --> 00:32:59,000 CS + 50?>>[Kevin] 50. 453 00:32:59,000 --> 00:33:02,000 [Nate H.] 50. Does that make sense? 454 00:33:02,000 --> 00:33:04,000 [Kevin] Yeah. 455 00:33:04,000 --> 00:33:06,000 [Nate H.] Why? Why does it make sense to you? 456 00:33:06,000 --> 00:33:11,000 [Kevin] Because it's just having the string, having number value zero. 457 00:33:11,000 --> 00:33:16,000 Yeah, great. 458 00:33:16,000 --> 00:33:20,000 Kind of ambiguous situations, but it's good to know what happens. 459 00:33:20,000 --> 00:33:25,000 Stella, what happens next with number 1 + string 2? 460 00:33:25,000 --> 00:33:27,000 [Stella] 3.>>3 again. 461 00:33:27,000 --> 00:33:30,000 And in this case, do we get a string or a number? 462 00:33:30,000 --> 00:33:36,000 >> Let's give this a try. 463 00:33:36,000 --> 00:33:43,000 Anybody faster than me get the answer? Charlotte? 464 00:33:43,000 --> 00:33:46,000 Oh, I didn't—okay, let's see, we're going to do the same sort of thing 465 00:33:46,000 --> 00:33:49,000 where we've got a number plus a string, and we're going to echo the type, 466 00:33:49,000 --> 00:33:51,000 see what we get. 467 00:33:51,000 --> 00:33:55,000 We also get an integer. 468 00:33:55,000 --> 00:33:58,000 It doesn't matter which one is the string, which one is the number. 469 00:33:58,000 --> 00:34:00,000 We're still going to get an integer. 470 00:34:00,000 --> 00:34:04,000 It's still going to do what we might expect. 471 00:34:04,000 --> 00:34:09,000 All right, so Sam, what about 90 + 9 bottles of beer on the wall? 472 00:34:09,000 --> 00:34:12,000 [Sam] 99.>>99. 473 00:34:12,000 --> 00:34:15,000 No bottles of beer on the wall, though. 474 00:34:15,000 --> 00:34:17,000 It gives us a little more information about what's happening. 475 00:34:17,000 --> 00:34:21,000 [Sam] If you had written 9 in letters 476 00:34:21,000 --> 00:34:23,000 then you would have 90, right? 477 00:34:23,000 --> 00:34:26,000 [Nate H.] Yeah. 478 00:34:26,000 --> 00:34:30,000 The question was if we'd written 9 out as N-I-N-E 479 00:34:30,000 --> 00:34:34,000 would we have gotten 99 or 90? 480 00:34:34,000 --> 00:34:38,000 We'd get 90. 481 00:34:38,000 --> 00:34:42,000 It's literally just looking for digit characters. 482 00:34:42,000 --> 00:34:46,000 It's not smart enough to recognize number words and that stuff. Yes. 483 00:34:46,000 --> 00:34:48,000 >> [Student] Is there such a thing as typecasting in PHP? 484 00:34:48,000 --> 00:34:54,000 There is, and it's exactly the way you would do it in C. 485 00:34:54,000 --> 00:34:56,000 What about 10 / 7, Charlotte? 486 00:34:56,000 --> 00:34:58,000 [Charlotte] 1.4285. 487 00:34:58,000 --> 00:35:01,000 [Nate H.] Yeah, so what might be surprising about this? 488 00:35:01,000 --> 00:35:04,000 What would happen if you did this same sort of thing in C, 489 00:35:04,000 --> 00:35:06,000 if you did 10 / 7 in C? 490 00:35:06,000 --> 00:35:08,000 [Charlotte] It would only give you—depending on how you typecasted I guess 491 00:35:08,000 --> 00:35:12,000 it would give you only a limited number of digits after the decimal. 492 00:35:12,000 --> 00:35:16,000 Yeah, so a limited number of digits after the decimal. 493 00:35:16,000 --> 00:35:21,000 What else might be—would it give you any digits after the decimal? 494 00:35:21,000 --> 00:35:28,000 Often not, so again, depending on how you're typecasting it 495 00:35:28,000 --> 00:35:31,000 it may or may not convert it to a floating point number. 496 00:35:31,000 --> 00:35:35,000 Here it was kind of nice that it did. 497 00:35:35,000 --> 00:35:40,000 Had we shown you this back when we started doing this kind of stuff in C 498 00:35:40,000 --> 00:35:44,000 it probably would have made a little more sense 499 00:35:44,000 --> 00:35:48,000 that it doesn't just go to 1. 500 00:35:48,000 --> 00:35:51,000 And then finally, Jamie, what about 7 + true? 501 00:35:51,000 --> 00:35:53,000 [Jamie] 8.>>8. 502 00:35:53,000 --> 00:35:56,000 What does that mean? 503 00:35:56,000 --> 00:35:58,000 I guess it just gives true the value of 1. 504 00:35:58,000 --> 00:36:01,000 Yeah. What happens if we change that to false? 505 00:36:01,000 --> 00:36:04,000 [Student] 7. 506 00:36:04,000 --> 00:36:09,000 Yeah, so remember where we talk about these binary values, 507 00:36:09,000 --> 00:36:12,000 1 being on, 0 being off? 508 00:36:12,000 --> 00:36:17,000 Now we have true is 1, 0 is false, 509 00:36:17,000 --> 00:36:21,000 and you might not have seen this in the C examples that we've done this past semester, 510 00:36:21,000 --> 00:36:27,000 but historically the bool type in C hasn't been a real type, 511 00:36:27,000 --> 00:36:32,000 so people have used 0 and 1 in the place of true and false. 512 00:36:32,000 --> 00:36:35,000 This is a manifestation of that. 513 00:36:35,000 --> 00:36:39,000 >> Okay, so the one important part about all this 514 00:36:39,000 --> 00:36:41,000 is that we have these different types. 515 00:36:41,000 --> 00:36:43,000 They can interact with each other. 516 00:36:43,000 --> 00:36:49,000 They can often interact with each other in ways that are nice, as we've seen here. 517 00:36:49,000 --> 00:36:53,000 It's nice to be able to have string 1 and the number 2, add them together and get 3. 518 00:36:53,000 --> 00:36:55,000 That makes sense. 519 00:36:55,000 --> 00:36:59,000 However, when you're writing websites, and especially when you're processing 520 00:36:59,000 --> 00:37:04,000 user input, so say you've written a web form 521 00:37:04,000 --> 00:37:09,000 that collects information from the user and then goes to process it 522 00:37:09,000 --> 00:37:13,000 on the back end, on the server side in your PHP code 523 00:37:13,000 --> 00:37:18,000 if you're expecting that value that the user typed in and submitted 524 00:37:18,000 --> 00:37:23,000 to your form to be an integer or to be a floating point number or something like that 525 00:37:23,000 --> 00:37:29,000 you need to explicitly cast it 526 00:37:29,000 --> 00:37:34,000 and then do some type checking. 527 00:37:34,000 --> 00:37:41,000 >> You don't want to just rely on this type juggling system to make things work out, 528 00:37:41,000 --> 00:37:48,000 especially for security reasons and just for the robustness of your website. 529 00:37:48,000 --> 00:37:51,000 Just something to keep in mind that whenever you're handling form data, 530 00:37:51,000 --> 00:37:57,000 anything that comes in the post or the get superglobals 531 00:37:57,000 --> 00:38:01,000 make sure that you always validate it and convert it and typecast it. 532 00:38:01,000 --> 00:38:04,000 And just like we were talking about a moment ago that typecasting 533 00:38:04,000 --> 00:38:08,000 in PHP is exactly the same as in C where you have the parentheses 534 00:38:08,000 --> 00:38:14,000 and then the type. 535 00:38:14,000 --> 00:38:16,000 Keep that in mind. 536 00:38:16,000 --> 00:38:19,000 One function that will come in handy when you're doing this 537 00:38:19,000 --> 00:38:22,000 is we've been using—and this is kind of as an aside— 538 00:38:22,000 --> 00:38:27,000 we've been using this gettype function right here 539 00:38:27,000 --> 00:38:30,000 to figure out the type of a variable, 540 00:38:30,000 --> 00:38:37,000 and while this is handy for debugging and to see what a variable's type is 541 00:38:37,000 --> 00:38:42,000 you don't want to use this, for example, in a condition where 542 00:38:42,000 --> 00:38:57,000 you're checking to see if gettype of $var = integer do something. 543 00:38:57,000 --> 00:39:01,000 This is bad, bad, bad. 544 00:39:01,000 --> 00:39:09,000 Instead there are these functions called is integer, is string, 545 00:39:09,000 --> 00:39:14,000 is array that you want to use instead, so in this case 546 00:39:14,000 --> 00:39:18,000 what I would want to do instead of this guy right here 547 00:39:18,000 --> 00:39:31,000 is use the is integer var. 548 00:39:31,000 --> 00:39:37,000 And they're often referred is is_* in the sense that you could replace the * with integer, 549 00:39:37,000 --> 00:39:43,000 string, et cetera, and just to make sure really quick 550 00:39:43,000 --> 00:39:50,000 is this is int php. 551 00:39:50,000 --> 00:39:59,000 Looks like you can do either is int or is there is integer as well? 552 00:39:59,000 --> 00:40:06,000 Yes, alias, so int integer aliases. 553 00:40:06,000 --> 00:40:08,000 Cool. 554 00:40:14,000 --> 00:40:17,000 >> How are we doing? 555 00:40:17,000 --> 00:40:20,000 Let's pick up the pace a little bit. 556 00:40:20,000 --> 00:40:24,000 Now we're going to talk about arrays, so as you can see 557 00:40:24,000 --> 00:40:28,000 in the next part of the spec we talk about how arrays in PHP 558 00:40:28,000 --> 00:40:31,000 are slightly different than they are in C. 559 00:40:31,000 --> 00:40:34,000 In truth, they're kind of an amalgam of 560 00:40:34,000 --> 00:40:36,000 the arrays that you've come to know and love in C 561 00:40:36,000 --> 00:40:40,000 where everything is of the same type 562 00:40:40,000 --> 00:40:43,000 stored consecutively and contiguously in memory, 563 00:40:43,000 --> 00:40:46,000 and you have these numeric indices. 564 00:40:46,000 --> 00:40:49,000 You have index 0, index 1, index 2, 565 00:40:49,000 --> 00:40:53,000 and you store values at those indices. 566 00:40:53,000 --> 00:40:57,000 You also in C, when you wrote Speller, a lot of you 567 00:40:57,000 --> 00:41:00,000 did the hash table approach, and you saw that there we had 568 00:41:00,000 --> 00:41:03,000 a different kind of storage where we were mapping 569 00:41:03,000 --> 00:41:07,000 a key to a value, so when you tried to store something in hash table 570 00:41:07,000 --> 00:41:11,000 you had to specify that you wanted to store it with a specific key, 571 00:41:11,000 --> 00:41:17,000 and that key determined the location of the value and where it would be stored. 572 00:41:17,000 --> 00:41:22,000 You've kind of got both of those concepts happening at the same time in a PHP array, 573 00:41:22,000 --> 00:41:27,000 and as a result, we often call these associative arrays 574 00:41:27,000 --> 00:41:33,000 where we are associating a key in a value. 575 00:41:33,000 --> 00:41:39,000 In this next part we talk about a simple PHP array 576 00:41:39,000 --> 00:41:45,000 where we have keys a, b, and c, 577 00:41:45,000 --> 00:41:53,000 all strings, mapping to the integers 1, 2, and 3. 578 00:41:53,000 --> 00:41:57,000 And you can have keys of different types. 579 00:41:57,000 --> 00:42:00,000 >> You can have some keys that are strings, some keys that are integers. 580 00:42:00,000 --> 00:42:03,000 You can have values of different types. 581 00:42:03,000 --> 00:42:06,000 You can have a value that's a string, a value that's an integer, 582 00:42:06,000 --> 00:42:11,000 a value that's an object or another array, for example, 583 00:42:11,000 --> 00:42:16,000 all in the same array object, which is kind of weird. 584 00:42:16,000 --> 00:42:20,000 You don't have to have an array that's just got one type of element in it. 585 00:42:20,000 --> 00:42:26,000 You can have many different things going on in there. 586 00:42:26,000 --> 00:42:33,000 The other thing to note is that when you do see something like this in your code, 587 00:42:33,000 --> 00:42:38,000 which is valid PHP to declare an array just like this, 0, 1, 2, 3, 4, 5, 588 00:42:38,000 --> 00:42:43,000 that will declare an initialized $arr to be this array. 589 00:42:43,000 --> 00:42:48,000 But what you're actually getting under the hood is this kind of implicit 590 00:42:48,000 --> 00:42:55,000 generation of keys where we've got 0 to 0, 1 to 1, 2 to 2, 3 to 3. 591 00:42:55,000 --> 00:42:59,000 And it turns out that even if you do have an array like this guy up here 592 00:42:59,000 --> 00:43:03,000 where you have a, b and c as the keys 593 00:43:03,000 --> 00:43:09,000 and then you start using the array push method to start using this array like a stack, 594 00:43:09,000 --> 00:43:12,000 so you can see that this array object, this array is really becoming overloaded. 595 00:43:12,000 --> 00:43:15,000 We can use it as an array. We can use it as a hash table. 596 00:43:15,000 --> 00:43:19,000 We can use it as a stack. 597 00:43:19,000 --> 00:43:22,000 When you start pushing things onto this array 598 00:43:22,000 --> 00:43:30,000 the first thing you push onto this will be index 0 and then index 1 and index 2. 599 00:43:30,000 --> 00:43:33,000 You can get this kind of implicit generation of keys 600 00:43:33,000 --> 00:43:38,000 unless you specify them explicitly. 601 00:43:38,000 --> 00:43:43,000 The way you specify keys explicitly, of course, is by using this bracket notation, 602 00:43:43,000 --> 00:43:48,000 which is similar to arrays except instead of only allowing 603 00:43:48,000 --> 00:43:54,000 integer indices in here, now we allow anything. 604 00:43:54,000 --> 00:43:57,000 If you want your key to be a string, you'd specify it like this. 605 00:43:57,000 --> 00:44:06,000 If you want it to be an int, you specify it with the int you want to use. 606 00:44:06,000 --> 00:44:13,000 >> Questions on that so far? 607 00:44:13,000 --> 00:44:18,000 One of the nice things about this idea 608 00:44:18,000 --> 00:44:26,000 is that you can only ever have one value stored with a particular key. 609 00:44:26,000 --> 00:44:32,000 If we go back over to our appliance—let me delete some of this stuff. 610 00:44:32,000 --> 00:44:47,000 And let's say I initialize an array to be 0, 1, 2, done. 611 00:44:47,000 --> 00:44:55,000 If I now know that, for example, if I echo $arr[0] 612 00:44:55,000 --> 00:44:59,000 I'm going to get the value 0 printed out, 613 00:44:59,000 --> 00:45:05,000 and since there can only ever be one value stored for a particular key 614 00:45:05,000 --> 00:45:11,000 if I store something at $arr[0], say a, 615 00:45:11,000 --> 00:45:16,000 then I know when I echo $arr[0] again 616 00:45:16,000 --> 00:45:19,000 I'm not going to get 0 printed out as before. 617 00:45:19,000 --> 00:45:24,000 I'm only going to get a. 618 00:45:24,000 --> 00:45:30,000 So this is basically saying that I can't have $arr[0] storing 2 different values. 619 00:45:30,000 --> 00:45:34,000 It can't store both 0 and the string a, like this literally replaces 620 00:45:34,000 --> 00:45:38,000 what was at $arr[0] previously. 621 00:45:38,000 --> 00:45:43,000 The reason I bring this up is run it, see what happens. 622 00:45:43,000 --> 00:45:47,000 See here that I got 0 printed out and then a down here. 623 00:45:47,000 --> 00:45:51,000 There's no new line there because I was lazy and didn't put that in. 624 00:45:51,000 --> 00:45:54,000 What's cool about this is we can use this as a way 625 00:45:54,000 --> 00:46:00,000 to capture this idea of a set 626 00:46:00,000 --> 00:46:07,000 where we can't have multiple keys within an array. 627 00:46:07,000 --> 00:46:10,000 We can't have identical keys within an array. 628 00:46:10,000 --> 00:46:17,000 I can't have key 0 and value 1 and key 0 and value a 629 00:46:17,000 --> 00:46:21,000 or key 0 and value true. 630 00:46:21,000 --> 00:46:27,000 The keys are all—there's only 1 key in the array. 631 00:46:27,000 --> 00:46:32,000 Even though you can have the same value stored 632 00:46:32,000 --> 00:46:35,000 multiple times in the array under different keys 633 00:46:35,000 --> 00:46:42,000 it's not possible to have identical keys multiple times in your PHP array. 634 00:46:42,000 --> 00:46:47,000 >> If we look at this next problem, unique.php, 635 00:46:47,000 --> 00:46:51,000 where we want to open up a PHP file 636 00:46:51,000 --> 00:46:54,000 containing a list of strings, one string per line, 637 00:46:54,000 --> 00:47:01,000 and we want to find all of the unique strings in that file 638 00:47:01,000 --> 00:47:06,000 all we have to do is use one of these PHP arrays 639 00:47:06,000 --> 00:47:12,000 and use the strings in the file as the keys to this array 640 00:47:12,000 --> 00:47:17,000 and keep updating our array as we store these new keys. 641 00:47:17,000 --> 00:47:22,000 As we read each line out of the file we can store it in the array, 642 00:47:22,000 --> 00:47:28,000 and at the end we will have as our keys in our array 643 00:47:28,000 --> 00:47:34,000 all of the unique strings within the file. 644 00:47:34,000 --> 00:47:37,000 Does that make sense? 645 00:47:37,000 --> 00:47:41,000 Let's see how this works. 646 00:47:41,000 --> 00:47:49,000 We're going to open up, according to the spec, a new file called unique.php. 647 00:47:49,000 --> 00:47:58,000 Open. Oops, sorry, new file. 648 00:47:58,000 --> 00:48:03,000 We're going to start it off with the same start and end tags. 649 00:48:03,000 --> 00:48:17,000 We're going to save it in section 9, and we're going to call it unique.php. 650 00:48:17,000 --> 00:48:25,000 Okay, now zoom in. 651 00:48:25,000 --> 00:48:32,000 The idea here is open a file, 652 00:48:32,000 --> 00:48:38,000 read in file line by line. 653 00:48:38,000 --> 00:48:46,000 For each line in file 654 00:48:46,000 --> 00:48:51,000 we'll have an array where we have 655 00:48:51,000 --> 00:49:03,000 the line as our key. 656 00:49:03,000 --> 00:49:09,000 And then when we get to the end here 657 00:49:09,000 --> 00:49:17,000 $arr's keys are the unique lines 658 00:49:17,000 --> 00:49:25,000 from the file since we know that if I put line into this array 659 00:49:25,000 --> 00:49:33,000 multiple times it will just keep overriding the old value, 660 00:49:33,000 --> 00:49:46,000 and we can actually put array line in as itself just like that. 661 00:49:46,000 --> 00:49:48,000 >> This is kind of weird. 662 00:49:48,000 --> 00:49:52,000 We're storing the same key value pair over and over and over again, 663 00:49:52,000 --> 00:50:00,000 but since we are guaranteed that there will be only 1 key called line 664 00:50:00,000 --> 00:50:05,000 so if we have a file that says—a file of animal noises and it has woof, woof, 665 00:50:05,000 --> 00:50:11,000 meow, meow, moo, moo, and each time we read an animal noise out 666 00:50:11,000 --> 00:50:15,000 like woof and we store it in our array we get woof, and then the second time 667 00:50:15,000 --> 00:50:20,000 we store woof it will overwrite the first time that we stored it. 668 00:50:20,000 --> 00:50:25,000 In the end we'll only have one entry in the array 669 00:50:25,000 --> 00:50:33,000 for each of the animal noises in our animal noises file. 670 00:50:33,000 --> 00:50:41,000 Do you guys feel confident that you can tackle the opening of a file in PHP? 671 00:50:41,000 --> 00:50:45,000 One way to do it—let's go over this quickly—one way to do it 672 00:50:45,000 --> 00:50:50,000 is with fopen, like we saw earlier. 673 00:50:50,000 --> 00:50:55,000 You can fopen some_file.txt. 674 00:50:55,000 --> 00:50:59,000 You can open it in read mode, just like in C. 675 00:50:59,000 --> 00:51:02,000 That's one perfectly good way to do it. 676 00:51:02,000 --> 00:51:05,000 You also then for reading in the file line by line 677 00:51:05,000 --> 00:51:12,000 have the same functions, many of them, that you did in C. 678 00:51:12,000 --> 00:51:14,000 You have fgets. 679 00:51:14,000 --> 00:51:17,000 You have feof, though we don't like using that 680 00:51:17,000 --> 00:51:22,000 because, remember, that was not great in C. 681 00:51:22,000 --> 00:51:25,000 You can do it the same way, 682 00:51:25,000 --> 00:51:34,000 but here is a really cool thing. 683 00:51:34,000 --> 00:51:41,000 Sorry, I don't want to do $file, but there is a function called file in PHP, 684 00:51:41,000 --> 00:51:47,000 and this function right here is cool because it reads the entire contents 685 00:51:47,000 --> 00:51:52,000 of the file that you specify, so some_file.txt, 686 00:51:52,000 --> 00:51:56,000 reads the entire contents of this file into an array 687 00:51:56,000 --> 00:52:03,000 and then lets you iterate over it, which is pretty nifty. 688 00:52:03,000 --> 00:52:07,000 >> If we go, for example, to our web browser 689 00:52:07,000 --> 00:52:14,000 and we look at Google for PHP file 690 00:52:14,000 --> 00:52:23,000 you can see here that our manual says that file reads entire file into an array, 691 00:52:23,000 --> 00:52:27,000 and we can file_get_contents to return the contents of a file as a string, 692 00:52:27,000 --> 00:52:30,000 but typically just getting it as an array is really nice because what it does 693 00:52:30,000 --> 00:52:33,000 is it breaks it up so that each element 694 00:52:33,000 --> 00:52:37,000 in the array is one line of the file, 695 00:52:37,000 --> 00:52:41,000 so if we look at file 0, that's the first line of the file. 696 00:52:41,000 --> 00:52:46,000 File 1, second line, file 2, third line, and so on and so on. 697 00:52:46,000 --> 00:52:50,000 Wouldn't it be nice if that was all you had to do in C? 698 00:52:50,000 --> 00:52:53,000 Pretty nifty. 699 00:52:53,000 --> 00:52:59,000 David showed this in lecture, and the idiom he showed was 700 00:52:59,000 --> 00:53:03,000 that in addition to our standard for loop—in PHP we had that 701 00:53:03,000 --> 00:53:09,000 for ($i = 0; i < 10; i++), 702 00:53:09,000 --> 00:53:16,000 and you can do this in PHP too, same thing— 703 00:53:16,000 --> 00:53:27,000 we also have this cool construct called foreach. 704 00:53:27,000 --> 00:53:32,000 Foreach is really handy when iterating over arrays or these data structures 705 00:53:32,000 --> 00:53:36,000 because it allows you to pull out each element of the array 706 00:53:36,000 --> 00:53:42,000 without having to manually do the indexing yourself, 707 00:53:42,000 --> 00:53:46,000 without having to manually create an index variable, increment it, 708 00:53:46,000 --> 00:53:51,000 pull out the value in the array at that point, because that's a very common thing to do. 709 00:53:51,000 --> 00:53:54,000 >> You probably have done that tons of times when you were doing C stuff 710 00:53:54,000 --> 00:54:00,000 over the semester, so with this foreach 711 00:54:00,000 --> 00:54:06,000 we can loop over this file array, 712 00:54:06,000 --> 00:54:12,000 and the syntax is that we want to now loop over this array 713 00:54:12,000 --> 00:54:18,000 and specify that the variable we're going to use to store 714 00:54:18,000 --> 00:54:27,000 the element of this array locally, local to the scope of this foreach loop, 715 00:54:27,000 --> 00:54:32,000 is we're going to call it line. 716 00:54:32,000 --> 00:54:38,000 If it's a file of just words and there's one word in a line 717 00:54:38,000 --> 00:54:43,000 we could call it word as well, really just you give this a name, 718 00:54:43,000 --> 00:54:49,000 whatever you want to call it, 719 00:54:49,000 --> 00:55:02,000 and then inside the loop you can do whatever you want with this variable line. 720 00:55:02,000 --> 00:55:08,000 If it's not enough to get the value of the array and you also want to get 721 00:55:08,000 --> 00:55:17,000 the index along with it you can specify a name for the index 722 00:55:17,000 --> 00:55:22,000 as well as the value, and now you have access to 2 variables. 723 00:55:22,000 --> 00:55:26,000 You have $i and line 724 00:55:26,000 --> 00:55:30,000 where $i is the index in the array, 725 00:55:30,000 --> 00:55:38,000 and line is the line that you retrieved from the file. 726 00:55:38,000 --> 00:55:49,000 For example, if we wanted to print out echo line 0 of the file as this, 727 00:55:49,000 --> 00:55:57,000 we could do it just like this, "Line $i of the file is $line," 728 00:55:57,000 --> 00:56:02,000 and here is something we also haven't seen yet either 729 00:56:02,000 --> 00:56:13,000 where I've just totally skipped over this whole %s %d business 730 00:56:13,000 --> 00:56:16,000 that we had to deal with in C, and instead I've gone straight to 731 00:56:16,000 --> 00:56:19,000 writing the variables in line in my string. 732 00:56:19,000 --> 00:56:23,000 >> This is called variable interpolation, string interpolation 733 00:56:23,000 --> 00:56:26,000 where you're stuffing the variables right in, and the PHP interpreter 734 00:56:26,000 --> 00:56:31,000 is smart enough when it's reading through a string that begins with double quotes— 735 00:56:31,000 --> 00:56:34,000 not single quotes, with single quoted strings you can't do this— 736 00:56:34,000 --> 00:56:38,000 but with double quoted strings as it reads through it's looking for variables. 737 00:56:38,000 --> 00:56:42,000 It's hunting them down, and if it sees variables it will take the value of the variable 738 00:56:42,000 --> 00:56:47,000 and stuff it into the string if it can convert it into a string representation, 739 00:56:47,000 --> 00:56:51,000 which is pretty nifty. 740 00:56:51,000 --> 00:56:56,000 For now, let's comment out the rest of everything, 741 00:56:56,000 --> 00:57:00,000 save this, and what we can do now is we can open up a file 742 00:57:00,000 --> 00:57:06,000 that we can call some_file.txt—let's create a new file— 743 00:57:06,000 --> 00:57:15,000 and we can put in a bunch of nonsense stuff in here just to test everything out, 744 00:57:15,000 --> 00:57:25,000 save it, call it some_file.txt, as I'm doing up here at the top, 745 00:57:25,000 --> 00:57:33,000 and now if I zoom out just to make sure everything is in the same directory— 746 00:57:33,000 --> 00:57:36,000 it looks like I have unique.php and some_file.txt in the same directory. 747 00:57:36,000 --> 00:57:42,000 If I run php unique.php 748 00:57:42,000 --> 00:57:51,000 see how it prints out each line in my file and what the line is? 749 00:57:51,000 --> 00:57:56,000 >> That's pretty powerful, right? 750 00:57:56,000 --> 00:58:03,000 Look, it took 3 lines of code to open up a file. 751 00:58:03,000 --> 00:58:08,000 Well, 4 lines of code. I can't count today, clearly. 752 00:58:08,000 --> 00:58:13,000 But really just 2 interesting lines of code, because the other 2 were the curly braces, 753 00:58:13,000 --> 00:58:17,000 but in this much code we were able to open a file, iterate through it, 754 00:58:17,000 --> 00:58:20,000 pull out the line number and the line itself 755 00:58:20,000 --> 00:58:24,000 and print it out. 756 00:58:24,000 --> 00:58:26,000 Cool stuff. Charlotte. 757 00:58:26,000 --> 00:58:28,000 [Charlotte] I have a question about the syntax. 758 00:58:28,000 --> 00:58:33,000 So foreach deals with every single line of the file that you open, 759 00:58:33,000 --> 00:58:37,000 and then when you want to do things with every single line you just do it as 760 00:58:37,000 --> 00:58:39,000 and then associate the value. 761 00:58:39,000 --> 00:58:41,000 [Nate H.] What you can do right here—the question was 762 00:58:41,000 --> 00:58:46,000 the foreach has to do with the array, so the foreach syntax 763 00:58:46,000 --> 00:58:51,000 is really foreach, and notice that there's no space 764 00:58:51,000 --> 00:58:53,000 or anything between the for and the each. 765 00:58:53,000 --> 00:58:56,000 They have to go right next to each other, 766 00:58:56,000 --> 00:59:00,000 and then it takes in an array, 767 00:59:00,000 --> 00:59:05,000 and then you have this other keyword called as that has to be there, 768 00:59:05,000 --> 00:59:11,000 and then after the as you can either put one variable name, 769 00:59:11,000 --> 00:59:14,000 in which case you're going to be pulling out the values of the array, 770 00:59:14,000 --> 00:59:20,000 not the indices, or if you do it as we've written below 771 00:59:20,000 --> 00:59:24,000 you get the keys and the values. 772 00:59:24,000 --> 00:59:33,000 You have foreach element of the array or pair of the array as keyed to value 773 00:59:33,000 --> 00:59:38,000 or as just value. 774 00:59:38,000 --> 00:59:40,000 Depending on what you need, if you don't need the keys, 775 00:59:40,000 --> 00:59:42,000 then you can go with the value. 776 00:59:42,000 --> 00:59:45,000 If you want the keys you can go with them too. 777 00:59:45,000 --> 00:59:48,000 >> [Charlotte] I also realized we never declared the i or line. 778 00:59:48,000 --> 00:59:51,000 How does it even know what they are? 779 00:59:51,000 --> 00:59:53,000 [Nate H.] What do you mean by declare? 780 00:59:53,000 --> 00:59:57,000 [Charlotte] We never told them what i or line means. 781 00:59:57,000 --> 01:00:01,000 [Nate H.] We never told the computer what i or line means in terms of— 782 01:00:01,000 --> 01:00:05,000 [Charlotte] That they're indexed or that they're— 783 01:00:05,000 --> 01:00:09,000 [Nate H.] We never told it that this is supposed to be the index or the key and the value, 784 01:00:09,000 --> 01:00:13,000 and that's because that's the PHP interpreter. 785 01:00:13,000 --> 01:00:17,000 This serves as the declaration and says okay, the key is going to be 786 01:00:17,000 --> 01:00:20,000 a variable called i stored in a variable called i. 787 01:00:20,000 --> 01:00:24,000 The value is going to be stored in a variable called line, 788 01:00:24,000 --> 01:00:28,000 so this serves as the declaration of these variables 789 01:00:28,000 --> 01:00:35,000 and says $i is a key, and $line is a value. 790 01:00:35,000 --> 01:00:37,000 Yeah, Ella. 791 01:00:37,000 --> 01:00:41,000 [Ella] If the keys aren't done numerically 792 01:00:41,000 --> 01:00:44,000 how does it decide what order it's going to print everything? 793 01:00:44,000 --> 01:00:47,000 Is it just like the order it's entered in? 794 01:00:47,000 --> 01:00:51,000 [Nate H.] Let's give it a try. 795 01:00:51,000 --> 01:00:57,000 Let's create a variable called arr, 796 01:00:57,000 --> 01:01:06,000 and we can do a goes to 7. 797 01:01:06,000 --> 01:01:19,000 Let's say 0 goes to another array with 1, 2, or apple. 798 01:01:19,000 --> 01:01:30,000 Let's say 7 goes to 1, b goes to 2, 799 01:01:30,000 --> 01:01:39,000 and 3 goes to 4. 800 01:01:39,000 --> 01:01:44,000 This is kind of a crazy looking example because we're mixing up 801 01:01:44,000 --> 01:01:47,000 strings and integers all over the place. 802 01:01:47,000 --> 01:01:50,000 There's no real order to this array. 803 01:01:50,000 --> 01:01:55,000 I mean, we could order everything in alphabetical order by the keys. 804 01:01:55,000 --> 01:01:57,000 We could order everything alphabetically by the value. 805 01:01:57,000 --> 01:02:00,000 >> We could try and take into account that some are strings, some are ints, 806 01:02:00,000 --> 01:02:03,000 and we could try and convert them all to the same type and see what happens, 807 01:02:03,000 --> 01:02:09,000 or we could consider them in the value in which we already entered them 808 01:02:09,000 --> 01:02:14,000 where we put this guy in first, this guy in second, this guy in third, 809 01:02:14,000 --> 01:02:19,000 this guy in fourth, et cetera. 810 01:02:19,000 --> 01:02:23,000 Let's see what happens when we run this code. 811 01:02:23,000 --> 01:02:29,000 If we scroll down and do the same sort of thing, 812 01:02:29,000 --> 01:02:31,000 and here it's not printing out the new lines. 813 01:02:31,000 --> 01:02:35,000 When it read things out of the file it was including the new lines in the values, 814 01:02:35,000 --> 01:02:38,000 which was why it printed out nicely, whereas here it didn't, 815 01:02:38,000 --> 01:02:40,000 so that's why everything is smushed together. 816 01:02:40,000 --> 01:02:44,000 Let's add in that new line just to make things nice. 817 01:02:44,000 --> 01:02:51,000 Let's rerun it, and so here 818 01:02:51,000 --> 01:02:54,000 look at what happened. 819 01:02:54,000 --> 01:03:00,000 It printed everything out in the order in which we put it into the array. 820 01:03:00,000 --> 01:03:10,000 It does preserve order in that sense. 821 01:03:10,000 --> 01:03:15,000 >> Going back to this problem of uniques 822 01:03:15,000 --> 01:03:20,000 where we want to be able to iterate over a file, 823 01:03:20,000 --> 01:03:24,000 and we'll give it some_file.txt, 824 01:03:24,000 --> 01:03:27,000 and we're going to iterate over it like this. 825 01:03:27,000 --> 01:03:31,000 We said that we wanted to use an array to make sure that we'd got all of the 826 01:03:31,000 --> 01:03:36,000 unique lines out of there, and we could do that really easily 827 01:03:36,000 --> 01:03:49,000 by just storing in an array that we of course declare outside the scope of a loop, 828 01:03:49,000 --> 01:03:55,000 and we said that if we used the lines in the file as the keys in our array 829 01:03:55,000 --> 01:03:59,000 if we entered a duplicate line we'd be overriding the previous value. 830 01:03:59,000 --> 01:04:08,000 It's not ever possible to have 2 keys that are identical in the same array. 831 01:04:08,000 --> 01:04:13,000 We can do just that. 832 01:04:13,000 --> 01:04:17,000 We'll get rid of this echo statement right here. 833 01:04:17,000 --> 01:04:24,000 Here we're storing the line in the file in our array 834 01:04:24,000 --> 01:04:28,000 using itself as the key. 835 01:04:28,000 --> 01:04:32,000 Nothing to it, and it turns out that we don't even need this key. 836 01:04:32,000 --> 01:04:38,000 We don't need that i variable. 837 01:04:38,000 --> 01:04:43,000 At this point if we were to do another foreach loop 838 01:04:43,000 --> 01:04:48,000 and we were to loop over each arr 839 01:04:48,000 --> 01:04:56,000 and line now if we echo—oops, sorry. 840 01:04:56,000 --> 01:05:00,000 We can't use the comma. We have to use this as keyword. 841 01:05:00,000 --> 01:05:05,000 Now if we echo line we should get all of the unique words in the file. 842 01:05:05,000 --> 01:05:10,000 If we go up to some_file.txt, 843 01:05:10,000 --> 01:05:19,000 and let's say we do apple, banana, apple, apple, banana, 844 01:05:19,000 --> 01:05:22,000 if we're printing out all of the unique words in this file we should only get 845 01:05:22,000 --> 01:05:30,000 apple and banana to print out. 846 01:05:30,000 --> 01:05:36,000 If we save this, now here we'll zoom back in, 847 01:05:36,000 --> 01:05:44,000 php unique.php, and ta-da. 848 01:05:44,000 --> 01:05:51,000 We've successfully uniqued the file. 849 01:05:51,000 --> 01:05:55,000 >> The final part of this problem is asking you to sort this array before you printed it out 850 01:05:55,000 --> 01:05:58,000 because in this simple example that we've just done 851 01:05:58,000 --> 01:06:04,000 we were lucky in the sense that the file— 852 01:06:04,000 --> 01:06:06,000 we did this contrived example with apples and bananas. 853 01:06:06,000 --> 01:06:08,000 It was already sorted. 854 01:06:08,000 --> 01:06:19,000 But using the simple sort function you can sort an array, which is pretty nifty. 855 01:06:19,000 --> 01:06:24,000 The final thing I wanted to talk with you guys about really quickly 856 01:06:24,000 --> 01:06:31,000 is that this kind of PHP is all well and good, and it's super handy 857 01:06:31,000 --> 01:06:43,000 to know how to do if you ever need to do little, quick things programmatically. 858 01:06:43,000 --> 01:06:48,000 For example, if I need to write a program that, say, 859 01:06:48,000 --> 01:06:53,000 puts everybody into sections I'm not going to go and write it in C. 860 01:06:53,000 --> 01:06:55,000 It's going to be long. 861 01:06:55,000 --> 01:06:57,000 It's going to be kind of a pain, especially if there are files involved, 862 01:06:57,000 --> 01:07:00,000 just as you guys have seen. 863 01:07:00,000 --> 01:07:05,000 It's so nice that with just this much code right here 864 01:07:05,000 --> 01:07:08,000 we were able to rip through a file, pull out all the unique values 865 01:07:08,000 --> 01:07:11,000 and print them back out. 866 01:07:11,000 --> 01:07:16,000 >> However, for your assignments, for your projects, 867 01:07:16,000 --> 01:07:20,000 if you're building websites with PHP the power is that 868 01:07:20,000 --> 01:07:25,000 we're running our PHP files through this interpreter, 869 01:07:25,000 --> 01:07:29,000 and the interpreter is processing everything within the PHP tags, 870 01:07:29,000 --> 01:07:34,000 leaving everything else untouched and spitting out the results. 871 01:07:34,000 --> 01:07:40,000 We can do this to build HTML programmatically. 872 01:07:40,000 --> 01:07:46,000 Now, if we go back to the spec, the last problem in the spec 873 01:07:46,000 --> 01:07:56,000 talks about this idea of concentrations 874 01:07:56,000 --> 01:08:01,000 and creating a drop-down menu, 875 01:08:01,000 --> 01:08:06,000 which you may or may not want to do, depending on what your final project is doing, 876 01:08:06,000 --> 01:08:13,000 that allows the user to select from a list of all possible concentrations 877 01:08:13,000 --> 01:08:15,000 their one concentration. 878 01:08:15,000 --> 01:08:21,000 Now, this is kind of a pain to type this out 879 01:08:21,000 --> 01:08:25,000 and have to do all this manually, especially when you're having to make sure 880 01:08:25,000 --> 01:08:28,000 that you have all the angle brackets in the right place and all the quotes in the right place, 881 01:08:28,000 --> 01:08:34,000 so with PHP you can do this programmatically, and you can do this really quickly. 882 01:08:34,000 --> 01:08:36,000 >> Let's see how to do this. 883 01:08:36,000 --> 01:08:42,000 We're going to open up a new file. 884 01:08:42,000 --> 01:08:46,000 We're going to put in our PHP tags. 885 01:08:46,000 --> 01:08:53,000 We're going to call it concentrations.php, 886 01:08:53,000 --> 01:08:58,000 and now when you're doing this, kind of a good thing to think about 887 01:08:58,000 --> 01:09:02,000 when you're trying to mix and match your PHP and your HTML 888 01:09:02,000 --> 01:09:08,000 is figuring out, okay, what is the part that I want to programmatically generate? 889 01:09:08,000 --> 01:09:11,000 What is the part that I can programmatically generate? 890 01:09:11,000 --> 01:09:17,000 It's true that you can do all of your HTML 891 01:09:17,000 --> 01:09:19,000 inside of PHP blocks. 892 01:09:19,000 --> 01:09:24,000 You can echo all of the HTML as strings. 893 01:09:24,000 --> 01:09:30,000 For example, if I want to start doing the select tags inside of PHP 894 01:09:30,000 --> 01:09:46,000 I can say echo, say select name = concentration, 895 01:09:46,000 --> 01:09:49,000 and then down below I could have another echo tag 896 01:09:49,000 --> 01:09:57,000 or another echo called close the select. 897 01:09:57,000 --> 01:10:00,000 This is one way to do it because what this is literally going to do 898 01:10:00,000 --> 01:10:09,000 is print out this string when it's run through the PHP interpreter, 899 01:10:09,000 --> 01:10:12,000 so the result will be HTML. 900 01:10:12,000 --> 01:10:18,000 If I save this file as it is right now 901 01:10:18,000 --> 01:10:24,000 and I run php concentrations.php 902 01:10:24,000 --> 01:10:26,000 look at what I got. 903 01:10:26,000 --> 01:10:30,000 I got this open close select tag. 904 01:10:30,000 --> 01:10:36,000 >> If I were to do this and I were to save this result to a file, 905 01:10:36,000 --> 01:10:45,000 say, concentrations.html—wait, it looks like the l has gone over to the other side— 906 01:10:45,000 --> 01:11:02,000 now if I open up here concentrations.html you see I have a valid HTML file. 907 01:11:02,000 --> 01:11:05,000 Is that kind of weird? 908 01:11:05,000 --> 01:11:09,000 We're using PHP to create HTML, create valid HTML. 909 01:11:09,000 --> 01:11:13,000 The way we're doing it is we're just having the PHP print 910 01:11:13,000 --> 01:11:16,000 the HTML that we want it to print. 911 01:11:16,000 --> 01:11:20,000 This is literally how PHP websites are working. 912 01:11:20,000 --> 01:11:24,000 When you visit a website that sends you to something like 913 01:11:24,000 --> 01:11:30,000 something, something, something dot com slash index.php 914 01:11:30,000 --> 01:11:33,000 the computer is literally calling up index.php, 915 01:11:33,000 --> 01:11:38,000 running it through the PHP interpreter, and whatever junk comes out 916 01:11:38,000 --> 01:11:49,000 it's sending back to the browser and saying hey, browser, interpret this as HTML. 917 01:11:49,000 --> 01:11:54,000 The nice thing is that it can be a pain to constantly write echo, echo, echo, echo 918 01:11:54,000 --> 01:11:59,000 and enclose everything in quotes like this, so if you want to write 919 01:11:59,000 --> 01:12:04,000 the HTML that's going to be static yourself you can do it like this, 920 01:12:04,000 --> 01:12:16,000 put it outside, close it, 921 01:12:16,000 --> 01:12:21,000 and then here you only put inside the PHP tags that which you know 922 01:12:21,000 --> 01:12:26,000 you want to programmatically generate, and in this case it's those option tags 923 01:12:26,000 --> 01:12:31,000 that are a pain to generate. 924 01:12:31,000 --> 01:12:37,000 >> For example, we could generate a thousand option tags 925 01:12:37,000 --> 01:12:48,000 by doing something like this, $i < 1000, i++ 926 01:12:48,000 --> 01:13:00,000 and saying echo option value = $—whoops, I can't do that. 927 01:13:00,000 --> 01:13:03,000 Well, let's give it a try and see what happens. 928 01:13:03,000 --> 01:13:22,000 $i and then saying $i 01:13:27,000 Rather than having to type out that option tag a thousand different times 930 01:13:27,000 --> 01:13:33,000 I'm using PHP code to generate it automatically, programmatically. 931 01:13:33,000 --> 01:13:38,000 Kind of nifty. Yeah. 932 01:13:38,000 --> 01:13:41,000 I remember this being mentioned in either the lecture or the walkthrough, 933 01:13:41,000 --> 01:13:43,000 but what's the difference between the quotation marks, 934 01:13:43,000 --> 01:13:45,000 the normal ones and these single things? 935 01:13:45,000 --> 01:13:49,000 That's actually something that I've done incorrectly here that I was going to show you. 936 01:13:49,000 --> 01:13:52,000 The single quotes don't allow any special characters, 937 01:13:52,000 --> 01:13:57,000 so everything inside a single quoted string is interpreted literally. 938 01:13:57,000 --> 01:14:02,000 If I had a \n inside of single quotes and I save this, for example, 939 01:14:02,000 --> 01:14:07,000 and now I go down here and run it— 940 01:14:07,000 --> 01:14:12,000 oops, where am I going? 941 01:14:12,000 --> 01:14:19,000 Oh, it's because I forgot the $. 942 01:14:19,000 --> 01:14:25,000 >> Now if I open up—actually, here, we'll get rid of the typing it to a file. 943 01:14:25,000 --> 01:14:27,000 We'll just read it. 944 01:14:27,000 --> 01:14:34,000 Here you see that I've got this literal \n in the string, like not an actual new line, 945 01:14:34,000 --> 01:14:38,000 and likewise, instead of actually interpolating the value of this variable 946 01:14:38,000 --> 01:14:42,000 it gave me the $i itself, which is kind of a pain. 947 01:14:42,000 --> 01:14:46,000 The way around this is to use the double quotes, 948 01:14:46,000 --> 01:14:52,000 and then when you use double quotes it will actually 949 01:14:52,000 --> 01:14:56,000 properly interpolate the values of all of these variables in here 950 01:14:56,000 --> 01:14:59,000 and also recognize this \n to be the new line that it is 951 01:14:59,000 --> 01:15:02,000 and not \n. 952 01:15:02,000 --> 01:15:07,000 Using the single ones is nice when you have 953 01:15:07,000 --> 01:15:10,000 characters that might be interpreted as special characters because then you don't have to 954 01:15:10,000 --> 01:15:15,000 escape them constantly. 955 01:15:15,000 --> 01:15:18,000 Things can get messy, and often the way I do it, 956 01:15:18,000 --> 01:15:24,000 just to make things easy, is that if I'm going to have anything 957 01:15:24,000 --> 01:15:31,000 HTML like in my code I'll include edit within single quotes 958 01:15:31,000 --> 01:15:36,000 and then use this concatenation operator 959 01:15:36,000 --> 01:15:43,000 to concatenate the variables in. 960 01:15:43,000 --> 01:15:47,000 This is another way of doing it 961 01:15:47,000 --> 01:15:52,000 where we've got the dot that's going to connect this is a string 962 01:15:52,000 --> 01:15:56,000 with this with this with this, 963 01:15:56,000 --> 01:15:59,000 and now I don't have to escape these double quotes. 964 01:15:59,000 --> 01:16:02,000 I don't have to use this guy to escape. 965 01:16:02,000 --> 01:16:08,000 They'll literally be printed out as double quotes and then this guy right here. 966 01:16:08,000 --> 01:16:11,000 This is another way of mixing everything together. 967 01:16:11,000 --> 01:16:14,000 >> [Student] Also, when you echoed gettype there were no quotation marks at all. 968 01:16:14,000 --> 01:16:22,000 Right, so echo is kind of special in the sense that you don't need parentheses. 969 01:16:22,000 --> 01:16:29,000 You don't need to have the quotation marks necessarily around variables, 970 01:16:29,000 --> 01:16:37,000 like it's valid to say echo $i, and it's going to interpret this as a string. 971 01:16:37,000 --> 01:16:44,000 It's going to do the cast, so we could do this, and that's valid too, but we don't need it. 972 01:16:44,000 --> 01:16:48,000 It's optional. 973 01:16:48,000 --> 01:16:53,000 In this week's problem set look out for a lot of this kind of stuff 974 01:16:53,000 --> 01:16:58,000 where you're mixing and matching HTML with PHP 975 01:16:58,000 --> 01:17:08,000 just like this, and now if we scroll back and we run this 976 01:17:08,000 --> 01:17:13,000 we can see that we have a whole bunch of option values 977 01:17:13,000 --> 01:17:15,000 that just got printed out. 978 01:17:15,000 --> 01:17:20,000 We have all this HTML that got generated really quickly for us. 979 01:17:20,000 --> 01:17:22,000 I'll leave it as an exercise. 980 01:17:22,000 --> 01:17:24,000 It's very similar to the file reading that we did earlier. 981 01:17:24,000 --> 01:17:31,000 >> There is a concentrations.txt file that you can pull down from the CDN, 982 01:17:31,000 --> 01:17:34,000 and you can open it using the same foreach trick that we did earlier, 983 01:17:34,000 --> 01:17:39,000 and foreach you just have to echo an option and plug in 984 01:17:39,000 --> 01:17:44,000 the appropriate values, the appropriate keys and indices, 985 01:17:44,000 --> 01:17:49,000 and then you get a drop down that literally creates 986 01:17:49,000 --> 01:17:54,000 programmatically for you all of the different concentrations at Harvard. 987 01:17:54,000 --> 01:17:57,000 A handy thing to have. 988 01:17:57,000 --> 01:18:02,000 There is a lot of configuration stuff for this assignment. 989 01:18:02,000 --> 01:18:07,000 If you haven't done it already take the next half hour, hour, 990 01:18:07,000 --> 01:18:10,000 some time tonight definitely for sure, 991 01:18:10,000 --> 01:18:14,000 walk through the problem set spec, 992 01:18:14,000 --> 01:18:20,000 see what's going on with setting up the vhosts, the local host, all that stuff. 993 01:18:20,000 --> 01:18:24,000 One thing to point out right now is that 994 01:18:24,000 --> 01:18:30,000 in most problem sets we've been going into your Dropbox folder 995 01:18:30,000 --> 01:18:34,000 and then downloading the problem set zip file and unzipping it. 996 01:18:34,000 --> 01:18:36,000 For this problem set you don't want to. 997 01:18:36,000 --> 01:18:40,000 You want to go into this vhosts directory. 998 01:18:40,000 --> 01:18:45,000 You'll want to go into vhosts and go into the local host directory within it, 999 01:18:45,000 --> 01:18:50,000 and this is where you'll want to pull down the zip file, 1000 01:18:50,000 --> 01:18:55,000 unzip it, and get ready to go. 1001 01:18:55,000 --> 01:18:57,000 You'll have to follow the steps closely. 1002 01:18:57,000 --> 01:19:02,000 >> If you have any questions, send email, post it on Discuss, come to office hours. 1003 01:19:02,000 --> 01:19:08,000 Thanks guys. Next week we're going to have a review session for the quiz. 1004 01:19:08,000 --> 01:19:12,000 Enjoy your last problem set. I hope it all goes really well. 1005 01:19:12,000 --> 01:19:14,000 [CS50.TV]