DOUG LLOYD: So in our video on PHP syntax, we talked a lot about how PHP can be used at the command line to run programs in a manner that we're pretty familiar with from C. But as I also mentioned in that video, PHP was initially developed to implement web programming, websites. And so in this video we're going to talk about how we use PHP in the web development context. We know, already, from our video on HTML, that websites are built out of a set of HTML tags that semantically define the structure of a web page. But websites that are built with pure HTML suffer from a very, very serious limitation. And to illustrate this limitation, let's consider the following. So now I want to build a web page that, whenever the user visits it, it gives me the current time in Cambridge, Mass., displaying it to the latest minute. If I was making this right now, I might say something like this. Current time in Cambridge-- so I have HTML tags, head tags, title tags, body tags-- the current time in Cambridge is 14:08, printing out military time. What happens if it's now 2:09 PM, 14:09? Well so far, with HTML, I have to go in and change it to 14:09. And then one minute later, I have to change it again. And then one minute later, I have to change it again. And as you can imagine, that's probably the worst job ever. You're the webmaster for a web page, and every minute, 24 hours a day, you need to update the current time by manually opening time.html and changing just that snippet of code to say the current time in hours and minutes. That's probably not a very good use of our resources, both human and computational. Websites that are all HTML are completely static. The only way you can update content, as we've just seen, is to manually open those source files, edit them, and save them. And then when the user refreshes the page, or visits the page for the first time, they'll get the latest content. But only because we've manually edited it. If we start to mix some PHP in there, our code can get a lot more flexible. And we can have a way for our pages to be dynamic, or update themselves, without requiring our poor webmaster in the previous example to be manually updating things. They can do it automatically. We don't have to intervene. We can get some sleep. Which is probably a good thing if you're doing a lot of web programming. So in CS50 IDE, we run a web server called Apache. It's a very commonly used, open source web server system. This system has the capacity to interpret PHP, which is going to be useful if you want to do any PHP programming. And in CS50 IDE, we have a command to make this very easy to do, apache50 start. And then that slash path, slash to, slash dir, that's just a really common way of indicating what you've specified here is a path to a particular directory that you want to be the root site, or the root page, or the folder from which your web server will start to examine files and serve them up to clients who request them. So let's pop over to CS50 IDE, just to show you, really quick, how this works. So here in CS50 IDE, I'm in a directory called Week Zero Nine. And I have two-- I have executed an LS command just above to show you that I have two directories in here, PHP, which is the set of files that I used in the PHP syntax video, and then PHP-web, which is the set of files that I would like to use in this video. And I would like to start a web server instance with the contents of the PHP-web folder as the files are being served up. So what am I going to do? I going to type apache50 space start, space PHP, dash web. That's the path to directory from where I currently am. Then I hit Enter. It's going to do a little bit of stuff here. And then it's going to say, Apache started successfully. Your site is now available at-- and then there's the URL for the site. So what am I going to do? I'm going to quickly copy this. And I'm going to open up-- and I'll zoom out a little bit here-- I'm going to open up a new tab in Chrome. And I'm going to visit that URL. I'm going to hit Enter. It's going to load. And I'll zoom out again. And we can see here is the contents of my PHP web directory. So now what is basically happening here is, my instance of CS50 IDE is serving up these files to anybody who requests them. And over the course of this video, we'll sort of take a look at a bunch of these different files in context. To test that your Apache server is working, which is generally going to be the case in CS50 IDE, but if you do this more generally, and you start building your own web servers, there's a really common sort of analog to, "hello world," that is usually used for PHP web development. Which is to have a file consisting of just this code-- a PHP delimiter set with PHP info, parentheses, semicolon in between. Which is basically a special PHP function that tells you what version of PHP you are running. So in CS50 IDE, I have that file available just to show you what this would look like. So I'm looking at my index from my PHP instance. My Apache instance is running the contents of PHP-Web. And I have a file here called info.php. I'm going to click it, zoom out. This is what you're going to see, pretty much. This is just telling me that my Apache server is working. And this is, apparently, the version of PHP that I'm running. This is my analog to "hello world." So I know things are operational. So we're good to go. We can proceed from here. So let's revisit that time example we were talking about with our poor webmaster who had to update the page constantly. This might be a fix for how I would implement things so that the webmaster didn't have to keep updating the time. It would just sort of happen automatically. The down below, the HTML, it's pretty similar, with one exception. But here I've got some PHP at the top. I have, apparently, called this function date_default_timezone_set. And we don't talk about all these functions in CS50, because PHP has probably tens of thousands. That might be a bit of an exaggeration, but it might not be. It's got a lot of functions built in. And so this is a function apparently sets my time zone as US/Eastern, which is the time zone that I'm currently in making this video. Then I make a call to function called, date. And, apparently, I'm storing the HIS of something called, time. So what's going on here? Well, basically what's happening is, I'm making a call to some server that is going to tell me what the time currently is. And I'm converting it to a format of hours, minutes, seconds. And the reason it's capital H is this is going to give me 24 hour time, not 12 hour time, which would be lowercase h. And I'm just storing that in a variable called, time. So that second line of PHP there, the call to the function, date, is just getting some string, which is going to give me the date, and the time, and a bunch of other information. And the first argument there, that HIS, is just extracting the important part that I care about for this example, which is the hour, minute, and second. So that's the all that's going on. So I store that in a variable $time. And then down at the very bottom there, I have that shorthand for printing out the current time. So I'm just going to print out what the current time is. So let's take a look at this in CS50 IDE and see how we're now saving that webmaster a lot of trouble. All right, so here I am again back at the root directory of my Apache instance. And I have a file here called time.php. I'm just going to click on that. And I'll scroll up, because we're zoomed in pretty far. The current time is 14:20:34. So I'm doing it hours, minutes, and seconds. And I can refresh the page and get new time. --41. I'm going to refresh the page and get new time, 44, 46, 47. So, I am clearly not changing anything myself. I'm here refreshing the page. So I can't be back there editing it. And I promise you, I don't have any confederates who are editing the file on my behalf on the side. I'm just using that PHP function, time, to generate the time for me automatically. So that even if I'm asleep, and my web server is running, the user who visits that page is still going to get exactly the current time. So that's pretty good. I've made my site more dynamic with not too much PHP code. It was just two lines of code and then a little bit of a print statement. And already I have a much more dynamic site than that first example we saw at the beginning of the video. So, recall from the video on PHP syntax that when the PHP interpreter runs our program, it ignores everything that's not inside of PHP delimiters, spitting it out. In that example, what the thing that it was spitting out was HTML. And this means I can now intersperse HTML and PHP together. Because the interpreter will just ignore the HTML and literally output it. Which is good, right? Because presumably, at the end of the day, I would like my site to be constructed of HTML. So that any web browser can interpret it, or understand the HTML on the page and render it as something that we can actually understand as humans. And I can only use PHP for the parts of my site the require dynamism, that require me to have things that update constantly. The static information can stay the same. OK, so that's fine. But why would I do this? Why would I mix HTML and PHP? I could just print out all the HTML, using the PHP print function. Why am I not doing that? Think about it for a second. Why, when I could just print out-- I could have lines of PHP that just say, print HTML tag, print head. Why am I not doing that? Why am I mixing the PHP and HTML? Well, if you think about it for a second. PHP, the interpreter has to interpret what it sees. And so it's going to have to execute a line of code. Print out HTML. Print out open head tag. Print out open title tag. It's going to have to execute and interpret that every single step of the way. Why not just let the interpreter just gloss over things it doesn't understand and do it automatically? It's going to save me a lot of time to mix my HTML and my PHP together. And so that's why we don't just have open PHP delimiter and then just print out the entire content of our page as one giant PHP call to the function, Print, and then close PHP delimiter and we're done. So that's why we mix them up together. So far, we haven't seen too much of a difference from general PHP syntax. It's been pretty straightforward. We've already seen the question mark, equal. We saw a couple of new function calls, but nothing really fancy going on. Let's maybe make things a little fancier. What if we want to pass information between different PHP files, so that maybe the user can submit information to me. And then I could do something with it on another page. So PHP has support for something called Super Global Variables, which sounds pretty awesome. They're really just giant associative arrays that help implement this functionality of passing information between PHP files that are existing on our web server. The first of these super globals is called $_GET. And it's probably the simplest to understand. What happens with $_GET? Well, basically, the user is going to type extra information at the end of our URLs. And whatever they type, assuming it's formatted in a particular way called a query string, which is just a set of key value pairs separated by ampersands. Those key value pairs will be stored in a $_GET associative array. And from our video on PHP syntax, we know how to work with associative arrays already. So here is an example of some PHP, where maybe I'm extracting all of the key value pairs that the user supplied in the URL. So I have my Open PHP delimiter. I have a foreach loop. I'm iterating across the Super Global Array called $_GET. And I want to be able to refer to both the key and the value. And I'm just printing them out, each one on apparently its own paragraph tag here. I'm printing out some HTML and interpolating the values of key and value into that statement. So let's take a look at how this would actually work on our IDE. And maybe this will help illustrate a little bit of what GET is actually doing. So I'm back here in my web root. And I'll zoom in a little bit to show you that I have a file called, get1.php. So let's click on get1.php. And I'm not seeing any content. That's weird, right? Well, not really actually. Because I didn't supply anything in the URL. I'm going to get1.php, but I didn't supply any key value pairs as part of my query string. So let's add a query string and see what this file can do. To begin a query string you just type question mark. Then maybe I'll say name=Doug&year=2015. And then I will hit Enter. Now notice what's happening. I'm still in get1.php. But now I've supplied key value pairs, and I'm printing them out on their own paragraph-- name, Doug-- year, 2015. That's exactly the code we just saw on the slide a moment ago. And if I want to maybe add another key value pair, &class=CS50. Now I have another key value pair that's printed out when I revisit the URL again. Now maybe this isn't formatted terribly well. So I have another version of get.php. It's get2.php. The difference in this one is I have CSS file, and it formats things a little more nicely for me. It's maybe not the most beautiful CSS in the world. But it's just another way of doing it. So I can still get access to my variables using $_GET. And in this case, I'm just kind of making the CSS a little more fancy. And if we pop over to my IDE for a second, I'll show you in my PHP web directory here, get2.php. We'll open it up, some HTML here. Apparently I'm linking in that CSS file I was talking about. I open a table tag. And then here's my foreach loop. Here is that, what I showed on the slide before. The only difference is I have this key and value CSS styling that I'm applying to it. But that's all I'm doing, is I'm iterating across $_GET to get all of the key value pairs. And I'm printing them out as the table. And, apparently, I'm formatting the keys in one way, with the