In this course, you'll explore the basic structure of a web application, and how a web browser interacts with a web server. You'll be introduced to the request/response cycle, including GET/POST/Redirect. You'll also gain an introductory understanding of Hypertext Markup Language (HTML), as well as the basic syntax and data structures of the PHP language, variables, logic, iteration, arrays, error handling, and superglobal variables, among other elements. An introduction to Cascading Style Sheets (CSS) will allow you to style markup for webpages. Lastly, you'll gain the skills and knowledge to install and use an integrated PHP/MySQL environment like XAMPP or MAMP.

강사:

Charles Russell Severance

Professor

스크립트

So now, we're going to talk a little bit about control structures, the if or if then else is kind of the first one we take a look at. There's a whole series of logical operators. We talked a little bit about the equals, the not equals, less than, greater than, greater than or equal, less than or equal, greater than or equal. These are pretty obvious and if you know other programming languages, they make sense. The ones that are kind of new unless you've known C-based programming languages before, double ampersand is and, the double vertical bars or, and exclamation point is not. Exclamation's like not, but it comes in the beginning, so it will be like not something, something, something, and hence, we put that at the beginning. So exclamation point is not. We use curly braces to set out the beginning and end of things. So here's a simple if then else, we have some variable, we have as most ifs, we have a parenthesis that required here. This evaluates eventually to a true or a false. If this is not a boolean operator and the double equals is a boolean operator, this could evaluate to non-zero and zero, and non-zero becomes true and zero becomes false. So this could be a mathematical calculation. So if I just put if dollar ans, which is 42, that would then become true because this is non-zero. If this somehow evaluated to zero, then it would become false. So then this evaluates to true or false, in this case it's true. And then there's a curly brace, and a curly brace and that's the block of text, then there could be many lines in here. There's only one line, and this is the true block and this is the false block. Like most if then else's, the first if part is the true part and the second part is the false part, and then at the end you have else which starts a new block, and then you have an open curly brace and a close curly brace. So this prints out, this comes and runs. This becomes true, so it runs this block of code, and then skips to the end, and then never runs this block of code, so out comes, Hello World! Whitespace doesn't matter. That's the nature of a C-based programming language. We put whitespace in and we are very good at putting whitespace in because we just don't want to be crazy. So this is all pretty, it's got indenting. You can argue all day long about what the best indenting is. This has like all a big mess, it's all concatenated together, could be one long line. PHP loves this stuff but humans hate this stuff, right? So we're very careful to be stylistically pretty. Some languages like Python demand indentation, PHP does not, but we do anyways. Just so you know, it's not essential. You want to start a fight among C-style programmers? Ask them if you like the old or the new style of indentation. The old original style from 1970s, which I still use because I was around back then, is the curly brace comes up, and then the close brace lines up with the if, then you put all the else up on to the same line, and then the second close brace lines up. This is my favorite. But others say, you should have this open brace and close brace on their own lines and they should line up, the else should be its own thing. And so I don't like this but I understand that this is an artistic choice when you're looking at code. What you should probably do is just follow the pattern. You should learn to read both of the variants, and then follow the pattern of the code that you're in. It's not worth arguing. The other great argument is whether or not this is a tab or four spaces. That's another great argument to have that can divide and anger programmers quite badly. A multi-branch if. This is an if else else else else. And like in most languages, a multi-branch if is the way it works, is it's evaluating until it finds a true, and then when it finds a true, it's done. So, it checks them in order. If this is true, then it runs the first one, and if test is false, then it skips and checks, so it doesn't even do the second check. It's not like it does all the checks first. It checks the first one. If it's true, it's completely done. Then if that one is false, then it even checks the second one. If that one is true, then it does that when it's done. And if this is false and this is false, then it'll run it, and you can have as many as you want in there. Curly braces are not required if it's a single line. If it's just going to be a single line, some people will pull this up on the end of the if because whitespace doesn't matter. And so, this is a completely legit thing. It's equivalent to this. I would never write this code. I sometimes will do it with one if and a real tiny bit of code up here, I'll put it up there and add a semicolon because then it's really obvious. But as soon as it gets this complex, I'm going to put in curly braces and I'm actually not even going to do it. Actually, that's a sad face. I'm not even going to like this because if it's this complex, I'm going to put these on separate lines and indent them, et cetera because part of the job is to write code that other people can see but you are not required and you can do it. For looping strategies, there's a while loop, which is a zero-trip loop, meaning that its functions, the first time in is like an if statement. So it's an if statement, but the difference with it is it just keeps going and checks at the top. So it's a top-tested loop or a zero-trip loop. So in this case, fuel is 10. This starts here acting like an if. If fuel is greater than one, true, so then run this code, so then it prints vroom. But the difference is it goes back up, and fuel is still greater than one, so it says vroom, vroom, vroom, vroom, vroom. This is an infinite loop, and it's not a good thing to do because we did not create an iteration variable. We have this iteration variable, we didn't change it inside the loop. That's the way you get an infinite loop. So here is a better version of that loop. So fuel starts at 10. If fuel greater than one, do it. The answer is yes, so it runs the vroom, vroom, then it subtracts one and goes up and fuel is nine, eight, seven, six, five, four, three, and it'll run and then two, then I'll subtract it and it will be one. And at that point, when fuel is one, it's no longer greater than one, so then it's going to go up. So this is going to run 10 times. And we have a real iteration variable and we are also responsible. We, the programmer are responsible for managing the iteration variable when we are using a while loop. There's a one-trip loop called a do. It starts with a do statement. I rarely write these, very rarely. Whiles tend to be how we think about code. It's a one-trip, which means it's going to run this code before it asks the question whether it's a good idea to keep running it. But sometimes, you want a one-trip loop, and so it's going to run once, and that's going to do whatever it does, and then plus, plus count, that's going to add one to count automatically So there's one of those plus, plus operators that adds one to count. And then it adds one to count, and then after the addition is done, that's what participates in the less than or equal to five. So that's going to run a few times, and then it's going to finally, because we are adding one to count right here, that's adding one to count, so we do have a well-managed iteration variable, and then we end up running this thing five times. One, two, three, four, five. Again, I tend to not write these, but back in the day, I mean a long time ago, 1970s, this was just there for completeness. Now we use this a lot, and you might as well go ahead and learn this because every C-based language has this counted for. Java, C++, Javascript, Objective-C, they all have it. This is a counted for loop which is not like a go all through all things in array, we can talk about that in a second. What this is, is a counted loop. So this has a bit of code that runs before the loop starts that initializes the iteration variable generally. We have a test to decide whether or not we're done with the loop. And so the loop continues as long as this remains true. It is a top-tested which means if it's initially, it's immediately false, this loop, it'll skip right past it. So it sort of functions like an if statement in that result, meaning if this becomes false before the loop is run even once, the loop will not run once. So it's a zero-trip, top-tested loop. And then we have a count plus plus, and this is really common to increment as count equals count plus one at the end of the loop. So it's kind of like down here before the loop comes up. This code here runs at the bottom loop. So this just now a counted loop for i equals 1-6. One, two, three, four, five, six, right? So that's a counted loop. So, this becomes kind of like an idiom. In a count, how you do counted loops in all languages that are C-based, that's a counted loop. Like all C-based languages, the notion of break and the notion of continue are in the language. So the break is the simplest one. While this loop is running, if this code executes, it basically takes this entire loop and gets rid of it and it just gets out of the loop, it escapes the loop. This could be hundreds of lines of code. As soon as you hit a break, it knows which loop it's in and it starts executing at the line after the end of the loop. Okay? And so that's what the break is. The continue is another executable line of statement. Instead of going to the line after the loop, it goes to the top of the loop. So continue says oh we're done, don't run these lines down here. We're going to go back to the top of the loop and we're going to run. The key thing is, is it sort of runs through this bit right here. So it does do the increment, this third thing, and then it starts the thing up, so you need to increment your iteration variable, otherwise your loop could become an infinite loop. So that's how continue works. And again, all C-based languages, C invented this idea of break and continue. Not everyone loves them as much as I do, but it's something that I use and something you will probably use, and then you'll meet someone who's a purist and they'll say, "You should never have to write a continue." And I'm like, "Yeah. And then you have too many if statements." Okay, so that was roaring through some of the unique language features of PHP. I didn't just try to teach you PHP because I assume at this point that you already know a programming language. Because if you don't know a programming language, you should go learn something like Python because it's way easier to learn programming in Python than it is to start with PHP as your very first language. So, hope this has been helpful. Up next, we're going to talk about arrays.