1 00:00:00,000 --> 00:00:04,410 >> [MUSIC PLAYING] 2 00:00:04,410 --> 00:00:12,250 3 00:00:12,250 --> 00:00:15,770 >> SPEAKER 1: This is CS50 and this is the end of week seven. 4 00:00:15,770 --> 00:00:20,240 Today is perhaps where we really teaching you languages. 5 00:00:20,240 --> 00:00:24,100 We introduced in the past couple of weeks HTML and CSS, neither of which 6 00:00:24,100 --> 00:00:25,240 are programming languages. 7 00:00:25,240 --> 00:00:28,600 And indeed, even though we didn't look at nearly all of the tags 8 00:00:28,600 --> 00:00:33,410 that HTML comes with and nearly all of the properties that CSS comes with, 9 00:00:33,410 --> 00:00:35,007 that's kind of it for HTML and CSS. 10 00:00:35,007 --> 00:00:36,840 We'll just generally start assuming that you 11 00:00:36,840 --> 00:00:39,545 understand the general ideas of tags, and attributes, 12 00:00:39,545 --> 00:00:41,850 and pages being sent from client to server. 13 00:00:41,850 --> 00:00:45,280 >> Because today we start to look at another programming language, PHP. 14 00:00:45,280 --> 00:00:47,110 We're going to do this super fast. 15 00:00:47,110 --> 00:00:50,910 We're not going to teach you PHP per se, much like we didn't aspire to teach you 16 00:00:50,910 --> 00:00:52,670 C, per se, but rather programming. 17 00:00:52,670 --> 00:00:55,088 And indeed, one of the goals for this course 18 00:00:55,088 --> 00:00:57,820 is not to teach you C, or HTML, or CSS, or PHP, 19 00:00:57,820 --> 00:01:00,370 or any number of other buzz words or acronyms, 20 00:01:00,370 --> 00:01:03,097 but rather the computer science and how to program fundamentally. 21 00:01:03,097 --> 00:01:05,930 And indeed, today we start to take those training wheels off all the 22 00:01:05,930 --> 00:01:10,600 further by flying through a language called PHP, as follows. 23 00:01:10,600 --> 00:01:13,640 >> Here is what this language looks like. 24 00:01:13,640 --> 00:01:17,790 It turns out that there is no need for a main function in a program written 25 00:01:17,790 --> 00:01:19,470 in a language called PHP. 26 00:01:19,470 --> 00:01:22,585 So that already sounds a little simpler than C, with which we're familiar. 27 00:01:22,585 --> 00:01:25,370 It turns out that if you want to declare a variable, 28 00:01:25,370 --> 00:01:29,470 you do it almost identically to C. But there's clearly one difference here 29 00:01:29,470 --> 00:01:31,850 when I declare a string, or maybe two differences. 30 00:01:31,850 --> 00:01:32,725 What looks different? 31 00:01:32,725 --> 00:01:35,810 32 00:01:35,810 --> 00:01:37,160 >> AUDIENCE: The dollar sign. 33 00:01:37,160 --> 00:01:39,951 >> SPEAKER 1: Yes, there's dollar sign, which we've never seen before. 34 00:01:39,951 --> 00:01:41,451 And what is missing? 35 00:01:41,451 --> 00:01:41,950 Yeah? 36 00:01:41,950 --> 00:01:42,820 >> AUDIENCE: [INAUDIBLE] 37 00:01:42,820 --> 00:01:44,403 >> SPEAKER 1: Yeah, there's no data type. 38 00:01:44,403 --> 00:01:48,410 So it turns out that PHP is what we call a loosely typed language, where 39 00:01:48,410 --> 00:01:49,959 C is strongly typed. 40 00:01:49,959 --> 00:01:52,500 Loosely typed just means that there are different data types, 41 00:01:52,500 --> 00:01:54,470 there are strings, and ints, and floats. 42 00:01:54,470 --> 00:01:56,330 But the computer figures that out. 43 00:01:56,330 --> 00:01:58,664 You the human programmer don't have to figure it out. 44 00:01:58,664 --> 00:02:00,580 So all you have to do to declare a variable is 45 00:02:00,580 --> 00:02:03,220 dollar sign, the name of your variable, and then, for instance, 46 00:02:03,220 --> 00:02:04,760 quote unquote, hello world. 47 00:02:04,760 --> 00:02:07,662 >> Well this is what a condition looks like in PHP. 48 00:02:07,662 --> 00:02:08,328 Any differences? 49 00:02:08,328 --> 00:02:10,650 50 00:02:10,650 --> 00:02:11,150 No. 51 00:02:11,150 --> 00:02:12,540 So it turns out these are identical. 52 00:02:12,540 --> 00:02:14,665 And you might have fewer branches or more branches, 53 00:02:14,665 --> 00:02:16,557 but the syntax turns out exactly the same. 54 00:02:16,557 --> 00:02:18,390 Boolean expressions can be ordered together. 55 00:02:18,390 --> 00:02:20,400 Boolean expressions can be anded together. 56 00:02:20,400 --> 00:02:21,430 You still have switches. 57 00:02:21,430 --> 00:02:23,790 Even if you haven't used these, these do exist in C 58 00:02:23,790 --> 00:02:26,020 and they're functionally equivalent in PHP. 59 00:02:26,020 --> 00:02:28,260 >> And in fact, in PHP they're a little more powerful. 60 00:02:28,260 --> 00:02:32,540 In C, you can only compare certain data types like ints and chars, 61 00:02:32,540 --> 00:02:36,320 whereas in PHP you can actually compare full fledge strings without having 62 00:02:36,320 --> 00:02:38,460 to worry about pointers and the like. 63 00:02:38,460 --> 00:02:39,430 So here's a for loop. 64 00:02:39,430 --> 00:02:41,270 This is just pseudocode code, if you will. 65 00:02:41,270 --> 00:02:43,400 But structurally it's identical to C. 66 00:02:43,400 --> 00:02:47,520 >> Here's a while loop, pseudocode therefore-- identical to C. 67 00:02:47,520 --> 00:02:49,390 Here's a do while loop-- identical. 68 00:02:49,390 --> 00:02:51,390 And so there's kind of this pattern here whereby 69 00:02:51,390 --> 00:02:54,400 there's a few other syntactic features we're indeed going to see. 70 00:02:54,400 --> 00:02:57,340 In fact, here is a nice way in PHP whereby, 71 00:02:57,340 --> 00:02:59,350 if you want to iterate over an array, and you 72 00:02:59,350 --> 00:03:02,309 don't want to jump through the mental and syntactic hoops of having 73 00:03:02,309 --> 00:03:05,178 int i equals 0, i less than n, i plus plus-- which 74 00:03:05,178 --> 00:03:07,040 is just a lot of annoying syntax. 75 00:03:07,040 --> 00:03:10,654 You can say a little more fluidly for each numbers 76 00:03:10,654 --> 00:03:13,320 as number, where in this case dollar sign numbers, I'm assuming, 77 00:03:13,320 --> 00:03:15,400 is like an array called numbers. 78 00:03:15,400 --> 00:03:18,810 And then I'm assuming number, singular, is going to be a variable. 79 00:03:18,810 --> 00:03:22,940 >> And what PHP will do for me as I iterate over this loop is, on each iteration, 80 00:03:22,940 --> 00:03:28,450 it's going update the variable number to be the ith element in numbers, plural. 81 00:03:28,450 --> 00:03:30,060 So it just handles all of that for me. 82 00:03:30,060 --> 00:03:32,750 No square brackets, no dollar sign, no semi-colons. 83 00:03:32,750 --> 00:03:34,117 It's just a little simpler. 84 00:03:34,117 --> 00:03:35,950 Well, if you want an array, it turns out you 85 00:03:35,950 --> 00:03:38,908 can do something pretty similar to C. The syntax is a little different. 86 00:03:38,908 --> 00:03:41,000 But here's an array called dollar signed numbers. 87 00:03:41,000 --> 00:03:44,261 And I use, in PHP, square brackets, it turns out. 88 00:03:44,261 --> 00:03:47,260 And we're going to see these again in JavaScript-- yet another language. 89 00:03:47,260 --> 00:03:49,968 I just use square bracket notation to have a comma separated list 90 00:03:49,968 --> 00:03:51,530 of integers, in this case. 91 00:03:51,530 --> 00:03:53,010 But that gives me an array. 92 00:03:53,010 --> 00:03:56,270 >> But even more powerful in PHP, we also have what are generally 93 00:03:56,270 --> 00:03:57,640 called associative arrays. 94 00:03:57,640 --> 00:03:59,540 And a lot of languages have these as well. 95 00:03:59,540 --> 00:04:03,810 The thing about C arrays is that the indexes 96 00:04:03,810 --> 00:04:06,940 for an array, the thing that you use in square brackets via which 97 00:04:06,940 --> 00:04:08,600 you access elements, must be what? 98 00:04:08,600 --> 00:04:10,620 >> AUDIENCE: [INAUDIBLE] 99 00:04:10,620 --> 00:04:14,220 >> SPEAKER 1: They have to be numbers or integers, specifically from zero on up. 100 00:04:14,220 --> 00:04:17,140 Well, in PHP, and, it turns out, in other languages, 101 00:04:17,140 --> 00:04:20,820 you can start to have indexes that are not numeric. 102 00:04:20,820 --> 00:04:23,880 But they're actually words or phrases, so that rather than 103 00:04:23,880 --> 00:04:27,070 try to remember where something is, or stored in a variable i, 104 00:04:27,070 --> 00:04:31,720 you can actually use a word and say that-- you can say a word like this. 105 00:04:31,720 --> 00:04:34,730 >> So dollar sign quote is the name of a variable here. 106 00:04:34,730 --> 00:04:39,360 This is somewhat funky syntax that's associating two key value 107 00:04:39,360 --> 00:04:41,250 pairs for this variable. 108 00:04:41,250 --> 00:04:45,490 Specifically, this is giving me an array that is of length 2. 109 00:04:45,490 --> 00:04:47,660 But the indexes are not 0 and 1. 110 00:04:47,660 --> 00:04:51,250 The indexes are quote unquote symbol and quote unquote price. 111 00:04:51,250 --> 00:04:55,990 And the values of those indexes, respectively, is FB for Facebook 112 00:04:55,990 --> 00:05:00,360 and $79.53, when I last checked a price for this. 113 00:05:00,360 --> 00:05:01,210 >> What does that mean? 114 00:05:01,210 --> 00:05:04,136 Well, it means that instead of writing code 115 00:05:04,136 --> 00:05:09,612 like this, where I used to do something like dollar sign-- or rather, in C 116 00:05:09,612 --> 00:05:11,070 I wouldn't even have a dollar sign. 117 00:05:11,070 --> 00:05:13,490 I would do something like quotes and then 118 00:05:13,490 --> 00:05:18,410 I would do bracket i to get at some arbitrarily numerically indexed value. 119 00:05:18,410 --> 00:05:21,430 Starting today in PHP, if you want to get at some value, 120 00:05:21,430 --> 00:05:24,742 we sort of have more semantic expressiveness. 121 00:05:24,742 --> 00:05:27,450 Just a fancy way of saying we can just call things what they are. 122 00:05:27,450 --> 00:05:29,800 >> And if you want to get at something symbol, 123 00:05:29,800 --> 00:05:32,180 now you literally do quote unquote symbol 124 00:05:32,180 --> 00:05:34,910 in square brackets instead of a mere number. 125 00:05:34,910 --> 00:05:36,450 So it's a nice convenience. 126 00:05:36,450 --> 00:05:38,140 And that's sort of it for now. 127 00:05:38,140 --> 00:05:40,240 There's bunches of other features in PHP. 128 00:05:40,240 --> 00:05:43,180 And indeed, PHP comes with the proverbial kitchen sink. 129 00:05:43,180 --> 00:05:48,640 Dozens, hundreds of functions come with PHP-- so many more than came with C. 130 00:05:48,640 --> 00:05:51,360 >> And that's not intended to overwhelm but rather help 131 00:05:51,360 --> 00:05:53,900 you realize that we no longer have to write code 132 00:05:53,900 --> 00:05:55,590 that's sort of down here conceptually. 133 00:05:55,590 --> 00:05:58,465 Now we can just start taking advantage of much more advanced features 134 00:05:58,465 --> 00:05:59,920 that languages like PHP have. 135 00:05:59,920 --> 00:06:02,630 So we don't have to worry about moving things around in memory. 136 00:06:02,630 --> 00:06:05,338 We don't have to worry about malloc and all of these lower level, 137 00:06:05,338 --> 00:06:09,390 powerful details that you will wrestle with, if not struggle with, pset5, 138 00:06:09,390 --> 00:06:13,380 but you can begin to soon take for granted. 139 00:06:13,380 --> 00:06:17,770 >> In fact, let's write a super simple program in PHP, as follows. 140 00:06:17,770 --> 00:06:21,550 I'm going to go head into CS50 IDE here, and what I'm going to do 141 00:06:21,550 --> 00:06:29,370 is create a new file, that very simply is going to be called hello dot PHP. 142 00:06:29,370 --> 00:06:31,860 So instead of dot c, it's going to be called dot PHP. 143 00:06:31,860 --> 00:06:34,260 And then in this file, I can start typing. 144 00:06:34,260 --> 00:06:36,960 But it turns out that the one thing I have to know 145 00:06:36,960 --> 00:06:41,470 is that any PHP file in general, needs to start with some special syntax. 146 00:06:41,470 --> 00:06:44,570 Similar in spirit to HTML, but again, PHP is a programming language, not 147 00:06:44,570 --> 00:06:45,590 a markup language. 148 00:06:45,590 --> 00:06:48,700 >> So every file, and it's sort of stupid looking I'll admit. 149 00:06:48,700 --> 00:06:50,480 Open bracket, question mark, PHP. 150 00:06:50,480 --> 00:06:53,060 And that's just the language's way of saying, 151 00:06:53,060 --> 00:06:58,400 hey, computer here comes some code written in a language called PHP. 152 00:06:58,400 --> 00:07:01,900 And then at the very end of your file, you do kind of sort of the opposite. 153 00:07:01,900 --> 00:07:04,860 You don't write PHP again, you just do question mark, angle bracket. 154 00:07:04,860 --> 00:07:08,500 And then anything you put inside of these two tags, so to speak, 155 00:07:08,500 --> 00:07:11,490 will be, or should be, PHP code. 156 00:07:11,490 --> 00:07:13,180 >> So let's do something super simple. 157 00:07:13,180 --> 00:07:19,370 I'm going to go ahead and do print def, hello world, backslash n, save. 158 00:07:19,370 --> 00:07:27,310 And now at my prompt, I'm going to do, make hello-- no, I'm going to do, 159 00:07:27,310 --> 00:07:28,726 dot slash hello. 160 00:07:28,726 --> 00:07:31,250 No, dot slash, hm. 161 00:07:31,250 --> 00:07:32,990 So something's different. 162 00:07:32,990 --> 00:07:36,920 And indeed, this is a key difference between PHP and languages like C. C 163 00:07:36,920 --> 00:07:38,730 was a so-called compiled language. 164 00:07:38,730 --> 00:07:40,929 What did it mean for C to be compiled? 165 00:07:40,929 --> 00:07:41,970 What was the implication? 166 00:07:41,970 --> 00:07:42,898 Yeah. 167 00:07:42,898 --> 00:07:45,220 >> AUDIENCE: Wrote it into machine code so it could be run by the computer. 168 00:07:45,220 --> 00:07:46,011 >> SPEAKER 1: Exactly. 169 00:07:46,011 --> 00:07:49,075 We have to first convert it into so-called machine code, zeros and ones, 170 00:07:49,075 --> 00:07:51,950 before it will actually be executable and understood by the computer. 171 00:07:51,950 --> 00:07:52,840 By Intel inside. 172 00:07:52,840 --> 00:07:54,530 By the CPU inside of the computer. 173 00:07:54,530 --> 00:07:58,170 PHP, by contrast, is what's called an interpreted language, which 174 00:07:58,170 --> 00:08:02,230 means that you don't compile it into zeros and ones, machine code, 175 00:08:02,230 --> 00:08:04,360 you instead leave it as source code. 176 00:08:04,360 --> 00:08:08,130 And you instead pass it as input to a program, an interpreter 177 00:08:08,130 --> 00:08:11,960 that someone else wrote years ago that understands it 178 00:08:11,960 --> 00:08:14,310 line by line, character for character. 179 00:08:14,310 --> 00:08:16,120 So in other words, humans out there wrote 180 00:08:16,120 --> 00:08:19,670 a program that will look at your code line by line, 181 00:08:19,670 --> 00:08:21,680 and figure out what the computer should do, 182 00:08:21,680 --> 00:08:25,960 without converting it directly to zeros and ones. 183 00:08:25,960 --> 00:08:28,370 >> So if it sees a for loop, this so-called interpreter 184 00:08:28,370 --> 00:08:30,630 is going to be like, all right, I should do something again and again and again. 185 00:08:30,630 --> 00:08:31,150 What should I do? 186 00:08:31,150 --> 00:08:32,159 Let me look at the next line. 187 00:08:32,159 --> 00:08:33,367 Let me look at the next line. 188 00:08:33,367 --> 00:08:35,350 And it truly interprets it line by line. 189 00:08:35,350 --> 00:08:37,220 So to execute it, I don't use make. 190 00:08:37,220 --> 00:08:39,409 I don't use dot slash in this case. 191 00:08:39,409 --> 00:08:43,530 I instead do PHP, which is the name of a program, a.k.a. 192 00:08:43,530 --> 00:08:46,562 An interpreter, that's pre-installed in CS50 IDE, 193 00:08:46,562 --> 00:08:48,770 and now I need to pass a command line argument, which 194 00:08:48,770 --> 00:08:52,290 is quite simply the name of the file that I want to interpret. 195 00:08:52,290 --> 00:08:54,740 Or more casually, that I want to run. 196 00:08:54,740 --> 00:08:57,000 And if I hit Enter, there we have it. 197 00:08:57,000 --> 00:08:57,825 Hello world. 198 00:08:57,825 --> 00:08:59,950 Now it turns out that I used print def deliberately 199 00:08:59,950 --> 00:09:01,600 to bridge us from C to PHP. 200 00:09:01,600 --> 00:09:05,150 Most PHP programmers, as you'll see, don't even bother using print def. 201 00:09:05,150 --> 00:09:08,290 They just use a function called print, which C did not have. 202 00:09:08,290 --> 00:09:11,390 And so if I rerun it now, it actually behaves functionally the same. 203 00:09:11,390 --> 00:09:13,730 I can't use the percent s and percent i and so forth, 204 00:09:13,730 --> 00:09:16,250 but there are other ways in PHP of doing that. 205 00:09:16,250 --> 00:09:18,100 But this is only to say that syntactically, 206 00:09:18,100 --> 00:09:21,850 other than some weird things up top, and some weird characters down 207 00:09:21,850 --> 00:09:25,960 at the bottom, the concepts now of programming in this other language 208 00:09:25,960 --> 00:09:27,860 are going to be exactly the same. 209 00:09:27,860 --> 00:09:30,120 >> In fact, let's do one other example. 210 00:09:30,120 --> 00:09:32,520 So I'm going to go ahead and close this file. 211 00:09:32,520 --> 00:09:37,740 Let me create another one that I'll call conditions 1 dot PHP. 212 00:09:37,740 --> 00:09:40,770 So no zero, because what I did was I looked back a few weeks ago, 213 00:09:40,770 --> 00:09:43,490 we had a file called conditions 1 dot C. And what 214 00:09:43,490 --> 00:09:47,590 I did was converted it in my head, and in here on paper, 215 00:09:47,590 --> 00:09:51,290 and soon on the course's website, line by line into PHP. 216 00:09:51,290 --> 00:09:55,520 >> So in the C version, we did something like string s gets get string. 217 00:09:55,520 --> 00:09:58,200 So it's a little different to do that in PHP. 218 00:09:58,200 --> 00:10:01,390 Instead, I'm just going to do, or rather, I 219 00:10:01,390 --> 00:10:05,870 might have done int n gets get int, in this example earlier. 220 00:10:05,870 --> 00:10:12,600 So instead of that, I'm going to do n gets read line, I'd like an integer, 221 00:10:12,600 --> 00:10:13,670 please. 222 00:10:13,670 --> 00:10:15,110 So this is just my prompt. 223 00:10:15,110 --> 00:10:17,260 And so it turns out, and I would only know this 224 00:10:17,260 --> 00:10:20,480 from having read or seen the function before, read lines of function in PHP 225 00:10:20,480 --> 00:10:23,810 takes an argument that is a prompt for the human, and its purpose in life 226 00:10:23,810 --> 00:10:26,220 is to read a line of text that he or she types in. 227 00:10:26,220 --> 00:10:29,160 And then it stores that text into the variable n. 228 00:10:29,160 --> 00:10:32,200 >> And now I might want to do something like I did weeks ago, in like week 229 00:10:32,200 --> 00:10:33,290 one of the class. 230 00:10:33,290 --> 00:10:36,600 If n is greater than zero, then, I'm going 231 00:10:36,600 --> 00:10:42,930 to borrow that syntax we just saw, print def, you picked a positive number, 232 00:10:42,930 --> 00:10:44,360 backslash n. 233 00:10:44,360 --> 00:10:49,990 Else if n equals equals zero, I'm going to go ahead and say, print def, 234 00:10:49,990 --> 00:10:54,090 you picked zero backslash n. 235 00:10:54,090 --> 00:10:56,830 Else, the case here should of course be, print def, 236 00:10:56,830 --> 00:10:58,620 you picked a negative number. 237 00:10:58,620 --> 00:11:00,970 >> And we can certainly implement the logic of this thing 238 00:11:00,970 --> 00:11:03,390 in a bunch of different ways, but the point here 239 00:11:03,390 --> 00:11:07,210 is that syntactically, barely anything is new. 240 00:11:07,210 --> 00:11:09,830 It's just the dollar sign and a new function and read line. 241 00:11:09,830 --> 00:11:12,090 But fundamentally, what's new now is I'm interpreting this. 242 00:11:12,090 --> 00:11:13,910 I'm passing it as input to another program. 243 00:11:13,910 --> 00:11:16,620 So if I want to run this, if I didn't make any mistakes, 244 00:11:16,620 --> 00:11:20,140 I'm going to run PHP, of conditions 1 dot PHP, Enter, 245 00:11:20,140 --> 00:11:23,440 I'm going to type in the number 50, and let's assume for now it works. 246 00:11:23,440 --> 00:11:25,940 Because the logic is the same stuff as week one. 247 00:11:25,940 --> 00:11:28,020 >> All right, so that's pretty underwhelming, right? 248 00:11:28,020 --> 00:11:30,240 We could do this in any number of languages. 249 00:11:30,240 --> 00:11:33,200 Let's do something way more powerful. 250 00:11:33,200 --> 00:11:35,610 Let's finish problem set five. 251 00:11:35,610 --> 00:11:37,370 So I'm going to go ahead and do this. 252 00:11:37,370 --> 00:11:42,920 I'm going to create a file called dictionary dot PHP, 253 00:11:42,920 --> 00:11:45,424 and inclined as you might be to scramble down lots of notes, 254 00:11:45,424 --> 00:11:47,340 the notes will only help you if you're allowed 255 00:11:47,340 --> 00:11:48,923 to implement problems set five in PHP. 256 00:11:48,923 --> 00:11:52,706 But we'll see how quickly now we can implement that same problem set. 257 00:11:52,706 --> 00:11:56,830 >> So in dictionary dot PHP, I am going to assume that there is already 258 00:11:56,830 --> 00:11:58,514 a file in the world called speller. 259 00:11:58,514 --> 00:12:01,430 And indeed, I did this in advance, and we won't walk through this line 260 00:12:01,430 --> 00:12:03,596 by line, but if you're curious later and really want 261 00:12:03,596 --> 00:12:06,250 to wrap your mind around the differences between c and PHP, 262 00:12:06,250 --> 00:12:09,030 literally compare this file, speller, from today's source code 263 00:12:09,030 --> 00:12:11,190 that we'll post later today, against p set 264 00:12:11,190 --> 00:12:15,309 five speller dot c that we give you, and it's almost the same line by line. 265 00:12:15,309 --> 00:12:16,600 There's some more dollar signs. 266 00:12:16,600 --> 00:12:18,330 A couple functions are a little different. 267 00:12:18,330 --> 00:12:20,038 But it's a nice way of sort of seeing how 268 00:12:20,038 --> 00:12:22,740 you translate one language to another. 269 00:12:22,740 --> 00:12:24,812 And it's almost identical line for line. 270 00:12:24,812 --> 00:12:27,770 But I'm going to assume that that exists, and what I'm going to do here 271 00:12:27,770 --> 00:12:31,310 is try to blow your mind by reimplementing 272 00:12:31,310 --> 00:12:36,210 the entirety of problem set five way faster than you've been doing thus far. 273 00:12:36,210 --> 00:12:39,330 So for instance, I'm going to first declare a global variable called size, 274 00:12:39,330 --> 00:12:40,444 and set it equal to zero. 275 00:12:40,444 --> 00:12:41,610 Now that's not much savings. 276 00:12:41,610 --> 00:12:44,170 Odds are you implemented size pretty much the same, 277 00:12:44,170 --> 00:12:48,440 or hint hint, will tonight or tomorrow, just using a global variable called 278 00:12:48,440 --> 00:12:49,900 size, and setting it equal to zero. 279 00:12:49,900 --> 00:12:52,660 That's not a particularly amazing spoiler. 280 00:12:52,660 --> 00:12:57,180 >> So what were your data structures of choice, those of you who have dived in 281 00:12:57,180 --> 00:13:02,170 and read the spec, what data structures have most of you have been using? 282 00:13:02,170 --> 00:13:06,840 A hash table or try, maybe some variants thereof. 283 00:13:06,840 --> 00:13:10,490 So implementing a hash table at least, is kind of a lot of lines of code, 284 00:13:10,490 --> 00:13:10,990 right? 285 00:13:10,990 --> 00:13:13,700 And not all of them might be functional at this point in the week, 286 00:13:13,700 --> 00:13:14,366 but that's fine. 287 00:13:14,366 --> 00:13:19,300 Because in PHP, if I want a hash table, done. 288 00:13:19,300 --> 00:13:20,260 Right? 289 00:13:20,260 --> 00:13:22,610 So that variable that I've just declared is obviously 290 00:13:22,610 --> 00:13:26,180 called dollar sign table, per the introduction before. 291 00:13:26,180 --> 00:13:28,902 >> But I really just want, at the end of the day, an array. 292 00:13:28,902 --> 00:13:31,610 But not a numerically indexed array, because that's like week two 293 00:13:31,610 --> 00:13:33,440 stuff, when we talked about arrays. 294 00:13:33,440 --> 00:13:34,850 I want a hash table. 295 00:13:34,850 --> 00:13:38,070 But a hash table is really just a concrete way of saying, 296 00:13:38,070 --> 00:13:40,190 you want an associative array. 297 00:13:40,190 --> 00:13:43,270 You want to be able to associate keys with values. 298 00:13:43,270 --> 00:13:44,200 Keys with values. 299 00:13:44,200 --> 00:13:45,640 What is a dictionary, really? 300 00:13:45,640 --> 00:13:47,080 Well it's a whole bunch of keys. 301 00:13:47,080 --> 00:13:50,320 Words like apple, pear, and banana, and all of these English words 302 00:13:50,320 --> 00:13:51,490 that we hand you. 303 00:13:51,490 --> 00:13:54,820 >> And the values that you're effectively inserting into your dictionary 304 00:13:54,820 --> 00:13:57,790 are either true, or the absence, false. 305 00:13:57,790 --> 00:14:00,540 In other words you are inserting a whole bunch of key value pairs, 306 00:14:00,540 --> 00:14:03,720 apple, true, pear, true, banana, true. 307 00:14:03,720 --> 00:14:07,446 So that when you retrieve or look up that data in your hash table 308 00:14:07,446 --> 00:14:10,070 with your check function, you're either getting back an answer, 309 00:14:10,070 --> 00:14:13,260 yes, I found it in my complex hash table, or no, it's not there, 310 00:14:13,260 --> 00:14:14,540 so you return false. 311 00:14:14,540 --> 00:14:19,440 >> So to do that, all I need is the data structure like I proposed before. 312 00:14:19,440 --> 00:14:22,050 I just need to be able to associate words 313 00:14:22,050 --> 00:14:25,570 like, quote unquote, symbol, with a value like, true. 314 00:14:25,570 --> 00:14:27,090 So there's my hash table. 315 00:14:27,090 --> 00:14:28,180 Let's actually use it. 316 00:14:28,180 --> 00:14:30,310 So there's a few functions I need to implement. 317 00:14:30,310 --> 00:14:33,300 I'm going to go ahead and bite off a function called size. 318 00:14:33,300 --> 00:14:34,390 It takes no arguments. 319 00:14:34,390 --> 00:14:36,660 I don't have to bother with the word, void, in PHP. 320 00:14:36,660 --> 00:14:38,770 I'm simply going to return size. 321 00:14:38,770 --> 00:14:41,287 >> And as an aside, PHP has this minorly annoying detail, 322 00:14:41,287 --> 00:14:44,620 where if something's global, you have to tell the function in which you're using 323 00:14:44,620 --> 00:14:46,220 it, hey, this is a global variable. 324 00:14:46,220 --> 00:14:49,330 So, minor stupidity, but you have to do it nonetheless. 325 00:14:49,330 --> 00:14:50,920 >> So what about load? 326 00:14:50,920 --> 00:14:52,990 I'm going to implement a function called load 327 00:14:52,990 --> 00:14:56,790 that takes in the name of a dictionary, just like in problem set five. 328 00:14:56,790 --> 00:14:59,554 And before I proceed, notice that I haven't 329 00:14:59,554 --> 00:15:00,970 been typing quite the same things. 330 00:15:00,970 --> 00:15:06,060 What's obviously different in PHP about how you declare a function versus C? 331 00:15:06,060 --> 00:15:06,616 Yeah. 332 00:15:06,616 --> 00:15:07,490 AUDIENCE: [INAUDIBLE] 333 00:15:07,490 --> 00:15:08,573 SPEAKER 1: No return type. 334 00:15:08,573 --> 00:15:12,350 And indeed, that's the case, PHP insofar as it's loosely typed, 335 00:15:12,350 --> 00:15:14,190 is also a little sloppy in that sense. 336 00:15:14,190 --> 00:15:17,680 You don't specify as the programmer what this function returns. 337 00:15:17,680 --> 00:15:20,490 You would have to actually look at the code to figure that out. 338 00:15:20,490 --> 00:15:22,670 Or read the comments or the documentation. 339 00:15:22,670 --> 00:15:24,870 So pluses and minuses of these kinds of decisions. 340 00:15:24,870 --> 00:15:26,750 But I do have to say the key word, function. 341 00:15:26,750 --> 00:15:28,360 And when we learn JavaScript in a couple weeks 342 00:15:28,360 --> 00:15:31,026 we're going to see the same thing again, but it's the same idea. 343 00:15:31,026 --> 00:15:34,220 The name of the function, its argument or arguments or lack thereof, and now 344 00:15:34,220 --> 00:15:35,630 here is the implementation. 345 00:15:35,630 --> 00:15:38,860 >> So I'm going to cut some corners just to be dramatic for just a moment. 346 00:15:38,860 --> 00:15:40,485 But I'm going to go ahead and say this. 347 00:15:40,485 --> 00:15:44,890 This is how I can load a file into an array. 348 00:15:44,890 --> 00:15:47,580 There is a function in PHP called file. 349 00:15:47,580 --> 00:15:49,300 You hand it the name of a file. 350 00:15:49,300 --> 00:15:53,950 It hands you back an array, inside of which is every line from the file. 351 00:15:53,950 --> 00:15:56,480 From zero, one, on up to n minus 1 lines. 352 00:15:56,480 --> 00:15:57,210 That's it, right? 353 00:15:57,210 --> 00:15:57,990 There's no f read. 354 00:15:57,990 --> 00:15:58,826 There's no f get s. 355 00:15:58,826 --> 00:15:59,700 There's no percent s. 356 00:15:59,700 --> 00:16:00,420 There's no headaches. 357 00:16:00,420 --> 00:16:01,220 There's no feof. 358 00:16:01,220 --> 00:16:03,950 All of that stuff with which you've been wrestling perhaps, 359 00:16:03,950 --> 00:16:06,550 with p set four and five, goes away. 360 00:16:06,550 --> 00:16:09,450 >> So I just read those lines into the file, and then you know what? 361 00:16:09,450 --> 00:16:15,510 If I want to iterate over those words, I can do, for each lines as line. 362 00:16:15,510 --> 00:16:19,992 Remember that this was kind of a clever way of iterating over an array, index 363 00:16:19,992 --> 00:16:25,090 by index, and on each iteration calling the current line, dollar sign line. 364 00:16:25,090 --> 00:16:31,560 And right here I'm going to go and say table, bracket, line, gets true. 365 00:16:31,560 --> 00:16:35,880 >> In other words, this is how I insert into my hash table in PHP. 366 00:16:35,880 --> 00:16:38,740 I say dollar sign table, which is my associative array that was 367 00:16:38,740 --> 00:16:40,690 empty per the line of code up above. 368 00:16:40,690 --> 00:16:42,790 I then index into it not, using a number, 369 00:16:42,790 --> 00:16:45,300 but literally using the keyword that I care about. 370 00:16:45,300 --> 00:16:47,730 Maybe it's apple or pear or banana or whatever, 371 00:16:47,730 --> 00:16:51,645 but specifically I'm indexing into it like I proposed earlier. 372 00:16:51,645 --> 00:16:53,770 I take the name of my associative array, and then I 373 00:16:53,770 --> 00:16:56,450 use quote unquote in the square brackets with a string, 374 00:16:56,450 --> 00:16:58,490 instead of an actual number. 375 00:16:58,490 --> 00:17:00,250 >> And so that's it. 376 00:17:00,250 --> 00:17:01,720 The load function is done. 377 00:17:01,720 --> 00:17:05,119 Once that loop iterates, I've put everything into the hash table. 378 00:17:05,119 --> 00:17:07,810 Now small disclaimer, there's a couple things I do need to fix. 379 00:17:07,810 --> 00:17:10,030 And the version I'll post online will have all of the nuances, 380 00:17:10,030 --> 00:17:12,670 but it's mostly just going to be some error checking and some minor tweaks. 381 00:17:12,670 --> 00:17:14,044 But that's indeed the gist of it. 382 00:17:14,044 --> 00:17:16,750 If I now want to implement the function called 383 00:17:16,750 --> 00:17:19,869 check, which expects a word as its argument, 384 00:17:19,869 --> 00:17:22,099 how might I go about doing that? 385 00:17:22,099 --> 00:17:30,020 Well, I'm simply going to say, if inside of my table, at location word, 386 00:17:30,020 --> 00:17:30,652 if is set. 387 00:17:30,652 --> 00:17:32,360 So if there is-- actually, you know what? 388 00:17:32,360 --> 00:17:34,830 I'm going to do it in a bit of pseudocode. 389 00:17:34,830 --> 00:17:36,020 But the idea is the same. 390 00:17:36,020 --> 00:17:41,640 If that equals true, return true. 391 00:17:41,640 --> 00:17:43,890 All right. 392 00:17:43,890 --> 00:17:46,851 Else-- you can kind of see where this is going-- to return false. 393 00:17:46,851 --> 00:17:47,350 Done. 394 00:17:47,350 --> 00:17:49,640 Check is done. 395 00:17:49,640 --> 00:17:50,940 >> Pretty nice, right? 396 00:17:50,940 --> 00:17:52,560 And so what is this really getting at? 397 00:17:52,560 --> 00:17:53,680 And this too, I cut some corners. 398 00:17:53,680 --> 00:17:56,020 Look at the version online for all of the slight nuances. 399 00:17:56,020 --> 00:17:57,103 But that's the gist of it. 400 00:17:57,103 --> 00:17:59,080 Index into your associative array, a.k.a. 401 00:17:59,080 --> 00:18:02,910 Hash table, see if there's a value there, if it's set to true, and if so, 402 00:18:02,910 --> 00:18:03,930 return true. 403 00:18:03,930 --> 00:18:05,819 So we've whittled down all of the complexity. 404 00:18:05,819 --> 00:18:07,110 So kind of mind blowing, right? 405 00:18:07,110 --> 00:18:09,680 I won't bother finishing it with unload, because in fact-- oh, you know what? 406 00:18:09,680 --> 00:18:11,620 Yeah, let's finish it with unload. 407 00:18:11,620 --> 00:18:18,940 >> Unload in a hash table might look like function unload, return. 408 00:18:18,940 --> 00:18:19,930 OK so that's unload. 409 00:18:19,930 --> 00:18:21,350 Because there's nothing to unload, right? 410 00:18:21,350 --> 00:18:22,170 There's no malloc. 411 00:18:22,170 --> 00:18:24,280 I didn't explicitly ask the operating system for anything. 412 00:18:24,280 --> 00:18:25,571 I just started using variables. 413 00:18:25,571 --> 00:18:30,510 And so this too is a manifestation of features of higher level languages. 414 00:18:30,510 --> 00:18:32,940 So again, most of this term we've been dealing down here 415 00:18:32,940 --> 00:18:34,280 with C. Super low level. 416 00:18:34,280 --> 00:18:35,980 You can see the computer's memory. 417 00:18:35,980 --> 00:18:38,440 You can touch anything you want in your computer's RAM, 418 00:18:38,440 --> 00:18:39,440 for better or for worse. 419 00:18:39,440 --> 00:18:41,760 >> Up here, we're going to give up that power. 420 00:18:41,760 --> 00:18:44,730 But my god, look how much less code I wrote. 421 00:18:44,730 --> 00:18:47,660 In fact if I weren't talking and talking over my typing, 422 00:18:47,660 --> 00:18:50,190 we would have been done with this example five minutes ago. 423 00:18:50,190 --> 00:18:52,150 So what's the price being paid? 424 00:18:52,150 --> 00:18:54,100 Well let's take a look. 425 00:18:54,100 --> 00:18:55,340 Let's take a look. 426 00:18:55,340 --> 00:18:59,340 I'm going to go ahead and run CS50. 427 00:18:59,340 --> 00:19:02,160 Let me first go into today's examples where 428 00:19:02,160 --> 00:19:03,970 I have the texts directory as before. 429 00:19:03,970 --> 00:19:08,520 I'm going to run the solution that it comes with problem set five called 430 00:19:08,520 --> 00:19:11,190 speller, which is in CS50's account. 431 00:19:11,190 --> 00:19:14,040 >> And I'm going to run it on something big like the King James Bible, 432 00:19:14,040 --> 00:19:16,547 just so that we really put the staff solutions to the test. 433 00:19:16,547 --> 00:19:19,130 Now sometimes the internet's a little slower, a little faster, 434 00:19:19,130 --> 00:19:20,338 might take a while to scroll. 435 00:19:20,338 --> 00:19:23,730 But it took a total of .56 seconds to spell check the King James 436 00:19:23,730 --> 00:19:25,150 Bible using the staff solution. 437 00:19:25,150 --> 00:19:25,620 So pretty good. 438 00:19:25,620 --> 00:19:27,786 Yours might be much slower, and that's totally fine. 439 00:19:27,786 --> 00:19:30,090 But it is correct here. 440 00:19:30,090 --> 00:19:32,440 So that's the staff solution. 441 00:19:32,440 --> 00:19:34,785 If I go in and clean up the PHP version. 442 00:19:34,785 --> 00:19:36,990 And I'm going to do a little like baking show thing. 443 00:19:36,990 --> 00:19:39,720 We're going to take the code I wrote earlier, so it's perfectly correct. 444 00:19:39,720 --> 00:19:42,678 Because the code I wrote is not going to be perfectly correct just now. 445 00:19:42,678 --> 00:19:47,310 But if I run the PHP version, what do I have to give up today? 446 00:19:47,310 --> 00:19:49,980 Took me five minutes to implement p set five, I claim. 447 00:19:49,980 --> 00:19:51,770 What price have I paid? 448 00:19:51,770 --> 00:19:52,270 Yeah. 449 00:19:52,270 --> 00:19:52,937 >> AUDIENCE: Speed. 450 00:19:52,937 --> 00:19:53,645 SPEAKER 1: Speed. 451 00:19:53,645 --> 00:19:54,380 What do you mean? 452 00:19:54,380 --> 00:19:55,230 >> AUDIENCE: It's going to take longer. 453 00:19:55,230 --> 00:19:55,660 >> SPEAKER 1: All right. 454 00:19:55,660 --> 00:19:56,160 Let's see. 455 00:19:56,160 --> 00:19:59,590 It's going to take longer, not to write, but to run it. 456 00:19:59,590 --> 00:20:00,090 Yeah. 457 00:20:00,090 --> 00:20:00,960 So let's try this. 458 00:20:00,960 --> 00:20:05,720 So here I'm going to go ahead and do PHP of speller, and because the file's 459 00:20:05,720 --> 00:20:06,360 called speller. 460 00:20:06,360 --> 00:20:07,580 So I'm not running the dictionary, recall, 461 00:20:07,580 --> 00:20:09,204 I'm running the program called speller. 462 00:20:09,204 --> 00:20:12,450 I'm going to pass in the same file, King James the fifth. 463 00:20:12,450 --> 00:20:14,377 It's flying by. 464 00:20:14,377 --> 00:20:17,210 This could just be internet speed, so don't read too much into that. 465 00:20:17,210 --> 00:20:21,680 But it does feel longer for sure. 466 00:20:21,680 --> 00:20:23,280 1.26 seconds. 467 00:20:23,280 --> 00:20:25,150 >> Now that's still pretty damn fast, right? 468 00:20:25,150 --> 00:20:28,140 And the only reason that felt like more like three, or four, or five 469 00:20:28,140 --> 00:20:30,515 seconds, that's just because the internet was being slow. 470 00:20:30,515 --> 00:20:33,980 The computer time spent was 1.26 seconds, versus I think 0.56. 471 00:20:33,980 --> 00:20:35,880 So more than twice as slow. 472 00:20:35,880 --> 00:20:39,170 Now that's still pretty darn fast, but it's a manifestation 473 00:20:39,170 --> 00:20:40,860 of, indeed, that exact price. 474 00:20:40,860 --> 00:20:43,460 >> Because we're interpreting in the code line by line, 475 00:20:43,460 --> 00:20:46,740 and that program PHP is reading my code top to bottom, left to right, 476 00:20:46,740 --> 00:20:47,940 it has to do more thinking. 477 00:20:47,940 --> 00:20:50,430 It has to kind of convert it inside of itself 478 00:20:50,430 --> 00:20:53,464 to the corresponding machine code on the fly, so to speak, 479 00:20:53,464 --> 00:20:55,380 even though the mechanics are a bit different. 480 00:20:55,380 --> 00:20:59,430 Instead of just feeding to the Intel CPU the raw zeros and ones 481 00:20:59,430 --> 00:21:00,930 that it understands natively. 482 00:21:00,930 --> 00:21:04,110 >> So absolutely, hands down, we have paid a price. 483 00:21:04,110 --> 00:21:09,120 And code written in a language like PHP tends to be slower. 484 00:21:09,120 --> 00:21:10,050 But my god. 485 00:21:10,050 --> 00:21:12,740 So now when I spell check my file, I spend an extra 0.7 486 00:21:12,740 --> 00:21:17,280 or so seconds vs 20 hours to implement a faster spell checker, right? 487 00:21:17,280 --> 00:21:18,397 It's kind of a trade off. 488 00:21:18,397 --> 00:21:20,980 And if you're just starting p set five, might not be 20 hours. 489 00:21:20,980 --> 00:21:22,188 Might be far fewer than that. 490 00:21:22,188 --> 00:21:23,600 But it's a trade off, for real. 491 00:21:23,600 --> 00:21:27,820 And if you're running code on really big data sets, or on even older hardware, 492 00:21:27,820 --> 00:21:32,650 those kinds of differences can certainly add up. 493 00:21:32,650 --> 00:21:34,880 >> Any questions thus far? 494 00:21:34,880 --> 00:21:35,617 Yeah. 495 00:21:35,617 --> 00:21:36,492 >> AUDIENCE: [INAUDIBLE] 496 00:21:36,492 --> 00:21:38,187 497 00:21:38,187 --> 00:21:39,978 SPEAKER 1: Sorry, can you say it once more? 498 00:21:39,978 --> 00:21:40,853 AUDIENCE: [INAUDIBLE] 499 00:21:40,853 --> 00:21:43,326 500 00:21:43,326 --> 00:21:44,700 SPEAKER 1: You're giving up time. 501 00:21:44,700 --> 00:21:45,283 Well, so yeah. 502 00:21:45,283 --> 00:21:47,322 You are saving time by not having to compile it, 503 00:21:47,322 --> 00:21:48,530 if that's what you're asking. 504 00:21:48,530 --> 00:21:50,710 And indeed, in CS50, most of the programs, 505 00:21:50,710 --> 00:21:53,668 even though they might take a while to write, they're relatively short. 506 00:21:53,668 --> 00:21:56,860 Few dozen, few hundred lines of code, and so they compile pretty quickly. 507 00:21:56,860 --> 00:21:58,470 But when you start writing bigger programs, 508 00:21:58,470 --> 00:22:01,511 like if you're Microsoft or you're Google and writing really big programs 509 00:22:01,511 --> 00:22:05,360 in C, or C++, or similarly compiled languages, could take many seconds, 510 00:22:05,360 --> 00:22:08,577 or even minutes or longer to compile millions of lines of code. 511 00:22:08,577 --> 00:22:10,660 And there too, that difference is going to add up. 512 00:22:10,660 --> 00:22:12,840 >> And in fact, once we transition today, same day 513 00:22:12,840 --> 00:22:15,910 today, to web programming using PHP, you're 514 00:22:15,910 --> 00:22:18,460 going to find it just so much more pleasurable to write code 515 00:22:18,460 --> 00:22:21,501 when you don't have to do these stupid steps like change to your terminal 516 00:22:21,501 --> 00:22:24,202 window, type make hello, re run it, reload the window. 517 00:22:24,202 --> 00:22:25,910 Just so many stupid mechanical steps that 518 00:22:25,910 --> 00:22:28,480 just get in the way of doing the interesting, the fun work. 519 00:22:28,480 --> 00:22:31,450 And indeed we throw that away with PHP. 520 00:22:31,450 --> 00:22:34,960 And we can interact with it more rapidly. 521 00:22:34,960 --> 00:22:35,495 Yeah. 522 00:22:35,495 --> 00:22:36,370 AUDIENCE: [INAUDIBLE] 523 00:22:36,370 --> 00:22:40,294 524 00:22:40,294 --> 00:22:40,960 SPEAKER 1: Sure. 525 00:22:40,960 --> 00:22:42,876 Can I clarify what it means to be interpreted? 526 00:22:42,876 --> 00:22:45,870 When you compile a language like C, it goes down 527 00:22:45,870 --> 00:22:48,140 as we said to machine code, zeros and ones. 528 00:22:48,140 --> 00:22:51,860 And Intel decided years ago that certain patterns of zeros and ones 529 00:22:51,860 --> 00:22:56,220 represent addition, or subtraction, or print, or other basic operations. 530 00:22:56,220 --> 00:23:00,250 In the world of an interpreted language like PHP, or JavaScript, or Python, 531 00:23:00,250 --> 00:23:03,120 or Ruby, or bunches of others, instead someone 532 00:23:03,120 --> 00:23:05,120 has written an interpreter-- in this case, 533 00:23:05,120 --> 00:23:09,770 it's also called PHP, identical to the name of the language-- that essentially 534 00:23:09,770 --> 00:23:13,840 has a big loop in it that iterates over all of the lines of the code 535 00:23:13,840 --> 00:23:18,170 that I feed it as input, like hello dot PHP, or dictionary dot PHP. 536 00:23:18,170 --> 00:23:21,760 >> And then you can think of their big loop as having a lot of conditions. 537 00:23:21,760 --> 00:23:26,340 And those conditions say, if the human has written the keyword for, 538 00:23:26,340 --> 00:23:28,470 start doing his or her code again and again. 539 00:23:28,470 --> 00:23:31,360 Or if the human has written the line of code, 540 00:23:31,360 --> 00:23:34,880 if, only execute their next line conditionally. 541 00:23:34,880 --> 00:23:37,740 So it's truly like interpreting it in a human sense, line 542 00:23:37,740 --> 00:23:39,660 by line, and that just takes time. 543 00:23:39,660 --> 00:23:41,340 It takes overhead. 544 00:23:41,340 --> 00:23:44,189 And so that's a price-- good question-- we pay. 545 00:23:44,189 --> 00:23:46,480 So let's do another demo that's a little more dramatic. 546 00:23:46,480 --> 00:23:51,160 There is, I'd say, a ballpark this at a 90% probability of failing horribly, 547 00:23:51,160 --> 00:23:53,869 but you will be amazed with 10% probability. 548 00:23:53,869 --> 00:23:55,660 So every year we try to do this, whereby we 549 00:23:55,660 --> 00:23:59,061 try to write a program that goes through problem set zero, for which you've 550 00:23:59,061 --> 00:24:02,060 submitted your phone numbers and a lot of other demographic information, 551 00:24:02,060 --> 00:24:06,030 and we programmatically send you a text message. 552 00:24:06,030 --> 00:24:08,540 And then everyone's phone starts beeping or vibrating, 553 00:24:08,540 --> 00:24:11,460 and it's just amazing, because well, look at what computers can do. 554 00:24:11,460 --> 00:24:13,840 >> This rarely works correctly it seems, in large part 555 00:24:13,840 --> 00:24:16,800 because I typically get blacklisted by Gmail or some other service 556 00:24:16,800 --> 00:24:19,630 for actually trying to do this on scale. 557 00:24:19,630 --> 00:24:24,940 But funny thing is too, since we don't have everyone here today, 558 00:24:24,940 --> 00:24:27,690 fun fact the last time we tried this two years ago, 559 00:24:27,690 --> 00:24:30,870 I think I wrote a program that sat in a loop iterating 560 00:24:30,870 --> 00:24:34,040 over all the students in the class, and each of them a text message, 561 00:24:34,040 --> 00:24:37,534 and it said something like, why aren't you in lecture? 562 00:24:37,534 --> 00:24:39,170 Love, CS50 bot. 563 00:24:39,170 --> 00:24:42,580 >> You have no idea how many apologetic and sort of painful emails 564 00:24:42,580 --> 00:24:46,970 I've received in response from the n minus 100 people who weren't here 565 00:24:46,970 --> 00:24:47,960 that day. 566 00:24:47,960 --> 00:24:52,116 And even funnier, more shameful story-- you know, let's put it out there. 567 00:24:52,116 --> 00:24:53,740 It's already on video a few years back. 568 00:24:53,740 --> 00:24:56,810 So you will soon see me write a for loop, whereby 569 00:24:56,810 --> 00:25:00,640 it's very easy to write a bug in that for loop, such that the first time 570 00:25:00,640 --> 00:25:02,800 through the loop, you send one text message. 571 00:25:02,800 --> 00:25:06,190 >> The second time through the loop I should have sent one more text message, 572 00:25:06,190 --> 00:25:08,250 and then one more text message, and one more. 573 00:25:08,250 --> 00:25:11,490 But it turns out if you make a typo when programming, sometimes you 574 00:25:11,490 --> 00:25:14,460 can write programs that send one text message, then two text messages, 575 00:25:14,460 --> 00:25:18,742 then three text messages to everyone who's already received a text message. 576 00:25:18,742 --> 00:25:21,450 And as you know, doing something, plus something, plus something, 577 00:25:21,450 --> 00:25:25,910 plus something, is big O of a lot of text messages, or n squared, 578 00:25:25,910 --> 00:25:29,830 or at $0.10 a text message, $20,000. 579 00:25:29,830 --> 00:25:33,700 >> Thankfully I hit Control-C before that happened, but I did owe at least one 580 00:25:33,700 --> 00:25:37,550 of your predecessors $20 for the 200 some odd text messages that, 581 00:25:37,550 --> 00:25:40,450 not only went to his phone, but also it was like an older flip phone 582 00:25:40,450 --> 00:25:43,520 so it like pushed out of his memory every other personal text 583 00:25:43,520 --> 00:25:45,930 message he had actually gotten. 584 00:25:45,930 --> 00:25:49,250 So we have one goal today, not to do that. 585 00:25:49,250 --> 00:25:50,620 Maybe turn on airplane mode. 586 00:25:50,620 --> 00:25:51,120 All right. 587 00:25:51,120 --> 00:25:56,590 So let's go into a program here callled-- 588 00:25:56,590 --> 00:25:59,060 that I will call text dot PHP. 589 00:25:59,060 --> 00:26:01,074 And in advance, I've got two files here. 590 00:26:01,074 --> 00:26:06,030 I made a file for just staff, that's got all of the staff's info, all the names 591 00:26:06,030 --> 00:26:07,260 from the course's website. 592 00:26:07,260 --> 00:26:09,756 And then for now I just put some fake numbers 555-1212. 593 00:26:09,756 --> 00:26:11,380 So these wouldn't actually go anywhere. 594 00:26:11,380 --> 00:26:15,140 But notice the inside of this CSV file, which we talked about briefly before 595 00:26:15,140 --> 00:26:16,540 in the context of file IO. 596 00:26:16,540 --> 00:26:18,730 >> What's a CSV file? 597 00:26:18,730 --> 00:26:19,970 Comma separated variables. 598 00:26:19,970 --> 00:26:24,400 And this is kind of like a very lightweight database, if you will. 599 00:26:24,400 --> 00:26:28,650 There's kind of sort of four columns in this file, and there jagged, 600 00:26:28,650 --> 00:26:32,400 but the commas essentially represent columns in the file. 601 00:26:32,400 --> 00:26:35,400 The first column is the TF or CA's name. 602 00:26:35,400 --> 00:26:37,800 Second column is their last name. 603 00:26:37,800 --> 00:26:39,820 Third column is their phone number. 604 00:26:39,820 --> 00:26:44,760 And fourth column apparently is-- fourth column is apparently what? 605 00:26:44,760 --> 00:26:45,870 OK, so it's their carrier. 606 00:26:45,870 --> 00:26:47,180 So Verizon, or Sprint, or what not. 607 00:26:47,180 --> 00:26:48,620 And if I misspoke earlier, CSV. 608 00:26:48,620 --> 00:26:51,800 Comma separated values is CSV here. 609 00:26:51,800 --> 00:26:54,260 >> So what can I do with this? 610 00:26:54,260 --> 00:26:56,940 Well this is just a big text file, and it's kind of long, right? 611 00:26:56,940 --> 00:26:58,690 Like this is going to be kind of annoying. 612 00:26:58,690 --> 00:27:03,510 And it turns out, though, that if I want to send a text message to, let's 613 00:27:03,510 --> 00:27:05,180 say Alex here. 614 00:27:05,180 --> 00:27:09,690 Let me go into my browser and exit the full screen mode 615 00:27:09,690 --> 00:27:11,660 just so I can toggle back and forth. 616 00:27:11,660 --> 00:27:20,280 It turns out, let's see, if I go in here, and log into Gmail, all right. 617 00:27:20,280 --> 00:27:22,950 Don't look at this part. 618 00:27:22,950 --> 00:27:23,880 Just I get to see. 619 00:27:23,880 --> 00:27:26,551 620 00:27:26,551 --> 00:27:28,800 of course the livestream can still see what I'm doing. 621 00:27:28,800 --> 00:27:29,360 All right. 622 00:27:29,360 --> 00:27:31,850 >> So here is just Gmail with John Harvard's account. 623 00:27:31,850 --> 00:27:34,000 So if I want to send a text message to Alex, 624 00:27:34,000 --> 00:27:35,530 I can of course compose a message. 625 00:27:35,530 --> 00:27:38,410 And it turns out, and I've tested it with Alex before here, 626 00:27:38,410 --> 00:27:42,260 so you can append a certain domain name to people's phone numbers, 627 00:27:42,260 --> 00:27:45,850 because there exists in the world things called SMS to email gateways. 628 00:27:45,850 --> 00:27:50,000 Which is a fancy way of saying all of us who have mobile phones that 629 00:27:50,000 --> 00:27:53,200 have phone numbers, of course, there probably is for your carrier, 630 00:27:53,200 --> 00:27:56,840 a certain suffix like at Vtext.com for Verizon text message 631 00:27:56,840 --> 00:28:00,320 dot com, that you can append to your own personal phone number, and then 632 00:28:00,320 --> 00:28:03,540 you or friends, or your parents can text you at that particular address. 633 00:28:03,540 --> 00:28:07,380 >> And Verizon has a server, an email server, that upon receiving this email, 634 00:28:07,380 --> 00:28:11,180 looks at the numeric part and then uses whatever special industry magic 635 00:28:11,180 --> 00:28:14,540 they have to actually send it out over the airwaves to your particular cell 636 00:28:14,540 --> 00:28:15,040 phone. 637 00:28:15,040 --> 00:28:18,030 Now this is nice because I don't really know how to send a text message 638 00:28:18,030 --> 00:28:20,460 or to write code yet for my mobile phone, 639 00:28:20,460 --> 00:28:23,580 and you might do exactly that for a final project, but for now all 640 00:28:23,580 --> 00:28:24,770 I have is my laptop here. 641 00:28:24,770 --> 00:28:29,830 So I want to iterate over that CSV file, line, by line, by line, by line, 642 00:28:29,830 --> 00:28:35,160 grab each of the staff's phone numbers, and their carrier and programatically, 643 00:28:35,160 --> 00:28:39,770 concatenate, that is, connect the phone number with the appropriate domain name 644 00:28:39,770 --> 00:28:40,820 and then send an email. 645 00:28:40,820 --> 00:28:41,940 >> So that's a lot of steps. 646 00:28:41,940 --> 00:28:45,530 And good luck doing that in C. It is a nightmare of a situation 647 00:28:45,530 --> 00:28:48,350 to open a file, to read it line, by line, by line, as you 648 00:28:48,350 --> 00:28:51,100 are seeing, or may soon see with problem set five, if not 649 00:28:51,100 --> 00:28:53,560 problem set four, and then to dynamically concatenate 650 00:28:53,560 --> 00:28:57,020 two strings together, because in C, to take two strings and combine them, 651 00:28:57,020 --> 00:29:00,900 what do you minimally need to do most likely? 652 00:29:00,900 --> 00:29:03,020 You need to declare more memory, right? 653 00:29:03,020 --> 00:29:04,710 And ask, malloc, can I have more memory? 654 00:29:04,710 --> 00:29:07,060 So I can put half of this here, half of this here. 655 00:29:07,060 --> 00:29:08,150 It's just so many steps. 656 00:29:08,150 --> 00:29:10,310 And by that point you've lost interest in the stupid little demo 657 00:29:10,310 --> 00:29:11,851 where you make everyone's phone beep. 658 00:29:11,851 --> 00:29:14,590 Let's do this sort of PHP style, whereby we just 659 00:29:14,590 --> 00:29:16,210 start using more of that kitchen sink. 660 00:29:16,210 --> 00:29:18,050 >> And the ideas are fundamentally the same, 661 00:29:18,050 --> 00:29:20,710 but we don't have to worry too much about that lower level. 662 00:29:20,710 --> 00:29:22,950 So let's see how I might go about doing this. 663 00:29:22,950 --> 00:29:26,664 And just so that I don't repeat past mistakes, 664 00:29:26,664 --> 00:29:28,330 I've written some notes for myself here. 665 00:29:28,330 --> 00:29:31,070 And let's see if I can walk us through some of the fundamental steps, 666 00:29:31,070 --> 00:29:33,861 and then for time's sake we'll perhaps cut some corners in the end. 667 00:29:33,861 --> 00:29:38,007 I'm going to go ahead and do open bracket PHP to start this file. 668 00:29:38,007 --> 00:29:39,590 And I'm going to go ahead and do this. 669 00:29:39,590 --> 00:29:41,490 File handle, as you'll soon see, actually 670 00:29:41,490 --> 00:29:44,790 calls a function called f open. 671 00:29:44,790 --> 00:29:46,280 And remember this opens a file. 672 00:29:46,280 --> 00:29:48,800 And the quote, unquote, r means what, again? 673 00:29:48,800 --> 00:29:49,800 Just open it for read. 674 00:29:49,800 --> 00:29:51,210 Now in p set for, you did this. 675 00:29:51,210 --> 00:29:54,500 And handle could be anything, fu, bar, bas, any variable name. 676 00:29:54,500 --> 00:29:57,470 But generally a computer scientist would call an open file, 677 00:29:57,470 --> 00:30:00,962 giving you a file handle, something to hold onto so to speak. 678 00:30:00,962 --> 00:30:02,670 I'm just going to do some error checking. 679 00:30:02,670 --> 00:30:07,260 So if the handle is false, just like in C, I'm going to do something like, 680 00:30:07,260 --> 00:30:09,545 could not open file, backslash n. 681 00:30:09,545 --> 00:30:11,420 And then I'm just going to go ahead and exit. 682 00:30:11,420 --> 00:30:14,340 And it turns out, in PHP, I'm not inside a function. 683 00:30:14,340 --> 00:30:16,160 There's no main function here. 684 00:30:16,160 --> 00:30:17,450 So I don't return, per se. 685 00:30:17,450 --> 00:30:18,120 I exit. 686 00:30:18,120 --> 00:30:19,619 Which is essentially the same thing. 687 00:30:19,619 --> 00:30:22,510 And I'm exiting with one y probably. 688 00:30:22,510 --> 00:30:24,220 One just means an error of some sort. 689 00:30:24,220 --> 00:30:25,580 It's non-zero, so it's an error. 690 00:30:25,580 --> 00:30:30,220 >> All right, so if I'm down here now in my program I have opened the file. 691 00:30:30,220 --> 00:30:34,250 I'm going to go ahead and declare an array called addresses, 692 00:30:34,250 --> 00:30:38,610 and in C I would have to know the size of the array in advance, right? 693 00:30:38,610 --> 00:30:41,910 Or I would need to declare this is a pointer, and then use malloc. 694 00:30:41,910 --> 00:30:44,720 And then every time I read through another row in the file, 695 00:30:44,720 --> 00:30:46,620 I'm going to need to malloc more memory, malloc more memory. 696 00:30:46,620 --> 00:30:47,703 That's a pain in the neck. 697 00:30:47,703 --> 00:30:50,950 And thankfully, PHP, and Ruby, and Python, and JavaScript, they 698 00:30:50,950 --> 00:30:53,880 will grow your arrays for you automatically. 699 00:30:53,880 --> 00:30:54,830 No more malloc. 700 00:30:54,830 --> 00:30:56,227 No more memory management. 701 00:30:56,227 --> 00:30:57,560 The computer takes care of that. 702 00:30:57,560 --> 00:31:00,330 But the price you pay. 703 00:31:00,330 --> 00:31:01,350 It's a little slower. 704 00:31:01,350 --> 00:31:03,190 And for a program like this we're not going to notice. 705 00:31:03,190 --> 00:31:04,720 We're going to send 100 text messages. 706 00:31:04,720 --> 00:31:06,136 For instance not going to feel it. 707 00:31:06,136 --> 00:31:08,430 IT really only starts to matter your language of choice 708 00:31:08,430 --> 00:31:11,490 when your data or your programs get really, really sizable, 709 00:31:11,490 --> 00:31:14,120 as will be the case more so with our web based stuff. 710 00:31:14,120 --> 00:31:15,450 >> But for now let's forge ahead. 711 00:31:15,450 --> 00:31:20,120 While it turns out there's a function in C called f get CSV. 712 00:31:20,120 --> 00:31:24,570 File get CSV, that takes a file handle as its argument, 713 00:31:24,570 --> 00:31:28,810 and it proceeds then, row, by row, by row, to read in a row. 714 00:31:28,810 --> 00:31:30,964 So it just reads a line of text from the file. 715 00:31:30,964 --> 00:31:34,130 But what's nice about this function, it doesn't just hand me a line of text. 716 00:31:34,130 --> 00:31:37,320 It looks for those commas, and parses the line. 717 00:31:37,320 --> 00:31:41,740 And to parse the line means to split it on certain values in this context. 718 00:31:41,740 --> 00:31:45,510 >> In other words, dollar sign row is a variable that's 719 00:31:45,510 --> 00:31:47,010 going to give me a bunch of indexes. 720 00:31:47,010 --> 00:31:49,635 This is going to be the first column in my CSV. 721 00:31:49,635 --> 00:31:52,510 This is going to be the second column, this is going to be the third, 722 00:31:52,510 --> 00:31:53,968 and this is going to be the fourth. 723 00:31:53,968 --> 00:31:57,440 Because recall, and Excel the not cooperate the last time we did this, 724 00:31:57,440 --> 00:32:03,350 but if I download staff dot CSV. 725 00:32:03,350 --> 00:32:04,940 Whoops, not rename. 726 00:32:04,940 --> 00:32:09,120 If I download staff dot CSV, and try opening it, 727 00:32:09,120 --> 00:32:12,510 I've already registered-- oh I didn't update Excel since last time. 728 00:32:12,510 --> 00:32:15,880 You'll see that in staff dot CSV, I have four columns. 729 00:32:15,880 --> 00:32:18,720 So when I read in the first row for Abby here, 730 00:32:18,720 --> 00:32:23,582 dollar sign row 0 is Abby, dollar sign row one is Lyons, 731 00:32:23,582 --> 00:32:27,740 dollar sign row two is her number, and then her phone's carrier. 732 00:32:27,740 --> 00:32:28,410 >> So that's all. 733 00:32:28,410 --> 00:32:30,740 And that's nice, because I don't have to now figure out 734 00:32:30,740 --> 00:32:32,800 where all of those commas actually are. 735 00:32:32,800 --> 00:32:35,300 So I'm going to go ahead and do this, given that definition, 736 00:32:35,300 --> 00:32:38,960 I'm going to say that her name is in row bracket zero, as promised. 737 00:32:38,960 --> 00:32:41,150 And I'm going to grab-- and actually, you know what? 738 00:32:41,150 --> 00:32:42,780 I'm not going to worry about her name, in this case. 739 00:32:42,780 --> 00:32:43,988 I'm going to keep it simpler. 740 00:32:43,988 --> 00:32:48,610 I'm going to do her number is in row bracket two, I think. 741 00:32:48,610 --> 00:32:50,510 And her carrier was in where? 742 00:32:50,510 --> 00:32:53,405 Row three, one over. 743 00:32:53,405 --> 00:32:54,280 So nothing new there. 744 00:32:54,280 --> 00:32:56,770 It's just zero indexing into an array. 745 00:32:56,770 --> 00:32:58,760 >> And now, I'm going to do the following. 746 00:32:58,760 --> 00:33:03,930 If the carrier equals equals AT&T-- I'm just going to have a loop-- 747 00:33:03,930 --> 00:33:06,790 I want to create an email address that looks like this. 748 00:33:06,790 --> 00:33:12,320 It's going to be 617-555-1212 at, what is it, text.ATT.net, 749 00:33:12,320 --> 00:33:13,490 is what I want to do. 750 00:33:13,490 --> 00:33:15,260 So how do I do this? 751 00:33:15,260 --> 00:33:16,730 I'm going to do the following. 752 00:33:16,730 --> 00:33:22,460 Her address is going to be her number, and now, let me go ahead and do this. 753 00:33:22,460 --> 00:33:23,760 This is some funky syntax. 754 00:33:23,760 --> 00:33:27,250 I could do the percent s trick, but I can actually just do this. 755 00:33:27,250 --> 00:33:29,900 >> So this is a PHP thing, but let's consider what I'm doing. 756 00:33:29,900 --> 00:33:31,630 Dollars sign address on the left, just a variable. 757 00:33:31,630 --> 00:33:32,300 Give me a variable. 758 00:33:32,300 --> 00:33:33,390 I don't have to specify the type. 759 00:33:33,390 --> 00:33:34,570 PHP will figure it out. 760 00:33:34,570 --> 00:33:36,580 On the right hand side, what's the data type 761 00:33:36,580 --> 00:33:38,399 of that whole thing on the right hand side? 762 00:33:38,399 --> 00:33:40,190 Looks like a string, because there's double 763 00:33:40,190 --> 00:33:42,280 quotes on the far left and far right. 764 00:33:42,280 --> 00:33:44,210 Now there's some funky new syntax here. 765 00:33:44,210 --> 00:33:47,550 There's the curly braces, and the dollar sign number. 766 00:33:47,550 --> 00:33:52,860 But take a guess what is that syntax probably telling PHP to do for me? 767 00:33:52,860 --> 00:33:53,360 Yeah. 768 00:33:53,360 --> 00:33:54,630 Just insert the value there. 769 00:33:54,630 --> 00:33:55,530 So no percent s. 770 00:33:55,530 --> 00:33:58,160 We could use print f, or s print f or something like it, 771 00:33:58,160 --> 00:34:00,070 but PHP and a lot of higher level languages, 772 00:34:00,070 --> 00:34:03,070 you're going to see that you could do the same thing functionally, maybe 773 00:34:03,070 --> 00:34:04,019 six different ways. 774 00:34:04,019 --> 00:34:06,310 And so it starts to become a matter of design or style. 775 00:34:06,310 --> 00:34:09,902 So this is just a cryptic looking way of saying, give me an email address, 776 00:34:09,902 --> 00:34:12,110 but plug-in the number in between these curly braces. 777 00:34:12,110 --> 00:34:15,440 And the curly braces will not end up in the final address. 778 00:34:15,440 --> 00:34:18,010 >> Now we can skip over some of these lines for time's sake. 779 00:34:18,010 --> 00:34:21,210 So if a carrier equals equals verHorizon-- whoops, 780 00:34:21,210 --> 00:34:24,989 Verizon-- I want to do something very similar, where the address gets, 781 00:34:24,989 --> 00:34:27,569 quote, unquote, number at text.ATT.net. 782 00:34:27,569 --> 00:34:30,380 783 00:34:30,380 --> 00:34:35,050 And then I can do the same kind of thing for the rest of the carriers. 784 00:34:35,050 --> 00:34:37,090 But I'll just do dot, dot, dot for now. 785 00:34:37,090 --> 00:34:43,050 And now let's suppose that I want to add array, push. 786 00:34:43,050 --> 00:34:45,969 I want to add to the array, called addresses. 787 00:34:45,969 --> 00:34:50,409 >> This address, this is how you add something to an array in PHP. 788 00:34:50,409 --> 00:34:51,610 You don't need to do malloc. 789 00:34:51,610 --> 00:34:53,068 You don't have to resize the array. 790 00:34:53,068 --> 00:34:54,259 You just say array, push. 791 00:34:54,259 --> 00:34:57,300 What do you want to push-- what array do you want to push something onto? 792 00:34:57,300 --> 00:34:58,000 Addresses. 793 00:34:58,000 --> 00:34:59,550 What you want to push onto addresses? 794 00:34:59,550 --> 00:35:00,350 Address. 795 00:35:00,350 --> 00:35:02,430 And in fact, if you really want to be clever, 796 00:35:02,430 --> 00:35:05,610 some syntactic sugar for this, so to speak, 797 00:35:05,610 --> 00:35:10,230 which just means how can you do this in the prettier way, would be to do this. 798 00:35:10,230 --> 00:35:14,060 That too has the effect of growing the addresses array by size one, 799 00:35:14,060 --> 00:35:17,130 and then plop that additional email address into it. 800 00:35:17,130 --> 00:35:17,630 Yeah. 801 00:35:17,630 --> 00:35:19,730 >> AUDIENCE: [INAUDIBLE] 802 00:35:19,730 --> 00:35:22,520 >> SPEAKER 1: A typo on the else if-- oh, carrier. 803 00:35:22,520 --> 00:35:23,070 That's OK. 804 00:35:23,070 --> 00:35:26,971 It also won't like my failure to implement part of this program 805 00:35:26,971 --> 00:35:27,470 later on. 806 00:35:27,470 --> 00:35:28,810 But thank you for catching. 807 00:35:28,810 --> 00:35:30,671 One more bug. 808 00:35:30,671 --> 00:35:31,170 Thank you. 809 00:35:31,170 --> 00:35:33,746 We want this to be Vtext.com. 810 00:35:33,746 --> 00:35:34,340 Yes. 811 00:35:34,340 --> 00:35:34,839 All right. 812 00:35:34,839 --> 00:35:36,330 So where does this leave us? 813 00:35:36,330 --> 00:35:38,224 We've written the code to open the file. 814 00:35:38,224 --> 00:35:40,390 We have a loop to iterate over the rows in the file. 815 00:35:40,390 --> 00:35:43,650 We have code that adds to my array, one at a time, the correctly 816 00:35:43,650 --> 00:35:44,790 formatted email address. 817 00:35:44,790 --> 00:35:48,480 So all that really remains is to send an email to each of these people. 818 00:35:48,480 --> 00:35:52,830 >> So I've gone ahead and readied myself as follows. 819 00:35:52,830 --> 00:35:59,580 What I'm going to do here is-- and let's go ahead and skip to some actual code, 820 00:35:59,580 --> 00:36:00,620 which looks like this. 821 00:36:00,620 --> 00:36:02,950 So here is the pre-baked version I wrote earlier. 822 00:36:02,950 --> 00:36:04,700 And notice I finished implementing Sprint. 823 00:36:04,700 --> 00:36:06,130 I finished implementing T-Mobile. 824 00:36:06,130 --> 00:36:08,020 I finished implementing Virgin Mobile. 825 00:36:08,020 --> 00:36:09,622 And I will apologize in advance. 826 00:36:09,622 --> 00:36:12,080 There's a few carriers that some students in the class have 827 00:36:12,080 --> 00:36:14,640 that I didn't bother enumerating in the if condition here. 828 00:36:14,640 --> 00:36:16,015 So not all the texts will go out. 829 00:36:16,015 --> 00:36:17,556 But let's see what else I need to do. 830 00:36:17,556 --> 00:36:19,660 I close the file, just like in p set four. 831 00:36:19,660 --> 00:36:20,835 >> And this is new syntax. 832 00:36:20,835 --> 00:36:22,710 And we'll see a little bit of this over time, 833 00:36:22,710 --> 00:36:24,450 especially with p set seven and eight. 834 00:36:24,450 --> 00:36:28,140 But this is syntax for creating a special type of structure. 835 00:36:28,140 --> 00:36:31,490 It turns out there's a library that comes with PHP, called PHP mailer. 836 00:36:31,490 --> 00:36:33,840 Its purpose in life is to programmatically send emails. 837 00:36:33,840 --> 00:36:36,840 It's code someone else wrote that makes it easier for us to send emails, 838 00:36:36,840 --> 00:36:39,131 so we don't have to keep going back and forth to Gmail, 839 00:36:39,131 --> 00:36:41,151 and pasting in people's email addresses. 840 00:36:41,151 --> 00:36:44,150 There's a whole bunch of lines here that honestly, I just kind of copied 841 00:36:44,150 --> 00:36:45,660 and pasted from the documentation. 842 00:36:45,660 --> 00:36:47,409 And in fact, if you're curious, I left all 843 00:36:47,409 --> 00:36:50,550 of the URLs of the documentation in the code that we'll post later. 844 00:36:50,550 --> 00:36:52,360 And notice that among the things I'm doing 845 00:36:52,360 --> 00:36:55,710 is, I'm telling this library, use Gmail's server, right? 846 00:36:55,710 --> 00:36:59,360 SMTP we talked briefly about when we were at Yale about being simple mail 847 00:36:59,360 --> 00:37:00,110 transfer protocol. 848 00:37:00,110 --> 00:37:02,920 It's the protocol that servers used to send email, 849 00:37:02,920 --> 00:37:05,150 and that's-- Gmail has one of those. 850 00:37:05,150 --> 00:37:07,290 A port, this is the TCP port number. 851 00:37:07,290 --> 00:37:09,630 It's fine if you haven't seen 587 before. 852 00:37:09,630 --> 00:37:11,420 Just know that from the documentation. 853 00:37:11,420 --> 00:37:12,250 Here's my username. 854 00:37:12,250 --> 00:37:13,670 I'm going to use the CF50 bot. 855 00:37:13,670 --> 00:37:16,690 And in a moment, I'm just going to type in the bot's actual password. 856 00:37:16,690 --> 00:37:18,690 And then down here, notice what remains. 857 00:37:18,690 --> 00:37:22,840 >> I set my from address to be bot at CS50.net. 858 00:37:22,840 --> 00:37:26,240 I think I won't regret sending a text message to 800 people that 859 00:37:26,240 --> 00:37:30,590 says, miss you, love CS50 bot, perhaps for those who couldn't make it today. 860 00:37:30,590 --> 00:37:33,160 And then in line 76, what do I do? 861 00:37:33,160 --> 00:37:39,010 This kind of looks like C. So for i equals 0, n equals-- now count is new. 862 00:37:39,010 --> 00:37:42,077 It turns out that if you want to get the number of things in an array, 863 00:37:42,077 --> 00:37:43,660 you don't have to remember it anymore. 864 00:37:43,660 --> 00:37:45,370 You can just ask a function called count, 865 00:37:45,370 --> 00:37:48,320 and it will tell you how many addresses are in that array. 866 00:37:48,320 --> 00:37:53,210 >> And then I'm going to add the address to the email. 867 00:37:53,210 --> 00:37:55,090 And I know this from using the documentation. 868 00:37:55,090 --> 00:37:56,900 There's an add address function. 869 00:37:56,900 --> 00:37:58,980 And now notice there is one piece of syntax. 870 00:37:58,980 --> 00:38:01,470 We've seen this before in C. Similar in spirit. 871 00:38:01,470 --> 00:38:04,100 You can think of dollar sign mail as a variable, which it is. 872 00:38:04,100 --> 00:38:08,100 But it's a struct inside of which are not only properties, 873 00:38:08,100 --> 00:38:09,400 variables if you will. 874 00:38:09,400 --> 00:38:13,480 But it turns out that in PHP, which is an object oriented programming 875 00:38:13,480 --> 00:38:16,520 language, like Java if you took APCS. 876 00:38:16,520 --> 00:38:21,420 >> Dollar sign mail, if you want to-- it also has functions inside of it, 877 00:38:21,420 --> 00:38:22,380 or methods. 878 00:38:22,380 --> 00:38:24,720 So this is to say that special PHP mailer 879 00:38:24,720 --> 00:38:29,280 library, if I've got a variable thereof, if I want to call a function that 880 00:38:29,280 --> 00:38:31,559 comes with that library, for today's purposes, 881 00:38:31,559 --> 00:38:33,100 just know you use the arrow notation. 882 00:38:33,100 --> 00:38:34,150 There's no dot notation. 883 00:38:34,150 --> 00:38:35,220 It's just the arrow. 884 00:38:35,220 --> 00:38:36,380 But that's not a pointer. 885 00:38:36,380 --> 00:38:38,680 It's just borrowed from C's syntax. 886 00:38:38,680 --> 00:38:43,620 So call add address inside of this library's object or variable. 887 00:38:43,620 --> 00:38:44,440 >> All right. 888 00:38:44,440 --> 00:38:45,570 This is the magical line. 889 00:38:45,570 --> 00:38:46,740 That's how I send an email. 890 00:38:46,740 --> 00:38:50,040 That is the equivalent of opening up Gmail, typing it out, and hitting send. 891 00:38:50,040 --> 00:38:54,870 But it's going to instead send an email to this address, with this body, 892 00:38:54,870 --> 00:38:57,120 from the CS50 bot. 893 00:38:57,120 --> 00:38:58,710 And now key line. 894 00:38:58,710 --> 00:39:03,130 This line was absent in like 2011 when I think I last tried this. 895 00:39:03,130 --> 00:39:05,640 That line there, of course, clears the addresses. 896 00:39:05,640 --> 00:39:07,920 So logically, if I don't clear the address 897 00:39:07,920 --> 00:39:11,740 after each iteration, the first email or text message goes to Alice, 898 00:39:11,740 --> 00:39:14,320 the second goes to Alice and Bob, the third 899 00:39:14,320 --> 00:39:17,870 goes to Alice and Bob and Charlie, hence the very expensive mistake 900 00:39:17,870 --> 00:39:18,760 I made that year. 901 00:39:18,760 --> 00:39:21,080 >> So let's see. 902 00:39:21,080 --> 00:39:24,200 Are there any questions before we send you all a text 903 00:39:24,200 --> 00:39:28,210 message with 10 percent probability? 904 00:39:28,210 --> 00:39:28,940 Any questions? 905 00:39:28,940 --> 00:39:30,330 All right. 906 00:39:30,330 --> 00:39:36,100 Let's me go in here and change the password to what should work, 907 00:39:36,100 --> 00:39:37,780 will likely get blacklisted by Gmail. 908 00:39:37,780 --> 00:39:39,821 So they might not all go out, since they probably 909 00:39:39,821 --> 00:39:42,960 don't like us sending 800 emails in a for loop all at once. 910 00:39:42,960 --> 00:39:45,140 Since that's not particularly human behavior. 911 00:39:45,140 --> 00:39:47,460 >> Oh, I'm going to change one other thing. 912 00:39:47,460 --> 00:39:50,850 Up at the top, I'm going to sent-- change the file to students dot CSV. 913 00:39:50,850 --> 00:39:55,420 And I won't open this, but this is an identical CSV file with not 100 staff, 914 00:39:55,420 --> 00:39:58,970 but 800 students, just from Harvard, because Yale 915 00:39:58,970 --> 00:40:00,207 has October recess this week. 916 00:40:00,207 --> 00:40:03,290 And it seems like kind of a jerk thing to do to text all of them as to why 917 00:40:03,290 --> 00:40:04,750 they're not in class on holiday. 918 00:40:04,750 --> 00:40:05,250 That's OK. 919 00:40:05,250 --> 00:40:08,310 Just Harvard students today inside of that file. 920 00:40:08,310 --> 00:40:12,320 And now let's go back to my terminal window. 921 00:40:12,320 --> 00:40:13,710 I'm kind of nervous. 922 00:40:13,710 --> 00:40:22,000 >> OK so now I'm going to go into the directory, and PHP text dot PHP. 923 00:40:22,000 --> 00:40:24,950 How about one brave volunteer? 924 00:40:24,950 --> 00:40:26,690 So I don't want this on me. 925 00:40:26,690 --> 00:40:28,410 OK, come on up. 926 00:40:28,410 --> 00:40:29,162 What's your name? 927 00:40:29,162 --> 00:40:29,662 MAYA: Maya. 928 00:40:29,662 --> 00:40:30,328 SPEAKER 1: Maya. 929 00:40:30,328 --> 00:40:31,610 Come on up, Maya. 930 00:40:31,610 --> 00:40:34,120 All right. 931 00:40:34,120 --> 00:40:35,389 Let's see if this works. 932 00:40:35,389 --> 00:40:38,430 I put my own email address in the file-- my own phone number in the file, 933 00:40:38,430 --> 00:40:40,110 so that hopefully I'll get one as well. 934 00:40:40,110 --> 00:40:40,819 Nice to meet you. 935 00:40:40,819 --> 00:40:41,443 MAYA: Likewise. 936 00:40:41,443 --> 00:40:44,510 SPEAKER 1: So all you have to do is hit Enter to send 800 text messages, 937 00:40:44,510 --> 00:40:45,466 if I didn't screw up. 938 00:40:45,466 --> 00:40:51,106 939 00:40:51,106 --> 00:40:51,606 Nice. 940 00:40:51,606 --> 00:40:55,556 941 00:40:55,556 --> 00:40:58,430 Could take a while, statistically, until we get to an email address-- 942 00:40:58,430 --> 00:41:00,573 a text message that's in the room right now. 943 00:41:00,573 --> 00:41:03,880 944 00:41:03,880 --> 00:41:05,390 Let's linger and see. 945 00:41:05,390 --> 00:41:06,940 >> AUDIENCE: [WHISTLING] 946 00:41:06,940 --> 00:41:08,270 >> SPEAKER 1: Oh, yes. 947 00:41:08,270 --> 00:41:09,230 >> AUDIENCE: Oh, gotcha. 948 00:41:09,230 --> 00:41:16,720 949 00:41:16,720 --> 00:41:19,180 >> SPEAKER 1: For loops are-- this is not slow because of PHP. 950 00:41:19,180 --> 00:41:21,810 This is slow because Gmail is throttling us and not 951 00:41:21,810 --> 00:41:25,170 letting us send more than like one email per second. 952 00:41:25,170 --> 00:41:26,300 Anything? 953 00:41:26,300 --> 00:41:29,990 This was a hard thing for me to test at home with just me and my one phone, so. 954 00:41:29,990 --> 00:41:37,630 955 00:41:37,630 --> 00:41:41,111 Is that hopefully from the bot? 956 00:41:41,111 --> 00:41:41,610 Yes? 957 00:41:41,610 --> 00:41:42,110 No? 958 00:41:42,110 --> 00:41:44,760 959 00:41:44,760 --> 00:41:46,620 Say yes. 960 00:41:46,620 --> 00:41:47,479 No? 961 00:41:47,479 --> 00:41:49,374 It's from a friend? 962 00:41:49,374 --> 00:41:49,874 Awkward. 963 00:41:49,874 --> 00:41:50,374 OK. 964 00:41:50,374 --> 00:41:56,580 965 00:41:56,580 --> 00:42:00,090 Pretty sure it's working though. 966 00:42:00,090 --> 00:42:00,910 Uh oh. 967 00:42:00,910 --> 00:42:03,190 They black-- oh, wait a minute. 968 00:42:03,190 --> 00:42:04,280 Oh, you know what? 969 00:42:04,280 --> 00:42:06,100 One moment please. 970 00:42:06,100 --> 00:42:07,802 Only Maya and I-- Mia, was it? 971 00:42:07,802 --> 00:42:08,301 MAYA: Maya. 972 00:42:08,301 --> 00:42:12,110 SPEAKER 1: Maya, are going to know what I did wrong here. 973 00:42:12,110 --> 00:42:14,890 Oh, wait a minute. 974 00:42:14,890 --> 00:42:23,320 I think I just sent 100 text messages to 555-1212. 975 00:42:23,320 --> 00:42:24,346 Stand by. 976 00:42:24,346 --> 00:42:28,080 977 00:42:28,080 --> 00:42:29,270 I can't win any year. 978 00:42:29,270 --> 00:42:30,180 OK. 979 00:42:30,180 --> 00:42:30,680 Maya? 980 00:42:30,680 --> 00:42:33,530 981 00:42:33,530 --> 00:42:36,219 Fix that in post production. 982 00:42:36,219 --> 00:42:36,718 Dammit! 983 00:42:36,718 --> 00:42:39,430 984 00:42:39,430 --> 00:42:40,390 Following from address. 985 00:42:40,390 --> 00:42:41,301 OK, stand by. 986 00:42:41,301 --> 00:42:41,800 Sorry. 987 00:42:41,800 --> 00:42:44,550 988 00:42:44,550 --> 00:42:47,080 It's painful every year. 989 00:42:47,080 --> 00:42:48,640 OK, one moment. 990 00:42:48,640 --> 00:42:50,530 This is good. 991 00:42:50,530 --> 00:42:54,010 I am instead going to do this. 992 00:42:54,010 --> 00:42:55,470 Print address. 993 00:42:55,470 --> 00:42:56,030 OK, stand by. 994 00:42:56,030 --> 00:43:01,340 Print addresses, bracket i. 995 00:43:01,340 --> 00:43:02,710 Yeah, I like that. 996 00:43:02,710 --> 00:43:10,220 Dollar sign-- OK I'll explain what I'm doing in a moment, after I've run this. 997 00:43:10,220 --> 00:43:11,670 Every year. 998 00:43:11,670 --> 00:43:13,125 OK, here we go. 999 00:43:13,125 --> 00:43:15,750 I have just-- and I don't want to show everyone's phone number, 1000 00:43:15,750 --> 00:43:17,740 but Maya can confirm with a nod of the head, 1001 00:43:17,740 --> 00:43:20,800 that she sees everyone's phone number in the class 1002 00:43:20,800 --> 00:43:26,750 that I'm going to copy into a program called Gmail. 1003 00:43:26,750 --> 00:43:30,860 And if in Gmail, what do we want to say? 1004 00:43:30,860 --> 00:43:32,137 Miss you. 1005 00:43:32,137 --> 00:43:33,220 Love-- that's not from me. 1006 00:43:33,220 --> 00:43:35,230 I haven't even hit send yet. 1007 00:43:35,230 --> 00:43:39,450 So let's go ahead and do this, if I can zoom in. 1008 00:43:39,450 --> 00:43:40,300 OK. 1009 00:43:40,300 --> 00:43:42,480 So I'm using a program called Gmail now. 1010 00:43:42,480 --> 00:43:44,490 If Maya, you'd like to click the Send button, 1011 00:43:44,490 --> 00:43:46,970 we will simulate what that code should have done. 1012 00:43:46,970 --> 00:43:54,630 1013 00:43:54,630 --> 00:43:55,254 >> Anything? 1014 00:43:55,254 --> 00:43:56,062 >> [BEEPS] 1015 00:43:56,062 --> 00:43:56,870 >> AUDIENCE: Yep. 1016 00:43:56,870 --> 00:43:58,030 >> SPEAKER 1: Yes? 1017 00:43:58,030 --> 00:43:59,210 Salvation? 1018 00:43:59,210 --> 00:44:00,427 >> AUDIENCE: Yep. 1019 00:44:00,427 --> 00:44:01,260 SPEAKER 1: Miss you. 1020 00:44:01,260 --> 00:44:02,718 And I'm going to send out the rest. 1021 00:44:02,718 --> 00:44:06,060 I sent out half because I didn't think Gmail would let those go through. 1022 00:44:06,060 --> 00:44:12,410 So just remember, today is the day that you learned that I can use Gmail. 1023 00:44:12,410 --> 00:44:14,977 1024 00:44:14,977 --> 00:44:15,810 That was horrifying. 1025 00:44:15,810 --> 00:44:17,140 A big applause to Maya. 1026 00:44:17,140 --> 00:44:18,980 Thank you. 1027 00:44:18,980 --> 00:44:21,280 All right, so. 1028 00:44:21,280 --> 00:44:22,210 what 1029 00:44:22,210 --> 00:44:23,140 >> [BEEPS] 1030 00:44:23,140 --> 00:44:25,010 1031 00:44:25,010 --> 00:44:26,580 >> We got a few beeps, yes? 1032 00:44:26,580 --> 00:44:27,080 All right. 1033 00:44:27,080 --> 00:44:30,590 Now I'm going to get 700 really unhappy emails back from classmates. 1034 00:44:30,590 --> 00:44:32,400 So at least we'll see if that was worth it. 1035 00:44:32,400 --> 00:44:33,300 So what went wrong? 1036 00:44:33,300 --> 00:44:35,300 I'll figure out what went wrong after the fact, 1037 00:44:35,300 --> 00:44:38,340 but just look how easy it was to do that in PHP. 1038 00:44:38,340 --> 00:44:44,598 So what can we actually do now that we have this expressiveness of PHP. 1039 00:44:44,598 --> 00:44:45,970 That was-- that was horrible. 1040 00:44:45,970 --> 00:44:46,480 All right. 1041 00:44:46,480 --> 00:44:48,430 So that was what should have gone out. 1042 00:44:48,430 --> 00:44:52,770 This was me testing this morning on my phone, actual screenshot of my phone. 1043 00:44:52,770 --> 00:44:56,180 >> But now let's consider why we're actually introducing 1044 00:44:56,180 --> 00:44:57,840 all of this stuff in the first place. 1045 00:44:57,840 --> 00:45:01,625 So the goal is not to write these programs that 1046 00:45:01,625 --> 00:45:03,250 are written at the command line, right? 1047 00:45:03,250 --> 00:45:05,000 So I wrote a simple hello program, I wrote 1048 00:45:05,000 --> 00:45:09,155 a little program that does conditionals, and used a read line and so forth. 1049 00:45:09,155 --> 00:45:11,530 And in theory, I just wrote a program that sent out 1050 00:45:11,530 --> 00:45:14,290 800 text messages, give or take 800. 1051 00:45:14,290 --> 00:45:16,830 And that program was all still run at the command line. 1052 00:45:16,830 --> 00:45:18,538 And that of course is not the web, right? 1053 00:45:18,538 --> 00:45:22,160 The goal now is to start using what I claim is an easier to use 1054 00:45:22,160 --> 00:45:24,720 language, daresay a more powerful language in terms 1055 00:45:24,720 --> 00:45:27,104 of the features that come with it, to start 1056 00:45:27,104 --> 00:45:29,850 writing code that generates web pages. 1057 00:45:29,850 --> 00:45:33,248 >> So last time and last week, we talked about HTTP and HTML. 1058 00:45:33,248 --> 00:45:36,350 And what's nice about HTML is that it's just pure text. 1059 00:45:36,350 --> 00:45:38,220 And text is certainly something that you can 1060 00:45:38,220 --> 00:45:42,040 print by literally calling the print f function, or the print function. 1061 00:45:42,040 --> 00:45:46,160 And indeed, that's what PHP is now going to allow us to do. 1062 00:45:46,160 --> 00:45:48,610 So among the examples we have in today's source code, 1063 00:45:48,610 --> 00:45:51,610 in addition to some of the code that I put up there a moment ago, 1064 00:45:51,610 --> 00:45:55,440 we have programs like this for instance. 1065 00:45:55,440 --> 00:45:58,760 Froshims, or Freshman Intramural Sports, was actually 1066 00:45:58,760 --> 00:46:01,500 one of the first extracurriculars I got involved with years ago. 1067 00:46:01,500 --> 00:46:03,750 And my roommate and I, with some other kids on campus, 1068 00:46:03,750 --> 00:46:05,280 used to run the froshims program. 1069 00:46:05,280 --> 00:46:07,420 I'm getting my text messages now. 1070 00:46:07,420 --> 00:46:14,030 >> Now so we, back in the day, so this was 1996 or so, there was an internet, 1071 00:46:14,030 --> 00:46:17,395 but there was no process-- there was no website for froshims. 1072 00:46:17,395 --> 00:46:20,270 And so if you wanted to sign up for soccer or volleyball or whatever, 1073 00:46:20,270 --> 00:46:22,240 you would fill out this thing called a piece of paper, 1074 00:46:22,240 --> 00:46:25,060 and you would walk across Harvard yard, and you would slide it 1075 00:46:25,060 --> 00:46:29,230 into the mail slot of the proctor, who was in Wigglesworth, one of the dorms. 1076 00:46:29,230 --> 00:46:31,270 And this were you registered. 1077 00:46:31,270 --> 00:46:33,570 And he or she would then compose an email to you 1078 00:46:33,570 --> 00:46:36,514 manually, much like I just did here with Maya, 1079 00:46:36,514 --> 00:46:38,930 and then you would be confirmed as having been registered. 1080 00:46:38,930 --> 00:46:40,840 >> So this was like low hanging fruit, so to speak. 1081 00:46:40,840 --> 00:46:42,930 It was kind of stupid that we were using paper for this. 1082 00:46:42,930 --> 00:46:45,024 I wanted us to be able to just go on a website, 1083 00:46:45,024 --> 00:46:47,440 or have the freshmen go on a website, register for sports, 1084 00:46:47,440 --> 00:46:50,398 and just automate a lot of this stuff by putting their names and emails 1085 00:46:50,398 --> 00:46:51,850 and so forth into a database. 1086 00:46:51,850 --> 00:46:56,510 And in fact, the very first version of the website, using super ugly HTML, 1087 00:46:56,510 --> 00:46:58,195 might have looked something like this. 1088 00:46:58,195 --> 00:47:01,070 It was actually, ironically, more embarrassing looking than this back 1089 00:47:01,070 --> 00:47:01,460 then. 1090 00:47:01,460 --> 00:47:02,890 But I used a programming language. 1091 00:47:02,890 --> 00:47:06,060 Not PHP, but rather called Perl, which is an older language, 1092 00:47:06,060 --> 00:47:07,251 but very similar in spirit. 1093 00:47:07,251 --> 00:47:09,000 And I just completely taught it to myself. 1094 00:47:09,000 --> 00:47:12,208 And it took me a while to figure it out, asking lots of questions of friends. 1095 00:47:12,208 --> 00:47:14,080 But the ideas there were exactly the same. 1096 00:47:14,080 --> 00:47:18,050 Because what PHP was really designed for is not the command line stuff 1097 00:47:18,050 --> 00:47:20,450 we just played with, but for web programming. 1098 00:47:20,450 --> 00:47:24,960 >> It's really tied in its feature set to the web, as follows. 1099 00:47:24,960 --> 00:47:26,370 This is froshims. 1100 00:47:26,370 --> 00:47:28,720 And if I wanted to register in this form, 1101 00:47:28,720 --> 00:47:30,930 this is quite ugly, certainly, but let me go ahead 1102 00:47:30,930 --> 00:47:34,440 and be a less comfortable student, and whatever sport for Matthews, 1103 00:47:34,440 --> 00:47:35,630 click Register. 1104 00:47:35,630 --> 00:47:39,780 What I want to be able to do is submit information from a form, 1105 00:47:39,780 --> 00:47:42,400 not to Google's server like we did last time, completely 1106 00:47:42,400 --> 00:47:47,100 cheating by using their backend, I want to implement my own backend code. 1107 00:47:47,100 --> 00:47:51,410 Which means write PHP code that lives at a URL 1108 00:47:51,410 --> 00:47:54,180 that form data can be submitted to. 1109 00:47:54,180 --> 00:47:57,510 >> And then your code, written in PHP on a server, 1110 00:47:57,510 --> 00:47:59,400 can then respond to that user input. 1111 00:47:59,400 --> 00:48:03,287 Like his or her name, or comfort level, or dorm, and then do something with it. 1112 00:48:03,287 --> 00:48:05,120 In this case, I just stupidly printed it out 1113 00:48:05,120 --> 00:48:06,750 as text, which is not all that pretty. 1114 00:48:06,750 --> 00:48:09,590 But you could certainly imagine, if you know HTML, 1115 00:48:09,590 --> 00:48:13,010 and you'll soon know how to generate HTML with print f, and print, 1116 00:48:13,010 --> 00:48:16,500 and similar functions, you can certainly generate a prettier web page that says, 1117 00:48:16,500 --> 00:48:17,929 hey David, you are now registered. 1118 00:48:17,929 --> 00:48:20,220 You're a less comfortable student from Matthews, right? 1119 00:48:20,220 --> 00:48:23,880 >> You can just use a whole bunch of %ses, or the curly braces and dollar sign 1120 00:48:23,880 --> 00:48:27,750 notation I used a moment ago, to generate text that's more user friendly 1121 00:48:27,750 --> 00:48:28,567 than this. 1122 00:48:28,567 --> 00:48:30,150 So let's take a look at this one file. 1123 00:48:30,150 --> 00:48:34,666 Froshim zero dot PHP looks like this. 1124 00:48:34,666 --> 00:48:37,860 1125 00:48:37,860 --> 00:48:43,500 When I reload CS50 IDE, froshim zero looks like this. 1126 00:48:43,500 --> 00:48:45,700 This is froshim zero dot PHP. 1127 00:48:45,700 --> 00:48:47,730 And what do you notice about this file? 1128 00:48:47,730 --> 00:48:49,506 >> AUDIENCE: [INAUDIBLE] 1129 00:48:49,506 --> 00:48:50,922 SPEAKER 1: Sorry, a little louder? 1130 00:48:50,922 --> 00:48:53,250 AUDIENCE: [INAUDIBLE] It's all in HTML. 1131 00:48:53,250 --> 00:48:57,130 And in fact, it is, because what's interesting about PHP 1132 00:48:57,130 --> 00:48:58,880 is that it was designed to be, for better 1133 00:48:58,880 --> 00:49:02,110 or for worse, intermingled with HTML code. 1134 00:49:02,110 --> 00:49:05,850 And in fact, even though this file, froshim zero dot PHP, 1135 00:49:05,850 --> 00:49:11,470 has a PHP comment at the top of it, it's all just HTML. 1136 00:49:11,470 --> 00:49:17,655 But by contrast, if I open up, let's say, register zero dot PHP, which 1137 00:49:17,655 --> 00:49:19,550 just has a big comment at the top. 1138 00:49:19,550 --> 00:49:26,500 This too looks almost entirely like HTML, except for what? 1139 00:49:26,500 --> 00:49:29,350 Line, what, 21 looks a little weird. 1140 00:49:29,350 --> 00:49:32,739 >> But notice I'm entering PHP mode with open bracket question mark PHP. 1141 00:49:32,739 --> 00:49:33,780 Then I've got some stuff. 1142 00:49:33,780 --> 00:49:36,724 And then at the end of that line almost I have, question mark PHP. 1143 00:49:36,724 --> 00:49:38,390 And this is what I mean by intermingled. 1144 00:49:38,390 --> 00:49:41,910 You can write HTML that you just want the server to spit out. 1145 00:49:41,910 --> 00:49:45,090 But if you want to do something dynamic partway through the page, 1146 00:49:45,090 --> 00:49:47,699 like insert my name or dorm or comfort level, 1147 00:49:47,699 --> 00:49:49,490 I can use a function like print r, which is 1148 00:49:49,490 --> 00:49:52,492 print recursive, which just means print out this variable, formatting it 1149 00:49:52,492 --> 00:49:53,200 however you want. 1150 00:49:53,200 --> 00:49:55,880 It's really not for human purposes, it's just 1151 00:49:55,880 --> 00:49:58,290 for debugging or diagnostic purposes. 1152 00:49:58,290 --> 00:49:59,460 So that's how I did that. 1153 00:49:59,460 --> 00:50:06,100 >> If I instead go froshims two dot PHP, notice that the action of this form 1154 00:50:06,100 --> 00:50:09,680 is not registered zero dot PHP as the old-- the previous one actually was. 1155 00:50:09,680 --> 00:50:11,222 But it's registered two dot PHP. 1156 00:50:11,222 --> 00:50:13,680 So let's look at how this one behaves a little differently. 1157 00:50:13,680 --> 00:50:15,140 If I go into version two. 1158 00:50:15,140 --> 00:50:20,430 If here, if I register as David, less comfortable, from dorm of Mathews, 1159 00:50:20,430 --> 00:50:21,480 register. 1160 00:50:21,480 --> 00:50:23,770 So this time it said, you are registered, not really. 1161 00:50:23,770 --> 00:50:25,370 Let's see what I did here. 1162 00:50:25,370 --> 00:50:29,760 If I look at register two dot PHP, this has some more PHP code. 1163 00:50:29,760 --> 00:50:35,410 It's a little cleaner, although it still wraps a little long on this line. 1164 00:50:35,410 --> 00:50:36,260 Notice here. 1165 00:50:36,260 --> 00:50:40,300 I print out my HTML tag, my head tag, my title tag, the head, the body. 1166 00:50:40,300 --> 00:50:45,410 >> And then I start to say things like enter PHP mode. 1167 00:50:45,410 --> 00:50:48,540 And so that tag, open bracket PHP-- question mark PHP says, 1168 00:50:48,540 --> 00:50:53,460 hey, server, execute-- interpret the following stuff as PHP 1169 00:50:53,460 --> 00:50:55,030 until you see the end tag. 1170 00:50:55,030 --> 00:50:58,590 And even though you might not know any other PHP than you've seen thus far, 1171 00:50:58,590 --> 00:51:01,290 you can kind of read it pretty intuitively. 1172 00:51:01,290 --> 00:51:10,670 If name is empty, or comfort is empty, or dorm is empty, what do I do? 1173 00:51:10,670 --> 00:51:12,830 What do I say to the user? 1174 00:51:12,830 --> 00:51:15,330 You must provide your name, comfort, and dorm, and so forth. 1175 00:51:15,330 --> 00:51:17,750 Else, I say you are registered, well not really. 1176 00:51:17,750 --> 00:51:19,190 >> And I say not really because there's no database. 1177 00:51:19,190 --> 00:51:20,200 I'm not doing anything with the data. 1178 00:51:20,200 --> 00:51:22,408 I'm just throwing it away for demonstration purposes. 1179 00:51:22,408 --> 00:51:24,320 Now I kind of skimped over line 22. 1180 00:51:24,320 --> 00:51:26,400 There's more syntax there, but dollar sign 1181 00:51:26,400 --> 00:51:28,320 underscore post is kind of interesting. 1182 00:51:28,320 --> 00:51:30,300 And this is what's nice about PHP. 1183 00:51:30,300 --> 00:51:35,610 In PHP, you have what are called a few super global variables. 1184 00:51:35,610 --> 00:51:38,430 They are sort of even more important than global variables. 1185 00:51:38,430 --> 00:51:41,370 And these, all capitalized as such, come with PHP. 1186 00:51:41,370 --> 00:51:44,550 So if you have a server that has a web server installed, like Apache, 1187 00:51:44,550 --> 00:51:49,390 and you install the language support for PHP, all of a sudden you 1188 00:51:49,390 --> 00:51:52,250 can start using these super global variables in your code. 1189 00:51:52,250 --> 00:51:54,330 >> And what's nice about PHP is that if you just 1190 00:51:54,330 --> 00:51:57,540 write a file that ends in dot PHP, put it on a web server, 1191 00:51:57,540 --> 00:52:01,260 and then you submit a form to it via that action tag and a form tag, 1192 00:52:01,260 --> 00:52:05,110 that action attribute in the form tag, PHP and the web 1193 00:52:05,110 --> 00:52:09,360 completely figures out how to grab all of those key value pairs out 1194 00:52:09,360 --> 00:52:11,633 of the URL like question mark q equals cats, 1195 00:52:11,633 --> 00:52:14,782 it will figure out how to q and cats. 1196 00:52:14,782 --> 00:52:17,710 If you submit a photo, or a username, or a password to like Facebook, 1197 00:52:17,710 --> 00:52:21,360 PHP will figure out for you where all of that data is. 1198 00:52:21,360 --> 00:52:23,821 And it will just hand you an associative array 1199 00:52:23,821 --> 00:52:27,560 called dollar sign underscore post, or dollar sign underscore get, 1200 00:52:27,560 --> 00:52:30,590 depending on whether you're using get or post submissions. 1201 00:52:30,590 --> 00:52:32,950 And it will just hand you a super global variable, 1202 00:52:32,950 --> 00:52:37,254 so that if you want to get at the name that the user submitted via that web 1203 00:52:37,254 --> 00:52:41,894 form, you literally just say the name of the super global variable, quote, 1204 00:52:41,894 --> 00:52:43,380 unquote, name. 1205 00:52:43,380 --> 00:52:46,144 And quote, unquote comfort, and quote, unquote dorm. 1206 00:52:46,144 --> 00:52:48,810 And we're going to be able to do this and so much more with PHP. 1207 00:52:48,810 --> 00:52:50,540 And even though this was a whirlwind glance at it, 1208 00:52:50,540 --> 00:52:51,980 we'll dive in much more next week. 1209 00:52:51,980 --> 00:52:54,247 We'll introduce a database so that you're actually 1210 00:52:54,247 --> 00:52:56,816 going to start implementing your own e-trade based-- 1211 00:52:56,816 --> 00:52:59,150 like website in just a week's time. 1212 00:52:59,150 --> 00:53:02,750 So we'll see you next time, and hope you got the text messages. 1213 00:53:02,750 --> 00:53:05,450 Bye. 1214 00:53:05,450 --> 00:53:06,950 >> [MUSIC PLAYING] 1215 00:53:06,950 --> 00:53:11,450 1216 00:53:11,450 --> 00:53:12,950 >> [DOOR OPENING] 1217 00:53:12,950 --> 00:53:14,475 >> SPEAKER 2: Hey. 1218 00:53:14,475 --> 00:53:15,350 SPEAKER 3: What's up? 1219 00:53:15,350 --> 00:53:17,750 SPEAKER 2: What are you doing? 1220 00:53:17,750 --> 00:53:19,850 SPEAKER 3: Working on a problem set. 1221 00:53:19,850 --> 00:53:21,050 SPEAKER 2: Nice. 1222 00:53:21,050 --> 00:53:22,550 High five. 1223 00:53:22,550 --> 00:53:23,150 >> [HIGH FIVE] 1224 00:53:23,150 --> 00:53:25,584 >> What are-- uh, what are you doing here? 1225 00:53:25,584 --> 00:53:26,750 SPEAKER 3: Just hanging out. 1226 00:53:26,750 --> 00:53:28,134 Just got back from the gym. 1227 00:53:28,134 --> 00:53:30,050 I couldn't help but notice when we high-fived, 1228 00:53:30,050 --> 00:53:32,450 that you have pretty rough hands. 1229 00:53:32,450 --> 00:53:33,950 Do you go to the gym? 1230 00:53:33,950 --> 00:53:35,150 >> SPEAKER 2: No. 1231 00:53:35,150 --> 00:53:36,650 No. 1232 00:53:36,650 --> 00:53:38,450 >> SPEAKER 3: Dude, you look pretty big. 1233 00:53:38,450 --> 00:53:45,950 Which is bad, because everyone knows 8-ball is the big man on campus. 1234 00:53:45,950 --> 00:53:48,050 High-five. 1235 00:53:48,050 --> 00:53:49,550 >> SPEAKER 2: Uh, no. 1236 00:53:49,550 --> 00:53:50,450 No, I think I'm good. 1237 00:53:50,450 --> 00:53:52,300 I think I'm good. 1238 00:53:52,300 --> 00:53:54,097