Foundations: The Beginner's Guide to PHP | John Morris | Skillshare

Foundations: The Beginner's Guide to PHP

John Morris, I help freelancers get clients.

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
37 Lessons (8h 48m)
    • 1. Introduction PHP 101

      7:21
    • 2. What Software Do I Need?

      18:14
    • 3. What IS PHP?

      7:45
    • 4. Hello World

      6:01
    • 5. PHP Variables

      12:17
    • 6. PHP Arrays

      7:38
    • 7. If, Else and Switch

      12:24
    • 8. For and Foreach Loops

      7:28
    • 9. While Loops

      5:47
    • 10. GET

      10:13
    • 11. POST

      3:23
    • 12. How to Send Email With PHP

      7:41
    • 13. Create a PHP Contact Form

      24:11
    • 14. PHP Operators

      17:16
    • 15. Read, Write and Append Files

      13:34
    • 16. Create Folders

      5:14
    • 17. PHP Sessions

      9:47
    • 18. PHP Cookies

      10:15
    • 19. Writing Custom PHP Functions

      11:06
    • 20. Scope

      6:39
    • 21. Constants

      4:28
    • 22. cURL

      19:58
    • 23. File Get Contents

      11:18
    • 24. How to Upload Files In PHP

      20:54
    • 25. Create a Multi-Page Form Using PHP Sessions

      44:00
    • 26. Designing Your Database and Object Model

      28:13
    • 27. Structure Your Database In PHPMyAdmin

      25:17
    • 28. Create Tables Dynamically In PHP

      18:09
    • 29. Connect to a MySQL Database With PDO and MySQLi

      7:24
    • 30. Read Data From a MySQL Database With PDO and MySQLi

      6:56
    • 31. Create Data In a MySQL Database With PDO and MySQLi

      8:22
    • 32. Update Data In a MySQL Database With PDO and MySQLi

      4:29
    • 33. Delete Data In a MySQL Database With PDO and MySQLi

      6:42
    • 34. Writing Prepared Statements In PDO and MySQLi

      17:35
    • 35. Create a Database Class

      28:03
    • 36. Submit and HTML Form to a MySQL Database Using PHP

      17:24
    • 37. BONUS: Michael Phoenix Interview

      54:16
47 students are watching this class

About This Class

Hey! I'm John. I created PHP 101. Let me tell you a little about the course and what you can expect to learn. I've been "doing" web development for about 12 years now. And, when I first started learning PHP (back in the "dark ages" of the web) sites like YouTube and SkillShare didn't even exist. So, there weren't a ton of really great tutorials. And, I really struggled.

So, I got PHP figured out... I decided to turn around and help others.

And, PHP 101 was born.

Now, this course is aimed at beginners. We start with basic syntax and a simple "Hello World" script. And, my aim is to really break down what's going so you don't just understand WHAT to do, but WHY. I think that's so critical. To me, it's not enough to know what to type... you need to know when to, when NOT to, why... all of that.

That said, we do get pretty fancy by the end.

We get into MySQL database, PDO, prepared statements, writing a class, and more. Along the way we'll send emails. upload files, create folders, build a contact form, build a multi-page form use PHP sessions, submit form data to a database and whole lot more.

And, I labor to make sure you understand every bit of code.

This class is perfect for you if you're brand new to PHP and you want to go beyond just knowing what to code and really dig into the WHY; to free you up to be creative, tackle your own scrips in your own way and just make you supremely confident in what you're doing. That's my goal for you.

Anyway, I'd love to have you as a student. So, let's get started shall we?

Transcripts

1. Introduction PHP 101: Hey there, John Morris here. I'm the creator. PHP 101 And I created this video just because I want to tell you a little bit about what is in PHP one a one and hope you make your decision to enroll. So first off, it's composed of three modules, and I would call those basic advanced and then my sequel. So the first module is really for beginner Beginners. If you've never really messed with PHP, we're gonna go through basic syntax and all that sort of thing in that first month. Don't get you up to speed with all that the second modules where we get into a little bit more advanced stuff, creating folders, creating files, uploading files, that sort of thing. And then the third module, as you might have guessed, is all about my sequel. So just to give you example of some of the lessons that you're gonna find inside the course as I mentioned basic syntax, I'm gonna show you just basic PHP syntax. Talk about variables writing custom fuck functions. If Elson switch a raise, loops get imposed sessions. Cookies create update, delete files, creating folders. Gonna go through curl. Ross going to do? Of course. In module three, database crowd. So that's create read, update and delete on. We're gonna do that both in my SQL I end PDO. I'm also gonna show you prepared statements in both my SQL I and PDO. We're talking about designing your database and your object model. Uh, we're gonna talk about structuring it in PHP. My admin. We're also gonna talk about writing ah, database class as well. And then each module concludes with a practical exercise or very class project. And so those three different practical exercises or projects are creating a contact form, creating a multi page form using session. So this is a form where if you enter data on one page and then you go to the next page, but then you go back, it's still gonna have your data saved. And we're gonna use sessions to do that. This kind of ah ah, high profile type skill that you can learn and then finally submitting ah, form to a database or actually gonna go from the very beginning of forms all the way through to submitting it to a database also, along with all of those lessons and modules, you're gonna get exclusive bonus, which this is an interview that I did with Mike P. So if you haven't heard his heard me talk about his story before. Basically, he went from a broke college student who was kind of going through his CS degree and really wasn't having any sort of initial success coming out of that into a six figure coder. And he works for one of Fortune's five ist fast, fastest girl, 500 fastest growing tech companies. And he did all of that in just about 2 to 3 years. So it's a pretty amazing story, and the thing that he really focused on focuses on his leverage. So he explains how to leverage whatever skills you currently have. Whatever they happen to be a ZMA chairs little's as you currently have or get at any given time how to leverage that to get a job, to get paid more, to get promotions and have ultimately have companies fighting over you to hire. You have sat there and, quite literally, listen to him talk on the phone with companies kind of going back and forth fighting over him. So and he did all of this comes from stuff. He's actually done his own personal experience. So you're gonna get that bonus interview for kind of the turning your coding skills into a career side of things as well. Now, when you hear all this might be wanting, Okay, well, what do actual students think? Of course. So here's a few examples. So this is Joe Welch from the any Indiana Attorney General office Attorney General's office , he said, I just want to say thanks. I recently signed up for your PHP modules on your website. I've been developing for a couple years now, professionally, and I still find plenty of value in your lessons. Appreciate the extra knowledge he put out as to why it works the way it does. This is Kirk Oaks. He's the owner. Blue Line Graphics, he says, so far have gone through the 1st 2 tutorials in the first module, and I could say, is making so much clear toe. Understand? I've spent many mind numbing hours looking for an understanding of this stuff. All the other stuff Everett about PHP and HTML and CSS and JavaScript and my sequel and so on have left me not knowing your understanding how to put it all together, which is what the practical exercises air really four to To to do that anyway says this brought me to frustration. Heck, even the code Academy never really explained it. They just have you regurgitate what to type until you get it right, but never really explain why you are doing it as clear as you do. Thank you for the same training. Keep it up whatever you dio And then finally, the last one comes from YouTuber, Christiane says. Just wanted to take the time to thank you. I got hired and accepted an offer for 45 K for the 1st 3 months. If I do well, it will go up to 60 k a year, and I've only been studying code for three months while working full time. I just went for it like you said, and I got hired. I would have never thought in the span of three months I would double my pay check and not have to live in Silicon Silicon Valley to do it. So that's just a few examples. You know, the thing I think that you'll find with PHP one. No. One is A. It's a great way to learn PHP, but its unique and really how I labor and go to pains to explain the why behind everything that you do, you'll notice in a couple of those kind of comments that students have left. They talk about me really going into the why behind your doing what you're doing. And I really believe that's the thing that's missing from a lot of other courses that you could take that are out. There is a lot of times they just they just kind of show you the code and maybe give you a little bit kind of a cursory explanation of why you're doing what you're doing. But you're left just okay, I know what code to write. But why am I writing this this way? What's other ways that I could do this etcetera? And so I really go to pains to try and explain that sort of thing to you and really give you that. Why behind it? Because I think that is really the spark of creativity not to go too much of a rant here, but I really believe well, being a true coder is someone who is a master at at their craft. Is it really all about creativity coming up with new ideas, new new applications or products, or unique ways to solve problems or different ways of doing things? That's what being a coder really is, and that's what's gonna bring you success. That's when it's gonna bring you notoriety. This was gonna make you stand out from all the other developers that are out there, and the key to being able to be creative is knowing why you're doing what you're doing. That's what allows you to try different things because you get the why behind it. So I really go to pains to try and explain all of that to you in this course, and I believe that going through the course, you're gonna may have success and learning PHP and a deeper understanding of it that you might be able to get from any other course that's out there. All right, so anyway, that's my painting. Of course, that's for you to decide. Now if you'd like to enroll all the information and details and all that sort of thing is down below, and I genuinely hope you just hope to see you inside the course. That's it for now. We'll see you over there by now. 2. What Software Do I Need?: Hey there, John Morse. Here, John Morris online dot com. This lesson. We're going to get into the different tools kind of the base tools that you would need in order to start working with code in particular. PHP. So there's three kind of Maine standard ones that you may have heard of before. And then there's 1/4 1 which is kind of the latest and greatest. We're going to talk about what all these different things are and how they all kind of work together and then what you actually need in order to get going. So now one of the things here is this stuff changes. In the time that I've been doing this coding thing, there's been all sorts of different products and brands come and go. And so I'm a little reluctant to tell you, go download and install this one in particular, or that one, or to get into doing maybe tutorials on exactly how to install all of these, because there's so many out there and again they come and go. I really want to give you something that no matter what's out there at any given time, ah, you can kind of get your head around what it is that you need. So I will show you some examples, but I want to kind of really talk about the bigger picture here. So the first thing that you need is, as you see here, some sort of code editor. So the code editor you could think of it could be a simple something like no pad. You know, there's I would I hate to say this, but when I first very, very, very first started coating, I started out in note pad and you can technically use no pad. But it is about the hardest way in the world for you to go about coating, so I don't recommend that. But you just need something that you can write plain text in. That's not gonna add any because something like Microsoft Word or even word pad it. It formats the text. And so it's gonna add, um, html and different things to the actual code that you write in it. So using those sorts of things isn't going toe work. It's gonna have a lot of extra stuff in there, even know pad. I think there's remember it's been so long, but there's certain things that it can potentially add to a file. Yeah, that you have to watch out for. So you really want something that's designed to write code in because it's not only gonna make sure nothing happens, but also gonna give you syntax highlighting. So it's going to actually highlight the code that you write in a certain way so that you can tell kind of what's what difference between, say, of variable in a function plain text. And it helps you to see spot problems a little bit eaves easier and just makes it easier to code. And so you want to start with some sort of code editor, and that's basically what you write your code. And now there are a number of these will click over one of the more popular and the one I first started out with and probably might say the most basic off with ones out. There is no pad plus plus, so it essentially takes the idea of no pad, and it turns it into a code editor. And so this one is just fine. It gives you syntax. Highlighting has some other good stuff in there, Um, but it works just fine. It's a more basic one. Probably. Ah, really popular. One is called sublime text. So this is one that when I talk to people, I hear ah, lot of people running. Um, it does cost. I'm not exactly so It's $80 here. You can download it for free, but it's got some sort of licensing on it. I haven't actually used to blind Texan on exactly how all of it works, but this is a really popular one that you probably hear people talk about. That's out there as well, and then another one that has come out is called Adam, and I believe this is made by get other the people that make you get help if I remember correctly, up made with love by get Hub. So this is a really popular one that's out there as well. It's really pretty looking, so that's another one that you could use. So no Plat Bless, plus sublime text Adam brackets is one that has come out recently. There's all sorts of them, if you literally if you just Google, you know, code editor or something along those lines, you're going to see a whole list of a bunch of different ones, and it's really just my advice when it comes to that that picking a code editor is you're going to be spending a lot of time in your code editor. It's really gonna be the thing that you interact with the most. So try a couple of different ones and see what feels most comfortable to you. It's more important. It's not really about what's the best editor. They all do lots of different things and do a lot of similar things. It's more about what you're comfortable with, what works in the way that you feel good about on. You're not overwhelmed by and so it might be that you start off with no pad plus plus, and then you move into something like Adam or sublime text or brackets or something later on, as you get a little more familiar with code. But focus on what feels the most comfortable for you because it's something you're going to be spending a lot of time in. All right, next is your local server. Okay, so this is probably the one where people get caught up a little bit more because this is an immediate, obvious immediately obvious. Now a code editor. You kind of make sense, people. Most people get that they're going to need that sort of thing. But then let's say, for example, you're running PHP code and you try to open a PHP file that you've coated in your Web browser. You're going to notice that you're just going to see the PHP code. It's not actually going to process that code. OK, it's not going to run the code or run the script. And that's because PHP is really ah, you can think about as software really a piece of software that's meant to run on a particular kind of computer. So you have, ah, computer that's meant to be a Web server. And so in with that, that Web server has another piece of stop software on it that is actually the air. That computer has another piece of software on it that is the Web server. So ah, patchy might be something that you've familiar with or II s for. Windows might be something that you're familiar with, so those are actually Web server Softwares, and what they do is essentially when there's a request for a Web page of the made to the computer. It's that Web server software that ultimately handles it and determines whether something needs to be processed by PHP or not and and kind of handles all of that back and forth. You don't have that by default installed on your regular computer. Okay, so you need something on your computer to emulate that sort of environment. And that's what these local servers, these local Web server software's do they allow they create? A. They let you emulate a Web server and be able to run PHP files and all this sort of thing in your browser. Okay, so there's this is something that you pretty much if once you get outside of HTML CSS and JavaScript you need you need a local server like this, you're for PHP and interact with my sequel in any sort of server side script. You need something that can emulate this for you, so there's a number of on them out there again. I'll cover a couple of them were probably more popular ones with ones that I know and and then again can decide. So the 1st 1 is what's called man up and So that's Macintosh Apache, my sequel and PHP. And so essentially what this is going to? Well, actually, now I guess it used to be just for Mac. I hadn't noticed this, but now it runs on Windows as well. That's why they change this to my Apache. That's interesting. So anyway, that what this does is it sort simulates that environment for you. So it's going when you install this, it's basically going to install all the things that you need. Apache, my sequel, PHP is gonna stall them and set them all up so that you can start writing code and viewing Web pages and interacting with the my sequel database and all that sort of thing on your local computer. Okay, so that's what ma'am Pop does this again. Like I said, Bridges originally designed for Mac, which is why I put this on here. But now it actually does run on windows as well. You also have Zampa, so Samp was kind of the sister product to map. So originally, ma'am was just for Macintosh, and then they came out. I believe they actually reported ma'am to be able to be used on windows, and that's What? Zam Poisson. Now you can see both of these both run on Windows and Mac now, so you can kind of, no matter what you're running, you can, ah decide what you want to use, but it does the same thing. So it's a development environment. It hopes, Create the Web server and install my sequin PHP and all the stuff you need so that you can actually run your scripts. Another one that I've never used eyes desktop server. But I've This is one that I've heard heard of before and people talk about so ah, it's another one that you can use out there. And then one that I view I used quite a bit is why amps server. In the past, this is probably the one I use the most. Just because it seemed to work a little bit better on Windows and Zampa. And I haven't used sample a really long time, so that all could be totally different now. But ah, this again does the same thing that sets up your environment so that you can run your scripts. Apache PH. B, my sequel, that sort of thing. All right, so those air some different Ah, kind of local servers that are out there now. The next category that we have is what's called an I D e. So that stands for integrated development environment. And essentially, what it does is it takes this idea of the code editor of the local service and and then usually wraps in some other things that might be useful for someone who's working on code and it puts it all in tow one. So essentially you can install this piece of software and you're gonna have the coded or you're gonna have the local server server. And it's all gonna be kind of integrated in a way where you don't have toe mess with the files on the back end. It's all kind of set up toe work together. Okay, so if you're looking for kind of a one stop shop than this, a nine I d. Might be the way to go. Now you might be wondering, Why would anybody use a coded in reverse a local server? It goes back to what I said with the code editor. A lot of people as they use a code editor, they really kind of get attached to them. And so they really don't want to switch to the code editor that comes in an I. D. I would say that's probably the biggest reason why a lot of this stuff still exists so again for you. You'll just have to figure that out. But if you again if you want a one stop shop, that's one I d. Is right. So we're gonna talk about a few out there, so one of them probably amore enterprise solution is visual studio. Um, I mess with this at once. It has a lot of stuff in it. So it was a little overwhelming for me. This I first tried it when I was ah, first. Starting out coating was really a little overwhelming for me to use at that point, but I still see people use it. My suggestion is, if, really, if you're going to stick toe more Web type stuff, this might be a little bit of overkill. But if you ever see yourself getting into doing like C plus Plus or C sharp or those sorts of things, then this this would might be a good editor for you because I again I haven't used a ton. But my my sense of it is Is this a little bit better for those types of of software and maybe a little bit overkill for for the Web stuff? But again, it'll it'll run all of that, that that stuff and you can see it's got the code editor. Ah, you know, you can look at your different files and all that sort of thing. Another one. This one I used for a long time is net means, And they do have, uh, least at one point they had a PHP specific version. Let's go ahead and click this download here. Yes, so they do have net beans. Four PHP. That's what this one is right here. So it's you can see they have Java and groovy and all this other stuff here. But you can download just the PHP one to your computer and installed just what you need in order to run and work with PHP. So they have that option as well. They also have other flavors was, you know, C plus plus and C and all that sort of thing. So I used this very long time, and it has a lot of cool things like you can connect to get hub or apps. Aversion server. It's got all the syntax highlighting you expect it's got code completions as you're typing . Your coat will give you suggestions for different functions. You might be ableto that you might be trying to type and all that sort of thing. It's got you can look at. It's got a tree where you can kind of you can click on Ah, a class and look at a class you can search for without having actually go to the code file . You can search for different thing. It's it really is. I mean it. It's a pretty pretty good idea, in my opinion. Again, I use it for a lot of years. Probably the biggest thing is it. It is a little bit heavy, and it does take up a decent amount of resource is and at times can run a little slow. Ah has been a while since I used it, so they may have got some of that cleaned up, but that was what I noticed. Onda. Ultimately, why? One of the reasons why I switched off it. We're going to talk about what I use here in just a second. But that's another example. Eclipse is a popular one that I've heard of I've never used, But you can see here again. It's got the code editor in here. It's got your file system. It's kind of all just met to go together. It has the emulator and all that sort of thing. So then you have X code. X Code is really meant for if you're building if you're getting into building the IOS APs so this isn't necessary something used for for Web stuff, but I wanted to throw it in here just in case you get into building that sort of thing. All right, so, Lao, let's talk about the last piece here, which is my question marks. And no one could maybe argue the latest and greatest I'm really liking. And that is what's called a cloud. I ve So it's the same concept as an I D. But instead of it being a piece of software that you download to your site, it's actually some of them can have software that you download. But the whole idea is that all of your files are actually hosted in the cloud and and so you can access it through a Web browser. Eso let me just go to my dashboard and sign in here and you can see I have this back end here where I can get into you can spin out basically different work work spaces. If you create a new workspace, right, you can make it for PHP. If you're doing python stuff, you can do Ruby. You can have a specific WordPress Ah, workspace built C plus plus. You know, the and they all have sort of different things that they support, but it allows you to really quickly create that sort of thing. So it's really handy. And the nice thing about it is I could be on my desktop. I can go over to my laptop. I could even go on my phone. And when I log in to use it, it's all the same. Code right, which is different from if you have a code editor installed on your laptop, then all the code that you write in that code adder is saved locally. So if you go over to your desktop, well, it's not necessarily sync up there, whereas with this it is. And that's why I like it Because I do work on a number of different devices at times and traveling a decent amount. And so having access to the exact same code is really nice. Now, some of these air limited in the sense that, for example, with this I can't really I think you can connect to get, huh? But you can't connect to, say, subversion. There's another option besides cloud nine with code anywhere. I think you can create connect to some more of that stuff with this one of. Actually, I tried this one at first and didn't really like it. And then I've been using Cloud nine a while, and this one has, I think, done a lot of development. So I may check this back out again at some point, but ultimately, again, the whole idea and there will probably be others that pop up and so forth. I was using nitrous, actually, for a while, and nitrous actually went, uh, went under. And so that's when I switched to cloud nine. So, again, all of these pieces, after they've showed you they could come, they could go at one point could be good. At one point could be bad and then be good again. I have no idea where that hole marketplace and all of this is going. But what I want you to know is the difference between a code editor, Ah, Web server and I d in a cloud i d. So you're not going the I d route? You need both the code editor and the local server, the local Web server. If you go the i d route, whether it's a cloud or just a regular i d. All of that is built in tow one. So that's what I want you to get out of this from there. You know, just go out and do some research play with different ones. When I first started doing all this, I downloaded three or 41 side. I would check him out and see what I liked, and that's how we ended up settling on what I settled on. So, uh, hopefully that gives you a little bit better insight into what's all out there, what you're gonna need in order to get up and running, and some of the different software that's out there free to use. All right, that will do it for this lesson. Thanks for watching. We'll talk to you next time 3. What IS PHP?: welcome to PHP 101 In this video, we're gonna cover what is PHP to do that Let's start in the browser and talk about the concept of how code is interpreted. That is the code that you as a developer, right, is then rendered to to create the display that you see in your browser. So if we go to Google here and we right click on this and view page source, you're going to see the source code is really just a bunch of code here that for most people is probably pretty difficult to read. But it's this code that creates the page, and it's the browser in particular that looks at this code and then renders it into the display that you see over here. So your brows air really at its core is just until critter. It interprets the code into visual display. Now, with PHP, there's certain types of code or languages that are processed what's called client side. And there's some that a process that air called server side, now each to mow code, which you may be familiar with. JavaScript, CSS. Those are all process client side, which means by the browser. However, PHP is something that's processed server side. So we're going to take a look at my fancy drawing skills here, and I'm gonna show you what that means. So if we were to draw a line down the middle of this page here, and we were to say, This is the client side and this is the server side. As I mentioned, your browser would sit over here and then your server, which is really just a computer that's configured in a certain way to serve Web pages, would sit over here as I mentioned. Html, CSS, JavaScript. These all are over here, whereas PHP and my sequel are over here. And really, what PHP and my sequel are? Is there just software that's installed on the server? So as in fact, there's three kind of key pieces of software that are often installed on a Web server to make it work? That's Apache PHP and my sequel or some sort of database software that that interacts with PHP. Now this is what's called a lamp stack, assuming that this is running Lennix because it stands for Linux, Apache, my sequel and PHP. Now again, these are really just pieces of software that are designed to allow this computer to serve Web pages to a browser. So what happens is when you go to google dot com that actually gets translated into a what's called an I P address. So you're browser makes were request for google dot com. It gets translated into a I P address by what's called a domain name server, and then the browser gets the I B address and makes a connection with that particular computer. So every Web server out there, every computer connected to the Internet has an I P address, and browsers connect to the servers directly to transfer information back and forth, which is the Web pages that you see. So when that happens, the program that handles those requests is Apache. So Apache initially receives the request from the browser, and the browser will often tell it. We'll tell it what it wants. So if you go to google dot com, then that's actually a Web page typically called index dot PHP or index dot html could be default. Ah, or if you go to say the about page, it could be about dot PHP. So the browser tells Apache what it's requesting from it. And so then Apache looks in its file system, so it has a file system just like you have on your computer, and it says, Okay, it once index dot PHP So I'm gonna grab that file, but I know that it's dot PHP, so that means there's the potential for PHP code in this file. So that means I need to send it to the PHP processing engine to see if it needs any of that code processed. So that's what Apache does. It sends it over to PHP and says, Hey, is there any PHP code in here? If there is, then PHP renders that code, and if necessary, it will. It will make any requests. Oh, my sequel that it needs to make. And then once PHP is all done, it sends that file back to Apache completely interpretative interpreted. So once it sends its back to Apache, there's no more PHP code in it. That PHP code has been processed to create whatever to designed to create so often times it's HTML could be CSS could be JavaScript, whatever it's asked to create by the PHP code that you write that is all interpreted before it sends it back to Apache, and then Apache sends it back to the browser, where it's then interpreted by the browser because it's HTML CSS JavaScript etcetera. So PHP is what's called a pre processor. It's something that processes on the server side before it sends it back to my sequel. Now, this is somewhat important to understand, because it gives you an idea of what it is that you're actually doing when you're writing PHP code. What you're doing is you're almost always you're writing PHP code that's then going to write out or output some sort of HTML or CSS or JavaScript. So you're using peach. You using PHP code to write other code, and that's how we make the interactivity of a Web page. So if we go over here back to Google when we type in a search here, what's actually happening is there's a request made to that site. To rope. To perform this search and that's being processed is being sent to Apache than you know when we hit the submit button that sending that you know that request is being made to Apache Apache sending it to PHP. PGP is querying my sequel, and then it's getting information back that shows thes results here, and then it sends that PHP says it's done. It sends it back to Apache and patchy. Send it to your browser here. Now, I don't actually know of Google runs on PHP and my sequel. So for all the purists out there, this is merely Justus, an example of what's happening eso but that that's really what PHP does and what it allows you to do is that let you add conditional logic, let you interact with databases. Process each TP requests and do a whole lot mawr that you can't do with just static. Html CSS and Java script. So PHP is a pre processor. That latte allows you to add an element of interactivity. Make make your webpages dynamic. Ah, and really make robust applications that actually do something as opposed to just display information 4. Hello World: Welcome back to PHP one. No one in this video. I'm gonna show you just how simple and easy it is to get up and running using PHP over here on the left side. You'll see that we have this. Hello, world that html file. This may be something that you're familiar with that you've been working with up to this point. Html CSS and JavaScript. And so you've seen a file like this, and now you're ready to dive into PHP and want to know how to convert this to a PHP file so you can start using it. Well, it's really pretty straightforward. So I'll go ahead and right click on my app here again. Of course, whatever editor you're using, this may be a little bit different, but for me, I can simply hit this rename option here and change this to a PHP file. And now we have a PHP file that we can write PHP code in. And remember, if you watched the video, what is PHP? All that extension is really doing is telling Apache that this is a PHP file and it needs to be sent to the P to the PHP rendering engine to see if it has PHP code that needs rendered and if so, for it to be rendered. Okay, so it doesn't change really anything that we can do with this file. Outside of being ableto add Ph. B. It doesn't really take away anything. So me changing this to hello world dot PHP If I come over here to this page and I now visit this file and you'll notice I haven't actually changed haven't put PHP code in my file yet . I come here, I still get hello world, so you can still write a regular HTML page inside of a dot PHP extension. In fact, that's the point is oftentimes your PHP code will be embedded inside of a Web page to display certain parts of that Web page. So this is obviously something that's absolutely critical that they left in there that you have the ability to continue to do so that you could do that with PHP, right? So now how do we actually write some PHP code here? So to just some basic insect syntax to tell the PHP rendering engine that a certain block of code is going to be PHP? We use suggest some very special tags Now. There's nothing to explain about these tags. They're just arbitrary. It's what they just decided to use. So there's no deeper, necessarily meaning behind these. These air, just the tags that you need to use. So it's gonna be, ah, less than sign a question mark in the words PHP. This opens a block of PHP. This tells PHP or the rendering engine. Okay, we're starting PHP code now and then to close it to say, Okay, we're done with PHP. Use a question mark and the greater than sign. Okay, so anything in between these two symbols is gonna be considered PHP code. Now if I were to write hello world inside of here, which is improper syntax, right? This is not proper PHP syntax, and I redo this. You can see it breaks my page because it's what happens is this file gets sent to PHP to render it. The PHP engine tries to render it and says, Oh, that's not the language I know. And so it says it breaks it. It's is this file's broken and sends that back to Apache. And so than Apache, since that's the browser and says, Hey, something's up with this page. It needs fixed. Okay, So if we come over here now and we use proper syntax, we do echo and hello Oh, world like this, which is now proper PHP syntax and echoes Just a way of printing information to to the screen and we reload this. Now, you see, we have our hello world here. And of course, we saw this one down here. So if we really erase that one, then we'll have our one hello world. Okay, so it's really that simple to get up and running, you just need a file with dot PHP extension in it, and then you need to start writing your you do your open and closing PHP tags, and then you start writing your PHP code. In the middle of that, it's really pretty easy to get up and running. Of course, the one caveat to this is that you need to have a Web server up and running, So you needed either need to have a live Web server on a live domain that you're using out there for for what you're doing, or you need to have a program like Wamp server or example installed on your local computer that has the PHP rendering engine in it so that it will render these PHP files. If you don't have that, then you're just going to see this code in your browser. So if if you do this and you just see this exact same code in your browser here, that means that you don't have your Web server properly up and running. And I would recommend watching the PREREQUISITE, one of the pre records that videos for this course, which is how to get a local server installed with Wamp Server. Now there's lots of options out there for doing that kind of thing. I use a program. Everybody always ask me what program I used a user app called nitrous. It's ah, nitrous dot io and it's a cloud i d. Integrated development environment. So it has the Web server and all that already built into it, so I don't have to install any of that stuff. There's lots of options out there like that, Um, but if you want something just to get up and going, you can watch that to Douro video on how to do it with lamps over which I use for a lot of years. All right, so that's how to get up and running with PHP, as you can see, very simple and straightforward. 5. PHP Variables: Welcome back to PHP one a one. This video. We're gonna get into Peach P variables. All right, so let's start off with just the simple usage. So you notice Here we have eco. Hello, world. Let's say we want to actually use some variables to do this so we could do then variable equals Hello world. And of course, the variable name can be anything here. And when we write variables, they always start off with this dollar sign here. So this is how PHP knows that we're creating a variable here. So we start off with the dollar sign, we do the variable name, whatever it is, it could be name. It could be string. It could be dog. It could be whatever you want. The name of that variable to be my advice make it descriptive of what the actual variable is gonna be. So, for example, if you're displaying someone's first name, then maybe first name like that. Okay, so that's a kind of a rule of thumb when creating variables. So dollar sign the name of the variable and then equals. So we're telling it this variable is equal to whatever we then put it equal to. In this case, we have a string. So we're gonna use hello world and then in order to come down here and use this variable Then instead of echoing hello world like we did above we can echo are the name of our variable like this. So if we do that, then you'll see we have a second hello world here. OK, so it's just a simple usage of variables. Now, one thing to keep in mind with variables. There's a number of things I'll run through here. The 1st 1 is escaping information. So let's say we wanted to write something like I'm a variable now. The thing to pay attention to is this quote right here. This is a single quote and of course, these air double quotes now that matters in PHP because you use double and single quotes as Indyk as rappers, essentially for four strings. So you notice up here I use single quotes and then down here use double quotes. The reason I've done that is because single quotes will actually out. Put it in literally where as double quotes, arm or oven interpretive um, usage. Ah, for strings and I'll show you what that means in a second. Now, if I save this and I refresh this, you'll see that this works fine. I'm a variable. However, if I come over here and I change this to a single quote and I change this to a single quote , you can already see that there's some weirdness happening. And see that this this here's one color. This year's another color, and if we refresh this, you'll see that we get an air. That's because we're what PHP ISS seeing here is that seeing an opening single quote think Okay, we're starting a string. Were using the literal interpreter or the literal designation here and then, Oh, we're ending it right here. But that's not actually what we wanted as the code writer. So then it says, Oh, it's ended, and now we have just this text here, which doesn't work, and we have a single quote that doesn't have a counterpart to it, So it PHP is confused by this is it's not right syntax, so it throws the air. Now there's a couple ways that you could handle this. The first thing that you could do is what's called escaping so you can add a backslash before the single quote and that tells PHP. Hey, the very next character after this back slash I don't want you to render. I don't want you to render it like you normally would. And so PHP says OK, and now if we refresh this, you can see it works fine the other way that you can handle. This is how I did it initially is You can wrap this in double quotes and then you single quotes inside of it. And so PHP what it's seeing is it saying, Oh, he started this string with double quotes. So any single quotes in sight of this isn't meant toe end the string. So I can just you treat those like regular text and so you can see that this also works as well. Now, another thing that you can dio is you can reverse it. You can use single quotes on the outside and and then you can use double quotes on the inside. So now if we refresh this, we can see that works as well. So you condone mess without a number of different ways to to get what you're after. Oftentimes, if you're out putting actual just text. So no HTML code. Then you would usually use the double quotes on the outs to start the variable, because there's a good chance when you're writing just text, especially if it's paragraph you're gonna use some sort of apostrophe in that text somewhere. And you don't want toe have to escape all those or have a break and so forth. It's one writing plane tax to Usually the good rule of thumb is used double quotes on the outside. When you're writing HTML, however, you probably know from having done some HTML. I'm assuming that you may have things like a ref equals and you have double quotes inside of it. In that case, you often want to use the single quotes on the outside so that you can freely use your double quotes inside here and be just fine. Okay, so that's a little bit on escaping. Another thing that you can dio is you can content content. Eight strings always hate that word, but ah, you can basically combine strings together so we could do something like I am and then dot a variable so you could do something like this you may have seen this with JavaScript, you'd use a plus sign here. And PHP it's a dot So if we come over here, you see, we have that works fine here. So that's ah, one thing that you can do it with your strings conch in. Take that. Also, you can dio let's say we want to do some, you know, adding or use numbers with our variable so you can set your variable to a number. So let's just say number equals No. 22. Now you'll notice with numbers. You don't need to put them in quotes or single quotes, right? PHP recognizes this is ah, data type is what we call these. So you string is a data type managers of data type, so PHP recognizes that and you don't need to do anything. You don't need to wrap that now. If you do, it's not gonna break. But the data type will also be recognized as a string and not ah ah number. So that will be important when you get into some more advanced stuff and get more heavily into data types. But for now, just know you for numbers you can just put them straight out like this. And so you can see we have 22 here. Now, if you were to add any sort of text to it, besides just a straight number like this, that would cause it to break. So it's on Lee. Strictly numbers can't add any sort of text to it. All right, so then the other thing that you can do is you can, of course, do math with ease. A lot of people think code is all about math, but it's really not. But you can still do math, so you could say something like number times to like this or let's see, that's do number equals number times two. All right, so let me just do this, all right? So we can do that. And you see that 22 times two is 44. So we've done some basic math. You could do divided by two. And so we get 11 over here, and so you can do all sorts of different math, and, uh, arithmetic functions with your variables as well. Now, one of the things that you can't dio with this is start your variable name with a number. So doing something like this. This is not going toe work. This is gonna PHP doesn't like this. And so it's going to cause it to break. But you can add numbers at the end like this, so PHP doesn't like them at the beginning, but it will take them just fine at the end. Another thing that we can do here, that you'll probably do quite a bit is use numbers in strings are used variables and strings. So, for example, you could do something like, um, Echo. My my age is number. I wish, right? I wish I was 23 or I guess this is going to give us 11. But we can come over here and you see, we have our string here. My age is 22. Um, L X. I didn't because I changed the variable here. So says my age is 22. You can see I put this right inside the string here. Now, when I talked earlier about the double quotes being interpretive meaning it'll render variables inside of it like this. If I use the single quotes here, however, it's not going to do that. So this is the difference between single and double quotes. If I refresh this, you'll see it actually just prints this out literally. So single quotes print things out literal like this, whereas double quotes will actually render these a little bit. And so one of the things you can do is if you, for some reason, need to use single quotes here. You can actually Concha, Concha Tatis. Ah, so we can go like this and then we can use our single Or actually we can do our single quote to end that string, Use the dot and then we have our number here like this. Get rid of this one. And so you see, we have our string. It's at a space. Have our string, we have our dot and then we have our variable. You can see now we get my age is 22. The last thing I'm gonna show you then, is something called variable Variables, which is a neat little trick. May not use it a ton, but it may be come in handy for you if you need to do something like this. So we have our number variable set to 22. What we can dio is we could create a new, very born will just say this is a equals and we're going to set it to number. Okay? And then we're gonna come down here and we're gonna change this to what's called a variable variable like this. And so what this is saying is that we want here, we want a which is set to number. We want the value of that variable with this name. So essentially, what this is going to do right here is it's gonna look at a and say that's set to number, and then it's gonna look back up here and see if there's a variable called with the name number which we have, which is 22 right here. So when we do this, then this should output 22. So if we look at it, you see that we get 22 right here, so that's called a variable variable. It lets you it allows you to add a little bit of a dynamic element to your variable naming . Ah, and when you get into some more advanced scripts and so forth that that can potentially come in handy and maybe something that you want to use if you want to create something where you need to create variable names on the fly, then this is oftentimes something that you can do to do that. All right, so that's variables. As you can see, they're pretty straightforward and easy to use. 6. PHP Arrays: Welcome back to PHP one. No. One in this video we're going toe dive into a raise. So first thing you may be wondering is, Well, how do I create in a race? So there's a several differently he's that you can create an array. The most simplest one is just creating an in what's called an index to Ray. And you can do it by doing something like this. We create a variable name or just calling it a ray here said it to equal. And then we view this use this array function, and then inside of this, we just write out the elements of our race so it could be name E mail email address, right, and you'll see that now. We've created three elements in our array, and those are name, email and address. Now, with an array, you can't echo this. So if I were to try and echo this array like this, what I'm going to get, you'll see here. It's just this word array, but I don't actually see what's in it. So we have to use a function here called Print Are, which is basically print recursive, and that will actually go through the elements of the array, and it will print those out so we'll come over here. And if we refresh that now you can see we have name, email and address. So this is called an indexed array. There's really kind of to types of a raise. There's an indexed array, and then there's what's called an associative or named Array and indexed array is essentially one where the keys are numbers. So to break this down and look at what's actually in this array, what we have here is we have these right here, which are the keys. So 01 and two are the keys, and then the values are name, email, an address. Now that's important when working with a raise, because that allows you to access different elements within this array. So, for example, let's say we wanted to come down here and we wanted to. We wanted to echo out this name here so we would do echo. We would reference our array, and then we would use these brackets and we'd specify the key of the element that we want. So in this case we would do zero and close that line and we refresh this, you'll see that echoes out name, which is the element that's in the zero position or has zero as a key here. So if we wanted one, we would just come over here to one, and that would give us email. And, of course, if we wanted the last one we would do to, and that would give us address. So that's important to know when you're working with the Rays. That Honore is essentially, um, it's a container of sorts that has different, essentially variables inside of it. Because in a way, each one of these keys is a variable that references that has a different value associated with it. So we can access that information doing this or using this syntax. Okay, so, um, once you know that, then there's a couple of different ways that you can create a race. So the 1st 1 is what we've we've done here. Another one is just using brackets so we can create array to, and we can just use brackets like this, and we can set this equal to something like salad No, like this. And then if we print our array to we take a look at that, then you can see we have a new array and we haven't indexed array with the value of salad. And then you can add things to this like this, and you don't need to specify any name here, necessarily. And we could put something like bowl. And if we print that array, you'll see that now in it we have salad and bullet. It really just kind of appends it onto the end of whatever is there that could be useful. When we get into looping and you're looping through different things, Um, and then you can come. You can add elements into your ray and so forth, um, and help build Honore programmatically. So that's something that you've been thinking about doing. This is one way that you could do that. Of course, the other way to create a raise is what what are called associative arrays. So we can do array three equals and we use our ray function, and here we actually give we specify the keys. So here we could do the key. Whatever we put first here is the key. So the key is name and let's say we just want to do, John. All right. And then we put a comma. So this is one element, right? This is gonna be one element, Narey. The key is name, and the value is John. Then we could come down here. But you do age. No, dear. Yes, I'm 35 as of now. So age 35 Um, and we could do email like this. Okay, So now I always It's probably some debate you could have about this. I always end with a common here. You can do it with or without the comma. But I always put one there, because if I come back here later and I want to add something and I don't have that common there, I often forget. And so I just I put it there as kind of a good practice for me. You don't have to do that. Some people even say, don't do that. But whatever. Ah, with or without the common at the end there, it'll still work. And then if we come here and we print our our array three, then you'll see. Now we have our ray three here and now we have named Kees. So if we wanted to access, for example, the name We could come here. We could echo array three name like this, and that's gonna print out what we have right here for this key name at the value of it is equal to John. All right, so that's what ah, that's going to force and that name to raise their valuable. Oftentimes we get stuff from a database or so forth. You know, you'll have name to raise like that because you have field field names and then the values of those fields and so forth. So three different ways that you can work with the Rays. Obviously, as you get into, um, looping and so forth, this is gonna become more important. But that gives you a no idea of how to create raise, how to start working with them and so forth. 7. If, Else and Switch: Welcome back to PHP 101 This video we're gonna get into if else and switch. So these air conditional operators that allow us to display different or do different things in PHP or displayed it for information based off some sort of checks, some sort of condition that we right. All right, so let's start off with just kind of the basics in tax. Here, let me kind of do this so we can move this up here a little bit. So the basics in taxes, you start off with the word if and then you create parentheses inside of these, this is where we're gonna put our condition, will come back to that in just a minute, and then you create curly brackets like this. And so what this is going to do is if the condition that we right in here is true than whatever's inside of these curly brackets is going to be done. Whether that's it could be an echo statement, it could be some sort of database call. It could be any sort of PHP function you put inside here. If this condition is true, that's that is going to happen. So we could create a simple condition. Let's go ahead and create a variable like this Will call it number will set it equal to one and we'll check and see if that number is in fact said toe one. So if number equals one, then we're going to echo True like this. OK, so a couple things to note here, one obviously we're referencing are variable were inside the parentheses. You'll notice we use double equals here, and that is again that that make sure that we're actually checking this number to see if it's equal toe one. If we used single ah quotes here, this actually will go ahead and set number equal to one, no matter what it was set up to here. So the essentially override this line right here, and then it will assume, obviously, because we've just said it, that it's true and therefore echo this. So if we did this, if we look here, you see, we get true and we could set this number to say three and it's still true, right? So if you're having an issue, who here and I've had this before? If you're having an issue where no matter what the conditional. You think the condition should be a certain thing and you should be getting a certain result and it's not happening. Check your condition statement. Make sure you have double equals. So for here, if we do this now now it shouldn't show. You see, we don't get a true because it's not equal to one. But if we said it toe one, then of course, and we get the true over here. So that's a very, very simple if statement here. Now you can do what's add on to this. What's called else. So we do write the word else after our curly brackets, for if statement and we add new curly brackets. And essentially, what this is going to do is, if it's true, then it's going to do what's inside these curly brackets else, meaning, If it's not, if this condition isn't true, then it's gonna echo, or it's gonna do whatever's inside these curly brackets. So here we could do echo false like this. So if we refresh, it's true, so we're going to get true. If we set this to to now, we should get false here, so that's how you can do a simple if else statement. This condition can really be anything. It could be anything that you want it to be, Um, and then what's inside of here can really be anything that you want it to be now, another thing that you could do instead of checking whether it's true, you can check whether it's not true, so we can see if this number is not equal toe one. And we do that by replacing one of these equal signs with an exclamation point. So this essentially stands for not equal, and you'll find in PHP the A lot of times this exclamation point will stand for not so. That's something to keep in mind in this case again, not equal. So if we do that, then is the number not equal toe one? Yeah, it's it's too, so it's not equal to one. So that's why we get true. So this is a good way of checking negatives, checking to see if something is not equal to something else. So that's another thing that you can do there. You can also do this between different variables, so let's set this back toe one. Let's come down here and do. Number two equals two. All right, so now we can see if number is and will changes here in a second. If number is equal to number two and we refresh that. And of course it shouldn't be because this is a one and this is a two, so we get false. If we change this to a one, then we'll get true so you can compare variables just like that. Another thing that you can do as you can create multiple conditions, and you can use different statements for that. So let's go ahead and create Number three equals three. And let's change this to one. So if number one or if number equals number two. And there's a couple ways that you can combine condition so you can use and you can use or so in this case will use and number one equals number three. So what this is saying is, if we don't have number one, we just haven't number. This is saying if number is equal to number two and it's equal to number three, then will echo True. Now we know that's not true, because this is number three is set 23 So this will go ahead and give us a false statement . So it has to be with that and sign it has to meet both conditions. Right Has to meet this condition here. Number equals number two, and this one here number equals number three. So that's and with or you can do it like this and its two pipes. And now what this is saying is it's if it meets either condition if meets one or the other . So if we refresh this we see we get a true because this one evaluates to true number one equal is the same as number two here. So you get with the oar statement as long as one of the other is true, then you'll get the this first line here you'll get the true statement. Okay, so that some stuff keep in mind also, you also have a way that you can check not only the value but the type. So if we set number two in ah, in single quotes like this that makes this data type, it changes it from a number an integer toe, a stream, and so now weaken. If we add 1/3 equal sign here. We'll go ahead and get rid of this condition for add 1/3 equal sign here. What PHP is going to do is it's gonna check. Not only is the value the same so the values the same. It's one. But is the data type the same? So is, are they both is number two a, uh, imager, just like number. The number is. So if we check that, you'll notice we get false, and that's because they have different data types. So that's come something to keep in mind if you want toe, um, also checked the data type. You can do that by adding this third parentheses here. If you want to check if it's not equal now you add your not signed at the beginning, so you have your explanation point and then to parentheses. And if it's not, both the value end the data type the same, then it will echo true and you see we get to true here. Okay, so that's messing with if an else Now the last piece of the last thing that you want to pay attention to is something called Switch and Switch is something that you would use if you're going to have a chain like instead of doing a long chain of If else so sometimes you may need to check multiple things. So instead of doing if else, you can also do else if like this and write another condition, right, Instead of doing a whole bunch of those, you can do what's called a switch. Ah, in PHP. So switch, then we check our variable. So we'll go. Number like this will put in parentheses and then we create our curly brackets and then inside of here, we can do a number of different different cases that we can check against so we can do case one. Then we're going to echo true. Okay? And then whenever you do this, you always put what's called a break. So break right here like this, and I'll walk through this here in just a minute. But that we could do case and we can dio ah, to And we can do echo false. Okay, uh, fourth. And then I'll cover that and a second we'll do a break here. Okay, So what this is going to do is it's gonna look at this variable right here. Number right. That's what we're specifying and is going to see if it's if it's equal toe one. If it is, it's gonna echo true and then stop. If it's not, that is going to continue on and it's gonna look at case, too. If that's true, then it's gonna echo false and then stop. So if we refresh this, then you'll see. We get this extra true right here because it's true for this particular case. Now, if we set this to to, then you'll see we get the false here. So what this allows you to do is you can go on with these cases here so you could know, do go on and do case you could check. Let's say you want to check against a string like this so you could do three like this and see if it's equal to that. An echo, maybe three, like this. So again you can go through and check a bunch of different cases and so forth. Ah, and see e m. I break here and see if it's if, ah, it's This number is equal to that so that you could do a bunch of different checks on down the path. So, um, it allows you to do a bunch of different if else statements without having to write out all this it felts syntax. Now one thing with this is ah, lot of times you want to set some sort of default. And so if we come here, we can do default like this. And what that's going to do is and let's say said, That's something we can do Echo. No idea. So we don't know if it's true. Riffles. Okay, so what that allows us to do is if all of these are untrue, So let's set this to say four. Right? So it's not. It's not one, it's not, too. So then we can set it to four or reconsidered to the default, which is no idea. Okay, so the default allows you to display something if everything else doesn't match isn't true . So if it's not equal to one, it's not equal to two. Okay, so that's what the default allows you to do on switch. So again, it's a very, very quickly toe. Check a bunch of things without having to write these long blocks of if else statements 8. For and Foreach Loops: welcome back to PHP 101 in this video, we're gonna get into four and four each loops. All right, so let's start with a four loop. So to create a four loop, you'd start off with word four like this, and then you have inside your parentheses. Here you have three things that you specify. You specify an initial value, you specify a condition, and then you specify some sort of increment. Right? So the first thing we'll do is we'll set this variable creative variable called I, and we'll set it equal to one. OK, so that's our initial value of our variable. I in their variable eyes, what we're gonna use throughout the rest of our four loop, um, to do our checks and display or data and so forth. So we're gonna set initial value toe one, and then we're going to specify a condition. So this is a condition that's gonna tell the for each loop and whether or not it should really kind of continue processing whether or not it should display what we're gonna put inside of our curly brackets. So, um, in this case, we're gonna set I less than or equal to 10. So, Aziz, long as I is less than or equal to 10 we're going to continue running our loop, okay? And then we're going to specify an increment, and we're going to set that to I plus plus what does? So what this is going to do is our initial value, be one. So the four loops gonna check if that is less than or equal to 10. And it is. And so if so, then it's going to do whatever we put inside our curly brackets here. And then it's going Teoh increment this I by one. So it'll change it to a to and they will run the loop again. And then a three and run the loop again a four. Run the loop again. And so keep running this loop until it meets this condition or tell this condition is no longer true. So if we come here and we just do something simple like echo, um and we'll do I end a break like this. If we do something simple like that and we refresh this, then you'll see we get 1234567 Once it reaches 10 then it stops. So this is a really handy way if you need to, like, create. Ah, if you need to create a number list like this or you need to create some sort of data and you don't want to write it all out by hand, then you could do this. For example, I use this a lot with, um, you know, once best, when creating an interface and you wanna have different text size is that someone conceal ECT so they could select ah six point font or eight point or 10 point or 12 point or whatever. This is an easy way to create that option inside of that select select box, without actually having to type out all of the different numbers there Now, a couple things here. One. You want to be careful with your conditions here because sometimes it's easy to set a condition that will either. That will kind of always be true. And so then you your your loop will just keep running and running and running and running. Um, the other thing is, if you want to do different increments here, you can certainly do that so you could do I equals when you could do I plus two, and that's going to give us a little bit different increments. So if we take a look at that, then you get 13579 OK? So you could do you could do five if you wanted to hear. Um, you could do whatever increment you want that that makes sense. You can also change this so this could be zero instead of one. Or it could be any other number. Could be like eight. Whatever the case may be. So there's some manipulation you can do here, um, with this. So keep that in mind, Um, and it's just a handy tool for kind of creating data that you might meet now the counter to that then or the sister brother of that is what's called the for each loop. So for each is like this, parentheses just like above. And then our curly Q brackets. The difference is, is we're actually going to be referencing something and really, where we're gonna be referencing his elements in an array. So if we create a ray real quick here and let's just do a ray equals name was a ray function and then say name, email, Address Something like this. Okay, now we can actually loop through our array and do different stuff with the data. So what we're gonna do is we're gonna spend we're gonna reference our race. So for each parentheses and then array, and then you were used the word as, and you can do this a couple of different ways. So Ah, you condone a name, name like, um, data like this, right? And then went inside here when referencing the elements. He could do listens, do this echo. And then we could do Dada, and I'll do a break like this. Okay, so whatever, let's go and show this. So as it loops through the array, the value is what's gonna what's gonna go here, Right? So the value of the element is name, and the 2nd 1 is email. And the 2nd 1 or the 3rd 1 is address. So it outputs name, email, address. All right, so you can do that, or you can do like this. And what this is going to do is now you can reference both the key end the value, so let's actually changes to value. So that makes a little more sense. And now we could do something, like Key and user key Variable here and then data like this, They're actually let's change that to value since we changed that above. And so if we look at this, then you see we get key Zero is named key. One email key to address. And that's because now we have access to both the key end the value from our array. And, of course, this was an indexed array, right? So if I just print are this array so you can see it, you can see are name email address. Right. So 012 key 12012 Okay, so you can reference both the key and the value, and that's what for each loops allow you to do is loop through raise and output, different data. So you may. This may be an HTML table that you're creating her maybe, ah, definition list and maybe a paragraph tags. Whatever you want to create here, whatever you want a loop through and create, you can do that with the for each loop. It could be a non ordered list, so lots of different things that you can use this for oftentimes again. It's used mostly in conduct conjunction with database data. So you grab something from the database, you get it back in Honore, and now you wanna loop through that array and you want to display it in a table or a definition list or non order list or whatever, right? So that is using four and for each loops. 9. While Loops: Welcome back to PHP 101 in this video we're gonna talk about while loops so similar syntax to four loops and for each loops. But of course, in order to do that to do a while loop, we're gonna use while instead of foreign for each So while then we'll have some sort of condition here, and then we will have our curly brackets, and that's kind of the basic syntax of a while loop. Now there's some things that you need to do in order for this to work and not cause you problems. So first off, you need to have some sort of conditions. So just like our four lope will use the I variable and let's say it's less than 20. Um, then we'll we'll go ahead and do whatever it is that we're gonna dio. Okay, Now, if you just run this like this, you kind of have you run into a little bit of a problem because I is not set anywhere else , and you don't set it inside of the while loop. So it'll just keep running and running and running and running because I is always gonna be less than 20 because it's never been said So what we need to do First office, set outside the while loop Are I equal to something? So we consider I equal to zero, right? But even still, this isn't This is still gonna cause a problem because I is not being incriminated, so I will still always be less than 20. So in order to make this work than inside a while loop, we then need Teoh increment our I variable so that now, each time it runs the loop, it will go ahead and it'll increment I. And eventually it'll reach. It'll become equal to greater than 20 and this will stop running. So this is just like a four loop. You set your initial value. Here you have a condition, and then you haven't increment. It's just not all inside of the parentheses here, so it works a little bit differently now. For that reason, sometimes or often times while loops can be a little bit more powerful or a little bit more useful simply because it's spread out like this so you can do doom or things you don't have to contain it all inside of, um, the parentheses here and you can actually manipulate a little bit more. So you probably find yourself using while loops more than four loops. All right, so then inside here, we'll just go ahead and echo the value of I. So we'll do this year and we'll go ahead and we will run that So you can see down here. Then we have starts off a zero, cause we set it to zero and loops all the way until it reaches 20. Because we have less than 20. Now, if we did less than or equal to, of course, it would then Anchorman up to 20. Now, another thing that we can do here with while Loops is we can use them in conjunction with a raise. So if we create ah, a new array here and was given a name array and we use our array function to create it, and we'll just do what we've been doing name, email and address like this, and then what we can do with this array because it's we've created an index to ray here and we have access. We have this looping through creating numbers. We actually use I to get a hold over toe, um, to use our elements inside it Far Ray using the indexes. So if we say while we do a ray and then we do I like this, then what this is going to do is it's gonna loop through. It starts at zero. So this is essentially the key of zero. So it's gonna look for the key of zero in our ray and see if it exists. And if it exists, this will evaluate to true, Which means the while loop will run, that will go. This will increments so they don't go one, and then it will go to. So it'll go through this array and it'll pull out these elements. But once it gets to four or once it gets toe three or the fourth element, then it's gonna evaluate to false because that doesn't exist in our way. So let's go ahead and take a look at this. If we go like this and we echo out array and set the variable here array and then I like this what this is gonna dio you look here, it loops through and you see, we get name, email and address, and again, the reason that works is because this is, ah, indexed array. So if we print our on our array, you can see 012 So this is when this is zero. Will get name when this is one will get email and when this is too will get you or were good address. And then once it's three, there's no there's No three in our array here, so it returns false and returns false at every point from there. So again, we can use this to, ah, loop through different raise and do things like this. All right, so that's some. Some usage there with while again could be a very handy tool. If you're working with, say, for example, a CMS like WordPress, their main loop is a wild loop, so it's good to be familiar with this and understand how it works so that you can have some familiarity when you get into some of those programs and so forth 10. GET: Welcome back, PSP 101 in this video or this lesson we're gonna be talking about Get variables. So you may have noticed if we come up here to the Earl Pair, you may have noticed sites where they have at the end of it, a little question mark. And then it says something like, Name equals John or something along those lines. What? This is up here. These are essentially you are l parameters that you that that in PHP they get they get automatically added to super what's called a super global, um, or system array. That is this Get variable that we're gonna be taking a look at. So what allowed to do is actually access thes you, Earl parameters and do something with them inside a PHP. Now, this isn't really the standard way that you're going to do a lot of your post passing of information back and forth between pages. Will gettinto post array in the next video, um, or the post super global, But ah, and that's the main way that you're going to do this. But this is ah used in certain things. For example, the place you'll often see this is in some sort of search. So you may have ah parameter like this where it says s equals. And then whatever search term was entered into the form. So search term like this or another place you may see it is if you're inside logged in somewhere, you may see something like you equals 10 or 1 24 and this is your user, I d. And so then it's That's how it knows that you're that user and it's pulling information from the database based off of that. Or if you've seen the WordPress, you maybe see something like P equals 34. That's the post, the idea of the post again, pulling it from the database and so forth. And that's how it knows what post your on. And then it can pull the data for that post unloaded into the template. So there are uses for this. And so I'm gonna show you how to use thes for our example. Let's just go ahead and set Name equals John, and we actually go to that page. Now, this isn't gonna do anything right now because we haven't written in code to handle to get variables but you'll see everything else loads just fine, so it doesn't affect anything like that. And then if we come down here, we simply just print out this. Get variable a super global. So you see, it's dollar sign underscore and then capital G ET. So if we print that out, you know it's haven't set this anywhere, haven't done anything. All I've done is put the URL parameter up here and then printed out our get so you can see here. This is now in an array for us. So it's something you know. It's very handy for us to be able to use and work with this. So again, very straightforward. Um, it's a courageous like any other array, so you can go ahead and use it just like you would any other right now if we add parameters . So let's say we want to have multiple parameters we could do, and email equals John at something dot com. So if we do that, you can see that that's now added to our array here. So again, it's just grabbing all of these. You are l parameters that you see here. All right. So as I mentioned one of things that this might be used for, say, some sort of search. So let's go ahead and just do a quick little form, and I can show you kind of how this works. So we come down here and let's just create a form. And when you create a form, you can specify the method and the method can be get or it can be post, okay. And so that determines. Then which one of these super global? Because you're gonna find your variables in can. Most often you're gonna use post. But in this particular case we're using gets we'll just put get, um And then you would specify an action, which is the U. R l that you're going to send that information to in here. You could either just leave this off or leave it blank like this, and it will repost it back to the same page. So that's what we want to do now. If you had a processing script, say it like process dot PHP. Then you could specify that, um, again, in our case, will just leave it blank like this. All right, so that gets us the start of a form here and then inside our form will have just a simple have a label and we'll say inter search term like this and then in here will have an input type equals in this case Weaken, weaken, put search. Since it was gonna be a search. Now this is an HTML thing. But that'll in many browsers specifying a type of search as opposed to text, will add additional functionality to it. That's helpful for searching like, um Ah, history, auto, complete stuff like that. So, uh, even if you want to make a more useful search box, you can use search here. But that's not really PHP the name of this. Then we'll set toe s for search, and we'll go ahead and leave that just like that. So I'm gonna go ahead and clean this up here just a little bit and all right, so we have this here and I need to, obviously, because we're still in PHP. I need to end it like this. And let's go ahead and do print are on our get variables here like this. All right. And so that should give us ah, little form that we can submit and get some information on. All right, So if we enter in our and actually I have ah thing clear down here we can get rid of. But if we enter in our search term here than let's just do say, for example, Yost, since we have Joosten here, do that. You see that? Then we have our array s is Yost. And if we come up here, we'll see that up here s equals zero. So search equals Yost. So then we could query our database for that search term based off of what we found in here . So our code right here could handle all of that. So another thing we could do here's then come appear and we could say echo, and we could do not mean to single here p And then your search term is, and then we can do dot and we can do get Yes. All right. So we're getting again what our search term is. So you can again. It's an array so you can call out, or you can pull out certain elements from that array just like you could any other race, so you could see here your search term is yost And so oftentimes what you'll do is you'll value equals. You'll echo the search term actually in the value blocks. So it's it populates our box here. So get s like this and will refresh that one more time. And you see, it's in our box here, right? So Ah, pretty straightforward. One other thing that you would want to do here, then, is if we go here and let's just get rid of our search term here for a second, go to this page. You'll notice that even though we have nothing up, there were still getting this box that says your search term is Well, that's kind of that's kind of ugly, right, because we don't want that to show unless there's actually a search term entered. So what? What you'll often want to do almost pretty much always want to do with get variables as you want to create some sort of if statements so in this case, will do if and will do is set. So we're gonna see if get s. Our search term is set, and if it is, then we're going to display our little we're going to display this here and so will come come down here and we'll close that like that. Move this over. So it's it's a little better. And so then, when we have no search term entered its not there. But if we again enter a search term, you'll see that now it shows up so often times, that's what you're going to do. Um, when you're when you're doing something like this, you want to check to make sure that the variable that you're after and get is set so that you can actually make sure it's there and use it. You also could check the whole array so you could do, if not empty and just do the whole get a ray like this. So now we're checking to see if the array itself has anything in it. Not a specific element, but just anything. And if it does, then we're going to display so you can see we have our Yost displayed there again. If we get rid of this, there's nothing in there. So that goes away so again, some stuff that you can do. You you almost well, you always pretty much want to check to make sure that the variable you're grabbing from a parameter is set also because this is often user submitted data. You, of course, want to make sure that you're escaping your data that you're if you're quitting a database that you are protecting against SQL injection attacks. So either using prepared statements or doing some sort of checking along those lines to make sure that your your sites not getting hacked and so forth so again that's using get variables inside a PHP. 11. POST: Welcome back to PHP one. No one in this video, we're gonna get into post variables. So what I'm gonna do is I'm actually take all of this stuff that we used with get, and I'm gonna come down here. I'm just gonna copy it because it's all very, very similar. Um, and you're going to see here in a second that we can use a lot of this same kind of syntax and do a lot of the same things with post and get It's just a little bit different way of dealing with, um you know where the variables are and so forth, but it's very, very similar. All right, so we'll go ahead and call this post variables. And so in order to do this, all we have to do is come down here and said Are four method to post a supposed to get and now this is gonna post it. You could say silently in the background. It's not gonna be in the URL up here. And we can really leave the rest of this in here the same, except for this part right here, because we're not gonna be using get we're gonna be using post. So this is going to then post the data to our the page that we specify here again by leaving this blank is just gonna come right back to this same page. Now, we also need to come up here and change this to reference our post array and so will change this to post and we'll change this to post. So let me go ahead and just kind of clean. You see, I have some kind of weird stuff over here, so let me go ahead and clean this up a little bit. I got a lot of code going on here. So, um, let's go and get rid of this one, All right, so that should get us cleaned up here and now we can enter a search term here so we'll just go back with our Yost and you'll see that we have our search terms. Yost. We have entered here just like we did before, but if we come up here, you'll notice that it's not there. It's not in the URL, and that's because it's being posted kind of silently in the background. Ah, you know. And so it's again. It's not visible through the URL here. All right, so that that's a simple way of using post post is what you're gonna be using. Ah, most often in order to perform, you know, interactions with forms and so forth simply because you don't again. You don't have all the information in the U. S. A lot times the information that's submitted a sensitive you don't want it up there. For example, you could imagine a log in form if someone submits their user name and password. You don't obviously don't want that showing in the URL up here. Also, there's a limitation on the amount of characters that you can use in the variables and the parameters in your, um, you know, in the U. R l parameter. So if you you have a form that has a lot of information that you have a long say, contact former application form or something along those lines, you're gonna run into a limitation with the amount of data you can actually throw up there in the oil. So you want to use post for that reason as well. But all in all, there's not a big difference in terms of how you use post and get it really comes down to the way that information is passed from one page to another 12. How to Send Email With PHP: Welcome back to PHP 101 I'm gonna be showing you how to send mail with PHP, so I'm gonna be showing you the function in PHP that you'll need to use. In order to do this, I'm gonna show you how to set up some of the parameters and show you how to add things like a from e mail, a reply to send html emails and so forth. All right, so let's start out by taking a quick look over here on the right hand side and you'll see I've gone ahead and sent this test email over here and so you can get a look at what you're actually gonna get from this here. So there's a couple things of note here. First, you'll see that we have our subject line here we have, Ah, who this was set from, and two So you can see it was sent from what I call the sender name here and then send her a john Morse online dot com, which is something I made up. And then I sent it to test at john Morris online dot com. You notice here we have some some larger text here and then some smaller text here. So this is actual H to mount email. And then if we come down here, we come over here and we hit Reply. You'll notice that even though I sent this from sender at john Morris online dot com, the reply is to reply to at john Morris online dot com. So this is all stuff that you can control inside of your code when you're sending emails with PHP. So let's go ahead and hop on over to the left will take a look at that. So the way that we do this is we use the mail function in PHP. So if we go to the web broke quick, you can just go to Google and you can Google mail and then PHP. This is the top result. As of this recording, I'd imagine that it will stay that way and so you can see all of the parameters. Here we have the two parameters. So who were sending it to? You can see some examples of how you might do this. You notice you can actually create a comma separated list of e mails here so you can send a multiple recipients that way. Your subject line, of course, is gonna be your subject line. That will be a string. Your message again you can do playing tax story can do eat H two mile. Although you do have to set some headers when you do a c'mon, we'll go through that. Then you have additional headers that you can send here those with headers that I was just talking about. We'll show you some of the common ones and then did additional parameters here that you can send, which is something that I rarely ever see used. All right, so you kind of go through this, you can look at some of the examples they have here. Ah, if you need a little more in depth or a little more information on it. But we'll walk through all of this stuff, right? So first off is the recipient. So that's our email address that we're gonna be sending the email to again. This could be something you hard code manually, like I've done here. It could be generated through your PHP code. It could be pulled from a database. If you have some sort of user system, there's lots of ways that you could come about this particular email dress or or like I mentioned earlier, comma separated list of email addresses. So a number of ways that you can do that. But that's essentially what this two parameter is is just the email address that you want to send it to next. Is the subject so again, pretty self explanatory? This is the subject line that you want to use when the email goes out the message. Then this could be plain text. Or it could be HTML. You notice here I'm using HTML, some using H one tag a paragraph tag here. Now the one thing to keep in mind with when using HTML emails is every email providers gonna render a little bit differently. So it's a good idea to do a lot of testing and different email providers. If you're using HTML. Now, if you stick to basic HTML, you probably all right. But if you start getting into a lot of images and styling and so forth, then you may find that in certain email providers doesn't show up exactly how you want. And so you just need to do a lot of heavy testing with that and there are frameworks out there as well. That have kind of done a lot of that for you that you can plug into and so forth. Obviously, that's beyond the scope of what we're doing here. But again, you can see here that I'm using html inside of my message, which is completely possible with this, right? So next let's actually drop down here to this header. So if you're gonna send HTML emails, then you need to include this header right here. And so this essentially just sets the content type of the email to text last each two miles so that they know that it's an html email and that it can be processed properly as each to my little Wiesel. Assume it's plain text on and then it will actually just display all of your HTML code. So if you send if you're doing this and you send the email and in your email you actually see that HTML tags, there's probably something up with this, and you want to come here and take a look at that, right? So for each to mount emails, you want to make sure you have this header now some other common headers that you might see . You have your from address, which is if we pop over. You see, here are from address. I said it as thus under name. So you can put whatever name you want here again manually. Could be pulled from a database, etcetera. And then the email address inside of these little less than greater than brackets here, this is actually I mean, this is optional. You could just put the email address. Ah, if you wanted to hear. This is kind of the standard way, Most most the time. You see email addresses like this. So that's why I formatted it that way. Probably want to format it that way as well, unless you have some compelling reason not to. But that's the from email address. And then we have this reply to. And so you remember when I hit reply here it went to the reply to email address. So that's where you actually set. This is right here in your headers. You can set this reply to email address. Right. So we've concentrated all of that into basically one long string for the headers for the email that we're going to send and then you could see we come down here and in our mail function, we just drop in our to our subject, our message and our headers. And then whenever this page was visited, when I visited this page that it went ahead and processed it and sent that email to me here . All right, so again, pretty straightforward in terms of actually using the function now again, of course, depending on exactly how you're going to be using this, you'd obviously want to have some sort of security checks. I generally try to avoid talking about what security checks you would put in in a tutorial like this, simply because those things tend to change. And what you need to do is keep up with the latest info security info that's going on at the time. You happen to be watching this tutorial, so this gives you the basic kind of idea of how to use it and then depending on the context in which you use it, if you're using a contact form or you're pulling from a database, that would be different security considerations. And, of course, the time period in which you're using it that will also affect the security. Ah, considerations as well. So just be sure to pay attention to that stuff, you know, adding captures of necessary honey pots, you know? Ah, you can validate the email address and so forth, so just make sure Teoh to see what's out there in terms of doing that and then implement that in your script. 13. Create a PHP Contact Form: Welcome back to PHP 101 we're gonna be talking about or what we're going to be showing you how to do is how to create a contact form using PHP. There's probably 100 different ways to skin a cat. There's a bunch of different ways that you could do this if you went out there and search for tutorial on this or code on this, you probably find ah, 100 different ways that it's done. So I'm gonna show you one way that you can do it. I'm gonna show you some of the advantages and disadvantages of doing it this way. All right, so let's take a look at what this contact form does. So you can see this is a pretty standard contact form, name, email message. And then I've added this math thing here. You could do this with recapture. You could use a honeypot if you're familiar with those terms. I'm just doing a simple math one here as a kind of a way to deal with people with with robots, essentially with scripts and stuff, submitting this form. So we'll go through how we do that. All right, so I'll go ahead and I'm just gonna I'm gonna actually trigger this not working. So the first thing I'm going to do is just enter my name and I'm not gonna enter. The rest of this information will hit Send here. And so you're gonna see we're gonna get some error messages and says, Please enter your email. Please enter your message and your math is suspect. So what this is doing is it's validating this form and making sure that this answer is right. We've entered information for the message and for the email. And so, in this particular case, we're requiring all of these fields. Now, if some of these fields that you enter in your form aren't required, then obviously you don't need to do these checks and so forth, so you can kind of Well, when we get to the code, you can kind of work through that again. It really depends on what you're for, miss. Now if we go ahead and enter this correctly, then enter all of the information and we hit send. Then you'll see we get a message that says your message was sent will be in touch. So pretty straightforward, A common thing you'll see. But if you've never done it before, sometimes can be a little tricky how to figure out how to do all of this. All right, so let's get into that. All right, So over here on the left side, there's a number of things that we're gonna go through here. Um, kind of another note is I've This isn't how for the trolls out there. Really? This isn't how that I would necessarily go about building this in terms of the organization , because I tried to put everything in one file. So it's easy for people to get access to. They don't have to download 10 different files, So everything's in one file, um, for you to get access to. Plus, I know most people are trying to integrate this into an application of their own, so it doesn't make sense for me to build this application that they didn't have to tear apart. So I really do it kind of down and dirty. That's why you see a function in here. You're going to see some styling custom styling in here. Normally, what you'd want to do is this part here you would want to put into its own file. If you have a functions file in your application already, you'd want to add this in there Or if you have something already does this than you don't need to worry about this part here. This stuff you'd probably want to put into a conflict file. Um, and, you know, include that file, the styles she down here, you'd want to put into a step separate style sheet and then include that here and so forth . Okay. So don't get too caught up in the organization. It's really the code that matters. And you can figure out how to fit it into your application. Right? So the first thing, let's just take a look at the each to mouth. So you can see we kind of have a standard, you know, header up here. One thing you'll notice. I'm using bootstrap for the styling. So that's why I really don't have much styling here. Um, I added some padding around this message here. So added 15 pixels around these messages. That's the only styling I did. Everything else is default. Bootstrap. So that being said, you can kind of gather that then the HTML down here is really gonna be default bootstraps. So and that's what it is. Section, container row column. And then inside of that, if we come down here to our form, which is the big thing we have are formed that the two biggest things Teoh notice here are method equals posts and action, Ingles Index, stop. PHP. So we're submitting back to this same page here. And that's why all of our PHP is up here because we're submitting to the same page. Right? So we have a label, so we have our label right here. Um, against standard bootstrap, we have our input. Biggest things to get out of this are the names. So the name equals name here, the name equals email here, and then the name it cools message here. So name, email and message. Because those air what we're going to get from our post data when we submit this form and I'll show you that right? So, again, the html is all standard bootstrap mark up. You can really mark this up however you want. You know, you could look through this. I don't want to spend a ton of time on the HTML because this is really more of a PHP tutorial. All right, Next we have the check this check down here are math. So the name of this one is then human. Okay, so name, email, message and human. And then down here, we have our submit button. All right. So again, standard bootstrap. Just to make it look a little bit pretty, you could, of course, come in and change its up How you want. All right, so as you probably know, but I'll show you what happens then. When you submit a form like this is that it sends the data that is entered in the form fields to the page that you specified to this action here, so index dot PHP and it uses the methods you specify. So in this case, we're using post now, you could use get, And what that does is actually passes all of the information in the URL. Generally don't recommend that because there's a limit on the amount of november information you can send via u R l plus it just makes your be oil like, really, really dirty. Um, so most often for a form like this, pretty much always you're gonna use post. About the only time that I really see using the get method is, um, first for something is is search forms. I mean, there's other instances and so forth, depending on your application. But an actual form that submitted using get is most often is just search forms. So ah, most reforms. You're gonna want to use this postman method so it's gonna post the data to index stop PHP . Meaning it's gonna basically gather up all this data and it's gonna send it to back to the same page when their page reloads, then is going to send it back to this page and is gonna put it into a variable called post . So if we just simply do prints are and post right at the top here. And let's go ahead and submit this again. And actually, I'm gonna have to go ahead and upload this. I'll move this over here so you don't have to watch me uploading stuff. And I actually have a nitrous app here. Nitrous stop a lot. People ask me what editor I uses nitrous dot io. So I have to sync up my file real quick. Um, so we'll go ahead and let that work here. Just a second, and then we'll upload that file. I'll try not to do this too much. And if we hit sand, we should get the post area at the top here. So you'll see. It took the data that I entered here, and it posted in their name. Email. Um, contact John Morris online dot com Message test, human seven. So it put all of it into this array. So that's what we need now that we have the data that was submitted in the form in an array this super global called post. Now we can use that data to interact with PHP and, uh, send or email or send our contact form. Okay, So, basic, You know, that's just kind of basic PHP stuff. Now we're gonna do a couple things. First off, we want to validate our math. So this is just the first thing that I'm going to do in here. I'm gonna check whether or not this is correct. So ah, five plus two should equal seven. So I'm just going to see if Post human equal seven, remember, are very where are formed fields were name, email, message in human. So we're gonna check Post Human to see if it equals seven. Now, you notice I'm wrapping this in a function called in Val. This is essentially just gonna make sure that whatever is entered here gets gets turned into or is an in Inger. Okay, so we want to make sure that we're working with an imager. Um, and then we're going to see if it's equal to seven. If it's not, we're not actually killing the script yet. What we're gonna do is we're gonna set this errors array, which we in stance seated up here, and we're gonna add an item to it that says you're Mathis suspect. Now, you could change this message to whatever you want, right? So whatever makes sense for your site, so change that, um it's gonna add that to that array, and then we're gonna keep We're just gonna keep going. So next we're gonna validate the email address. So we're going to see, check and see if it's empty first, if the email address is empty, so post email and we're going to see if it's empty. So that's right here. So if it's not empty and we're going to run this filter of our function here, which is essentially gonna look at the email address and it's gonna run it through whatever filter we specify in this case, it's filter valid e email. So this is just going to check and make sure that we have a valid email address. So if the email address is not empty and it is not valid, then we're going to set another air that says that is not a valid email address. Now the reason we add the not empty you're gonna you see down here we're gonna white list for empty again. Down here is because if we don't set it, don't do the is not empty up here. Then you'll end up with two error messages if you have an empty email address, one that says that's not a valid email address and one that says, Please enter your email address. So this just helps clean up our air messages a little bit, having this not empty right. So again, we added, If it's not a valid email address, we added this. Ah, we're going to add this to Item two are air message. So now we would have your Mathis suspect, and that is not a valid email address. Both would be in our heirs or a next. We're going to do some white listing. So there's probably, you know, if someone's trying to hack your form, they could be trying to Smith all sorts of different, um, different form fields to the form. You want to make sure that you white list only the field you want. That's what we've done appear. So we specify are a wait list, Um, and in this case were doing name, email and message because these are the only ones that are actual information we want. The math check is just to run this check here, so we don't need to worry about white listing it right. So name email messages Wait listed, if you add. So that's the thing. If you add fields to your form in the HTML, you need to make sure you wait list, um, up here as well. All right, so what we're gonna do is then we're gonna loop through our white list, and we're going to check. We're going to set than this fields array with Onley. The values from our post array that are in our wait list so that it may seem a little confusing. So let me try and explain a little bit. So we're gonna again loop through white list, which is name, email and message, and we're going to then grab the values from post for name, email and message. So if there is ah, item in post that has name we're gonna grab that and we're gonna announce that that as fields name So essentially, just creating a new array out of this But Onley, including the stuff in our white list, because we're looping through a white list array on Lee, the stuff that we've added to our ah white list are we gonna pull out of this race? So there could be 100 things in this array that somebody submitted. All we're gonna pull out is whatever we've added to our wait list. So name, email and message. That's what this little these couple lines right here doing so that's essentially white listing and just grabbing the stuff that we want this way better than blacklisting trying to come up with all these scenarios that people might want just Onley. Look, these are the things I want. So these air what I'm gonna grab, right? So we've grabbed those. So now we have the fields that we want in this new array called Fields. Now we're gonna loop through that array, and we're going to check the data, so we're gonna check the value, and we're going to see if it has data in it. So we're going to check and see if name has a value in it if contact has a value in it. If message has if something was submitted in that field, if it's empty, then we're going to add a line to our errors array that says, Please enter your and then the name of the field. So that's why we're doing fields as field. And you know this data This will allow us to you to both grab the key and the value from the array. So ah, again, we're gonna enter this line. This will add this to our heirs or Ray. So, um, now, you know, if this was filled out completely blank, then we would have several messages one that says your math of suspect one that says, Please enter your name when this is pleased their email when this has please enter your message. Okay, so that's what we're doing here. So you can see that if I just remove. If I remove all this and I hit send, then you can see there's nothing in our post story that was sent Causes all blank. And you can see that your math of suspect, please. In the name, please. So this is allowing us to display these air messages. It's also then down here we're gonna check. So in order to send our email, we need to know that everything went through a case. We're gonna check our heirs array. And if our errors array is empty, that means we didn't have any heirs. So we can now send our email. And so we're just going to use the PHP malfunction. We're gonna use email, dress subject and then fields message. So the message from our fields array. So email and subject are two of the things that you set up the top here. So, you see, you have the section that says stuff you need to change for your form. One of those is email. Address one of those this subject. So you change these two whatever you want. This is where you want the emails to be sent to. In this is a contact form. So this would be probably be your website or your clients, email address or whatever, wherever you want that set. So that's we're going to use the mail function. Very straightforward. It's the email address, the subject and the message. And then you could add another one here called headers. If you want to add email headers to that, I want to keep this really straightforward and simple. But adding headers to an email like this isn't too bad. All right, so then we're going to send this. We're going to set the value of this. Well, after this function runs to, we're gonna set this variable sent to whatever this returns. Now it's gonna return. True if the email was sent, is gonna turn false. If it wasn't so. It's pretty straightforward check and we're gonna use that down here in our HTML. So that's our That's our PHP. And it's really pretty straight forwards do running a couple checks, white, listening our values, and then we're sending the email address. If we don't have any act Ares. Very, very straightforward. Now we can then use some of this to display these air messages and creating more interactive form here. And so the way that we do that is, if we come down here first off we have this kind of this. Ah, if else statement here. And so what we're going to do here is because all of this, when the form processes, we have this airs array that gets set, and we have this fields array that gets set. We already have everything we need to display our air messages. And if you notice if I put in just my name here and I hit submit, you notice that air goes away. But my name populates back in here. We have everything we need to do that in our heirs array and our fields or a that we already have up here. So what we can do is we can check and see. Look, let's see if errors is empty or not. So if it's not empty, then we know we have errors. And so we need to display some. So we have a divide with the class of airs, and then we have this little funky line right here. And I'm gonna go ahead and try and move this back a little bit. See if we can get this mostly on one line. So what this line does is this is this is going to display each one of these airs, but you notice it's all done with one line. Now, this is a little trick that you can use using the implode function. So if you're familiar with the implode function allows you to break an array apart and then and put it into a string using some sort of separator. So normally you might have use a comma as a separator. So if you're Ray had name, email and message and then use commas a separator, you would have name comma. Ah, email, comma, message, comma. Right. That's what implode does, and you can go online. And I recommend if you haven't worked with implode, look, you know, look it up and look at some of the stuff on it. But what this little line does is allows us to create this and really without having to loop or any of that stuff. And so what we're doing is we're imploding. are heirs. All right. Remember we added all those message to it. Now you want to display them, so we're imploding It and the separator that were using is this closing paragraph tag and then this opening paragraph tag. Now, the reason we're doing that is because you'll notice this. We've wrapped this PHP statement in the opening same opening paragraph, and then we've at close, uh, closed it with the same closing paragraph tag. If you look what you're going to get, then if you haven't air messages, you're gonna get opening paragraph tag, and then you're going to get a closing one, and then an opening one again and a closing one. So if we look at our her mark up over here, what it ultimately does if we go down here is you can see it creates perfect syntax, opening this air message and creating wrapping it in our paragraph tag, wrapping this one and wrapping this one. So essentially, it's a quick way to wrap all of these in the the, um you know, in the HTML that we want for this to display correctly so you could use a Nhan ordered list you could do whatever you wanted with this. But this is what you know for Bootstrap. This is what I decided to go with again. You can change it up how you want, but that's a quick little line here. So that's what's displaying our air Messages here were essentially just imploding them, which is kind of like looping through them and displaying whatever air messages we have in our errors array. So if it's not empty, we're going to display those messages. Then we're gonna do a check and said that after that s so if if it is empty, if our ERA raise is empty, then we assume that we didn't have any heirs, however, are male still might not have sent right. We may have got this far, and then we went to send the email and there was a problem. So we still want to check for that. And remember, we set that the value of whatever male returns to our sent variable. So we're going to check and see this is essentially checking to see if that sent variable is true. If it's true, then we know that the email was sent so we can display a success message, and that's what we're doing here. And this is just a straight each two mile success message that you can change however you want. So that's why when we have success here, then we get the success message. So that gives you essentially the toggle right here between air message and success message . And it displays whatever air messages air value valid based off what was entered in the form. So you can see that's not not a ton of code. If you if you write your PHP up top in a way that that that corresponds with how you have it down here, you don't have to write a ton of code to make this happen. So it's very clean or very concise way of doing this. All right, then. The other thing that we talked about is how we have our values. If we don't enter everything, we don't lose everything in the form they come back to us again. That's because we have the values in this fields array appear that we created, and so we can use those to populate the form, and that's what we're doing here. So we're setting value equal to so the value of this input field equal to ah the the corresponding post variable from our, um, postal rate if there's anything in it. So in this the way we do this is we run a is set on fields Name oh, Arfield email or fields Message on the the the field for this particular input and we're using a turn eri operator if you're not familiar with those again, I would recommend taking a look at those. But essentially, the way this works is we're doing is set and is set is right here. So it's the full is set right here. It's not the turn. Eri operator doesn't get wrapped inside these parentheses. That's a common thing. I see people do. You actually doing a full check right here? So you're checking if it's set and then you're using This is basically kind of a shorthand for if else so if it's true. So this is kind of like saying this question marks kind of like saying, if it's true, then I want this. I want to show this okay or this is what I the VAT. This is what I want. So in this case, if it's true. We want fields name. So if Fields name if there's a value in it, we want that. So that's gonna be this is essentially what we're gonna get back from our turn. Eri Operator, If it's true this you can essentially substitute in. But if it's false, then this is what we want and in this case, we don't want anything. So we're gonna set value to Blank, OK? And then you notice we echo this. So essentially, what's gonna happen is if if you entered a value in the field name than it's gonna that's gonna put that into this value field. So if I came down here at just did anything actually won't let me send it, all right? So if I I enter that you see, I get it back. And then if I come to message, even though my Mathis store on that, I'm getting all these values back so they don't have to be re entered, okay? And so this is essentially the line that you'd use for every one of these. Just copy and paste and cheap change out Well, change out the EEM it named feel name. So name email messages, a little bit different because it is a text area. So you're not gonna have value equals. You're just gonna actually put this in between the opening closing tag for text area. So that's how we get these to show back up here. So again, not a ton of code to get this to work. So you notice that this is really fairly, pretty concise way of doing this of creating a contact form that has all of the air messages you want re populates the form How you'd like we haven't used sessions or cookies or all of that stuff. Ah, that can sometimes get a little bit complicated for people were just doing it all right here in this page, um and, you know, having it work, how we want. 14. PHP Operators: welcome back to PHP 101 In this lesson, we're gonna be going over comparison and logical operators. So this is gonna be a lot of the work that you're going to do when it comes to working with conditional statements throughout your coats. We're just gonna go through There's a whole bunch of these and we're gonna kind of rifle through all of these to get you familiar with them, right? So we'll start off with comparison operators in the 1st 1 We'll see. Here is this one called equals, which is Ah, double equal sign. And so, essentially, to use this, you just write in if statement So if and then open parentheses. And this is where we're going to put our conditional statements. We're going to check whether or not the statement in these parentheses is true. If so, then you see, we have these curly brackets and the these this set of curly brackets. This opening one here in this closing one here contains what we're gonna do. If this statement is true case on this case, we're just gonna echo true. And then we have an else statement with the opening and closing Curly bracket. And in that we're going to write what we're going to do if the statement is false, which in this case is to echo false. So our comparison here have kept the actual comparisons really simple because I want you to focus in on the actual operators here. So we're just comparing 1 to 2. So is one equal 22 And of course, the answer to that is false. And so that's what equal does. It just checks to see whether they're equal or not. Now we're going to get into a something called Identical a little bit later, Um, and I'll talk about the difference between identical and equal. There's a there's a subtle difference there that you want to pay attention to, but for now equal, it just does what exactly it sounds like it do checks whether or not the two values are equal. Now, these don't have to be numbers. They could be strings, right. So you can see if, ah, certain string of text that maybe you got from your database is equal to something you're expecting, right? So does it. Is it equal to is the name of the person that you've pulled from your database equal to John. If so, then you could do something with that. Otherwise you do something else, right? So, again, that's equals the next one, then is really just the opposite of that which is not equal. So this you right? By putting an exclamation point in front of the equal sign like this, and so that essentially means not equal. So in this case, we're checking is one not equal to two. Of course, it's not equal to two, so they should be true. And you'll notice over here in our number to block. We get true. Okay, So again, not equal does exactly what you think it was. Okay, Now we get to identical and so identical is like equals. It's gonna check whether the two values are equal to each other. But it's going also check if they're of the same data types. So with data types, you can have strings. You can have numbers you can have ah raise. There's a number of different data types that you can have in PHP. So if you need to make sure that the data type is also identical is the same between the two values that you comparing, then you'd use identical. So here you can see where is one equal toe one. But here were using an imager, and here were using a string so they don't have the same type. So when we use identical, which is just three equal signs, you'll notice that we get false because they're not of the same type. And so that's the difference between equals and identical. Next, we have not identical so again does exactly what you would probably think checks to see whether the two values are not identical. And so, in this case, you again the numbers are the same, but ones and energy and one's a string. So in this case, they're not identical. So we should get a true here for this Number four. And you see over here for number four, we get true. All right, continuing. Move on. Then we get into some Ah, no. Comparing things greater than less than etcetera. And so Ah, here we have the these air. Really, The initial ones are really pretty much exactly what you might think. So we have in this case, eyes one less than two. Well, yes. One is less than two. And so this is the less than sign will do that sort of comparison for you and you can see over here for number five. We get true, pretty straightforward on, and then you could actually just switch this and do a greater than like this and check to see whether ah, it is greater than so. That's all pretty straightforward. Next is then greater than or equal or less than or equal. So if you add an equal sign here, what is going to do is going to check whether this one is less than or equal to this one. Now this isn't because it's not identical. It's just a regular equals than it's not going to check type. It's just going to check the absolute values in here, so one is less than or equal to, because it's equal to one. So this should give us a true and over here you'll notice that we get the truth okay again . You could do the same with greater than or equal to, and it would. It would check to see whether it's greater than or equal to all right. Next, we have not equal after type juggling. So this this value right here is going to check to see whether or not ah, this this value or this comparison is not equal after type juggling type. Juggling essentially is adjusting for the differences in the types that these might have. So it's essentially going to nullify. If you have two different data types here and then it's going to check whether or not it's not equal. So this is kind of a unique operator, but that's what it does you can see here. Ah, this is gonna again. It's not equal. So is one not equal toe one? Well, that's false. Who knows over here that we get a false all right up next is the spaceship operator, which is available as of PHP seven. And this is really call this a trying Mary operator because it's gonna run three different comparisons, So it's going to do a less than and equal to or a greater, greater than comparison, and it's gonna return negative 10 or one as a result. So you can see the spaceship operator is a less than equal sign and then a greater than sign, and each one of those corresponds to what it'll return based off of the comparison. So if it's less than then a return negative one, if it's equal to return zero and if it's greater than it will to return one. So if one in this case, one is going to be equal to one over here, right? So the idea here, then and so you'll notice one thing I want you to notice. Here we run. This were on number eight over here on her list. It returns false. Why does it return Falls? Well, because it's equal. And so it's returning zero and zero is synonymous in PHP with false. So that's why we get a false return over here. But what this allows you to do is it allows you to run a comparison and instead of it's not a on off comparison like you would have appear. If this do this else do that this is more of run this comparison and tell me what? Tell me what what it is is that lesson? Is it equal tours a greater then and then Once you have that, you can basically check for a negative 101 and you'll know which which one, you get back what the comparison you're running actually is. So in this case, we know it's equal, and then we can move forward and do with our code from there with that information. So it allows you to just run all three comparisons and see what you get back and then run conditional off of what you get back from this. If it's equal, do this. If it's less than do this, it's created than do this. Okay, so it's a really handy little comparison tool. That's obviously why they added it in PHP seven, because it allows you to again just run all three of those comparisons and get it back so that you don't have to go through and do. And if less than than this, you don't have to write this all out. You can just do the one comparison here, and then you can move forward from there, right? So really handy little tool again. That's the spaceship operator. Next one is one that was added in PHP seven as well, called the Knoll coalesce. So what this does is when you're it reads from left to right and the first value, which exists and is not know is the value that will be returned. Right? So what you'll notice here is that a the variable a has not been set, right? There's no a in here. And if we look at we were working with our numbers nine and 10 over here. So if we look at our no coalescing operator, which is this operator right here. So for this first statement, we're checking the A variable and then the one variable So essentially it's like a chain. It's gonna check is a Is this value exist and is it not know if it's If that's false, then it's gonna move on, though this this one is that value set and not know Well, in this case, remember, a is not set. So it passes over a and ghosts with one, and you'll notice that's why we get what we get back is the one. So that's what the knoll coalescing operator does. And it allows you to do this in a long chain if you prefer so again, here we have a which isn't set and then we have this be variable, which we've set to 12. So this should give us the be back because the first value in this chain that exists and is not known so be exists and it's not know, then that's why it should give us this Be back. If you look over here, what we get back is the value of B, which is 12. Okay, so that's what the no coalescing operator allows you to do. No, there's ah you know, it's similar to something like this, right? If we're checking is ah, checking to see if a particular value is is Knoller is not know than we would echo a. Otherwise we're Echo one here. It's a similar comparison. It's just a short, kind of like a turn eri operators. It's a shorthand for being able to kind of do this kind of thing. Otherwise, you'd have to do a long chain of these FL statements in order to do the same thing that this is doing all right. So get a number of different uses, places you could use that we'll get into some of that stuff later as we get into building more applications and so forth. But that's what the Knoll coalescing operator does. And again, that's something that's available as is PHP seven. All right. Next we can get into logical operators. So logical operators essentially kind of allow us to combine different conditions. Okay, so for example, the 1st 1 we have the and operator And what this is going to do is it that the these two ampersand signs together like this? What that's going to do is in our If block here, we're gonna we're gonna run to comparisons and we're gonna throw this and in there. And so what that means is PHP is gonna check both of these and like it says here, both have to be true for this whole condition to be evaluated to true. So one has to equal one and two has to be less than four. Both have to be true. And so both are true, obviously. And so for number 12 you see, we get this true statement. So that's what the end does now that's different from, or which is either one is true. So if one or the other is true, then this is this whole statement will evaluate to true. So if either one is equal toe one or two is greater than four. What we know to is not greater than four, but we know that one is equal toe one. So this one evaluates to true and since we used or this whole statement then is going to evaluate to true and thus we get true over here. All right, Next one is the not operating. We've kind of already looked at this a little bit, but we'll look at some ways that you can use it that we haven't necessarily. So we've used it in conjuction with equal sign and so forth. But you can actually just use it all on its own like this. And so it will check to see it's essentially like a boolean check. It will check to see whether or not this variable ah is is not set essentially in this case . So if this variable in some way returns false, then this is going to return. True, if that makes any sense, so it's a little confusing, but it's basically saying, if not true, if this variable is not true right then this of this. This statement will evaluate to true. So we have variable zero set to zero, so that's an equivalent of false and PHP zero and false. Our work kind of the same in Boolean functions in PHP. So this is essentially like saying it's false. So if this doesn't if this is not true, then that means this condition is met, so you'll notice this actually returns. True because we're on Lee echoing this statement. If this condition is true and you'll notice for number 14 here we get true. So it's a way of checking a variable. You can check. You can use the not a operator to check if a variable a set. So if this variable work wasn't even set, then this would return true because it does not exist if it's set to zero and if it's set the false essentially the three main ways that this is going to return true. So if you come down here, then we can look at this. So if we look at not variable one well, variable one is set toe one which is in Boolean terms is the same is true. So this one is actually true. This variable actually exists. And so you notice over here we don't have a number 15 because we only echo if this statement is true. We come down here to variable number to you. It's set to false specifically to false. So are not Evaluator is gonna return. True because this variable not is not true. That's what we're checking with this. So you see Number 16 we get true and the number 17 this is set to true. So this evaluates this condition This whole condition evaluates to false so we never echo number 17 So we don't have a number 17 over here. Okay, so they're not again. The not variable is a handy little one that you can throw in front of variables to check if there if they exist. If there not set to zero or not set the false. If if that's true, if they're not, if they do exist, they're not dizzy. When they're not set to false, then this is going to evaluate as false, but at if they are set or if they don't exist, they are set to zero. They are set to false. This will. This will evaluate as true. Okay, So, again, pulling from a database you can check to see maybe you put the name of somebody into a variable. Well, Maybe maybe they didn't enter their name right? Maybe they didn't enter their name when they filled out the form. So there's no there's no data in that field. So when you pulled from the database, it's empty. You can check that by using this, not operator. So if not first name, then you can do something to account for the fact that there's no first name in that field . So it's it. I use this aton so very, very handy little operator here. All right, so that'll do it for comparison. And logical operators hopefully found that helpful. We'll talk to you next time. 15. Read, Write and Append Files: Welcome back to PHP one. A one in this lesson, we're gonna get into manipulating files. So we're gonna be talking about reading files, writing files and depending files. Now, I want to get into the set up just a little bit here. So you understand what we're looking at your notes. I have two lines here right now that say the same thing. And essentially, what we have here is I have a read of this file that the very, very beginning, and then we're gonna manipulate it in different ways, and you'll see a read of this file at the very, very end. And so that's gonna allow us to do is kind of see some of the changes as we work through this Ah, that we're going to do with manipulating the file. Now I bring that up because it's an important point also to remember when working with files. So when working with files, there's what's called a pointer in terms of how these functions read the file. And so, as you work with the file that pointers going to move and eventually usually reached the end of the file And so you have to imagine like it's this cursor that's moving through the file reading the file. That's important to know, because a lot of times when people try to do is they try to use concurrent function so they'll do an F read like this, and then we'll maybe try and do this f get asked to get a single line. The problem with that is, once you do an f read and you specify that you want to read the whole file, that Pointer is gonna be at the end of its gonna move as it reads to the end of the file. So if you try to do a follow up function at that point, your follow up function is going to start from wherever that pointer is that unless you close and then reopen the file, okay, so that's important to note. You have to remember that there's always this pointer that's working through the file here . And if you want to run multiple functions on a file, you have to remember that that pointers moving and have some idea of where it's gonna be at when you run those follow functions okay, so we'll see some of that as we move through this. Um, but it's important to point out that that point exists. It's moving and a zoo work with this. You kind of start to get some idea of how that works. All right, so let's talk about our code here and what we're doing. So the first thing we're doing is we're opening and reading a file. So what we do is we specify our file name. So I'm doing that here so I can just use the variable throughout, so it's little bit easier to work with our actual file over here. My file dot txt. I'm gonna see it says Hey there, I'm a file and then it says, Cool story, bro. So has two lines. You'll notice when we read in here and out. Put it to the browser. It doesn't respect the line breaks here again. Something to note as you work with files. So what we're doing here is we're simply doing an F open so file open were specifying our file name. And then we're specifying the mode that we want to open it in Probably the most important thing that you'll want to remember and pay attention to when you're working with thousands of the mode that you open it in because that's gonna determine where the pointer starts out at what you're gonna be able to do it. That file on a number of other things. That's what primarily we're gonna be going through here. Okay, so I'm opening it with this are which stands for read. So I'm opening it and read only mode, Which means I won't be able to manipulate this file anyway when I open it and read only mode so f open found named the mode. And I'm just running this simple check for not unable to open a file. We just kind of we kill the script and out put this on the nipple open file so that we can see that. So I'm saving this than opened file as this. My file is kind of like a connection to that file in a sense, and so that allows us to then manipulate that file this file that we've opened in our code here to see that then, in order to read the file, if we want to read the whole file, it's really pretty straightforward. We do f read. We pass in the file name, and then we pass in how much of the file we want to read and that that's done by size. Now, chances are you're probably not gonna know what size this file is, and so this is Ah, handy little thing to remember this file size function, and we're running this on the file name appear. So what that's going to do that's just gonna look at the file and see how big it is. And then we're gonna pass That in is how much of it we want to read what that does. Essentially his tells it to just read the whole file. Okay, so most often that's probably what you're gonna want to do. So this is probably most often the way you're going to use it, okay? And so then this just does exactly what it says. It reads the entire file and we echo it as output over here. And so that's what we're getting right here. All right, so the next way that you can read a file, then is to get a single line from the file. And so what this effort get s function is going to do is just going to read the first line . So you knows, we have two lines here. We're just telling it to read just the very 1st 1 So and then we're passing in the of course, the name of our file that we've opened here. So what this is going to do once we get done sinking here? Ah, is it's going. It's going to read just that first line so we can go ahead and refresh this. You can see now I'm just getting that first line. So that's what get f get s does again, if that's something you need to do with your files. Next, we have this kind of boolean function called F E O F. Which is just checking four end of file. So that's what F E o. F stands for four and a file. So we're just checking to see if we're at the end of the file. So that could be handy for you to figure out where, you know, Have I read everything. I mean, at the end of file, you can loop through the file. It's something interesting to note. You can actually use a while loop and loop through the file And you can use this as your condition in your while loop so that while loop the conditional be true and it will keep looping until you get to the end of file. And then it'll stop so again something Ah, function that could come in handy in different, different things that you're doing. You'll notice that when I run this, I get blank because essentially returning zero Not true, because we're not at the end of the file, right? We didn't read the file, We just opened it so actually right at the beginning. All right, so that's another function to keep in mind. And then on this last one here is F get See? So what, this is going to do this, Just going to read a single character from the fall. So it's essentially gonna grab just the first character. And so you can see here we have this h just grabs the h from hey, right here. Right. So those air several different ways that you can read files And then, of course, we close it. We always close out the file toe to be done working with it. All right, so and actually, let me go ahead and uncommon this one back out here so we can get a sense of what's happening at the beginning. And then at the end, as we manipulated so that the next thing that you can do is you can write to a file, and this is probably the thing that you're gonna be one of doing this in a pending. So let's show you how to do that. So the first thing to keep in mind here is that when we do this, we're doing our F Open and we need to write it in write mode. So this w here that's gonna allow us to open the file so that we can write to it now. The one thing to keep in mind with the right mode w here is that it will delete what's already there. So the contents of the file will be when you open it in write mode, they'll be erased. And you the pointer will start then at the beginning, and you'll start writing from there. So you should really think of right as overwrite, because that's what it's going to dio. Okay, so we open it in write mode. We just set. Specify some text and then we use this F right function. We write to our file the text that we just specified. And you can do this multiple times throughout the file. So you see here I specify other text and I write it again. You can write as many times as you want to the file and it will essentially keep a pending it toe what you've written before until you close it. Okay, so if we take a look at this, then you can see that we've specified Well, this is happy news. It sure is. And you can see when we refresh the page at first. Before we did our manipulation, we had our old text right here. We did our manipulation, and now we read it. And now we have our new text. OK, And if we come over here to our file, you can see it says, Well, this is happy news. Assure it. So it's been changed this file, you notice the content that was there before has been deleted. And now this new content is there. And that's what f right is going to dio. So again, think of it as over right, all right. The next one that we want to look at then is to append and upend. Does exactly what it says it's going to do instead of overriding the contents, is there It's just gonna add content to the end, Okay. And so to specify that you use this A that opens the file on a pen mode. Now you'll notice that after that, once we've opened it in a pen mode, we specify our text and do f right, just like we did before. So the only difference between a pending and writing her overriding really is how you open the file. And that's because when you open it in a pen mode, the pointer gets moved to the end of the file. So then you start writing from there. So now you're essentially a pending content to it. So that's it's important to know that when you're working with it's kind of stuff because ah, it's gonna affect you know what it is, what it is that happens as a result of the stuff that you're doing. So we've just depended this file, Um, and you can see now we have our text appended here. We come over here to my file. You can see that we have our text appended. Um, I refreshed it twice, That's why. Added it twice. So little. Ah, goof up on my part. But you can see that what happens is that it depends that that text to the end of the file here. Okay, so that's what upend does. And we still have our original content, and then we just added some to it. All right, then we have some Maybe you know, some different ways that we can work with these files so we can also open in X with this X mode. And essentially, what this is going to do is it's on Lee gonna open the file if the file doesn't already exist. So ah, this should be seemed. If we refresh this, you're going to see, we get an unable to open the file right here. The reason we get that is because that file already exists, right? We already have it right here. So that's a good way when you're working with files, if you want to create a new file but on Lee do it. If the file doesn't already exist, then you can use this x mode to do that. This here is what I talked about before just are changes. I just put that in there so that you could see some of the changes that happened as we work with file. So this is really just a regular re like we've already covered. And then these air some. I'm not gonna go through these because they're really kind of repeats, but they're things for you to keep in mind that you can use. So if you use a R plus, then this is going to open a file for read and write, and it's gonna put the file pointer at the beginning of the file. Okay, So you can use our plus W plus is gonna open for read and write, and it erases the content of the file or creates a new file if it doesn't exist. And the file pointer is at the beginning of the file. A plus opens for read and write, and the existing data in the file is preserved. The file pointer starts at the end of the file, and it creates a new file if the file doesn't exist and then x plus it essentially creates a new file for read or write. It returns false on an air if it already exists. So thes Plus is essentially what they're doing is opening and read and write mode. That's kind of doing what those modes already do. But then open the file in both read and write mode s so that you can do what you need to do with it from there, right? So these air some other options that you have have available well, you should have noticed as we go through this is most of the manipulating, and working with files is pretty similar. What matters is the mode that you open it in, whether it's read mode because of the open and remote, you're not gonna be able to write to it or write mode. If you open in write mode is gonna override all the content there that's there or upend mode, it's not going over. I didn't put you at the end of the file so that you can add content to it. So the mode that you open it is probably the most important thing because then from there, that's going to determine what you can and can't do or what you do do how it effects the file as a whole. All right, so that is working with files, manipulating them, reading, writing and a pending and a number of other things. So hopefully you enjoy that and we'll talk to the next video. 16. Create Folders: Welcome back, Beach B one a one. This lesson we're getting gonna get into creating folders. So this is actually really pretty straightforward. So it would be pretty quick video. But there's a function that you need to know in PHP in order to do this. And we'll just walk through a couple of things that you wanna know when using this function . So the function is make during em que de ir this function right here. And that's what's gonna allow you to create folders. Really pretty straightforward. The only thing that you really need to know or pay attention to here is for whatever folder you want to create. You just put put the name of that folder in this this first parameter here, and it will create that folder now is going to create it in the directory wherever the script is placed. So, for example, I'll come over here to our shell. Here, you can see I'm in on my server and publication, Mel. And then I'm in a folder called PHP 101 Which is where I'm doing all the code for this PHP one on one course. And so I've list done a list here, and it's listing out, and you could see right now all there is this files in here. Matter of fact, I dragged this over here. You can see it's just these files right here. Okay? So when I create the folder is gonna create it in this director where the script is that that's one thing to keep in mind when you're working with this And then the other thing is that you have your permissions, that you're going to sit here for whatever your you want your folder to be. I think most of the time it's probably gonna be 7 55 like this. I think that's fairly standard anymore. You know, in the past, I have seen where you would do 777 based on different server configurations. But I think most commercial servers now, um, it was I'm going off memory here. But the reason that it was different is because there was it was the way in which the server was configured and certain with certain servers, um, PHP essentially wouldn't have the correct permissions or wasn't the right kind of user to be able to be able to create do things like this create files and folders. Um, without the permission set to 777 Anyway, I think most of the time you're probably going to set the permissions to 7 55 If you have some special case where you know that you have to send to something different, then so be it. But I would my advice or general rule of thumb is 7 55 Probably the way to go. Um, so when you do that, this will just create this this directory. So if I come over here to the page and I just refresh this page, then it's going to create this directory called Testing. So we should be able to come over here now and list out our files or what's in this peach people in one folder. And now you can see I have this testing folder here. If I drag this over, you can see I have testing in here now. So pretty straightforward in order to create folders with one other thing here is that you can actually do. You can build folders recursive lee. So let's say that you want to build a path to certain folders here. So you can actually just type out that path. What what you want? And if you put the recursive parameter to true, then make Dural go through and actually create all of these folders. Okay, so you can see down here we've passed in folder path with passenger for permissions Ought to be the permissions for each folder. And then we've said this third parameter here to true Soto recursive really create these folders. Now, one thing that I noticed when messing with this is if you put a slash here ah, it does it'll you'll get permissions issues. So I was just typed it in and ran it and I got permission just using thought that was weird . So removing the slash fix the permission issues. So make sure, Justo, if you're having permissions issues, just remove that slash. Or maybe in your case, on your server, maybe you have to add it, whatever the case may be. But that was essentially what fixed it on my end. All right, so if we run this, then then this is going to recursive Lee, then create all these folders. So now if we come over here and we list out our folders you'll see that we have this testing, too, and then I can go in and you can see we have next folder and then last folder and then that there's nothing. So it went through and it recursive Lee created all of those folders, so that could be handy again. If you're creating a path you don't have to create each individual folder, you could just pass in that path, and it'll recursive Lee created for you. All right, so that is creating folders with peach. Be pretty straight forward. Thanks for watching. We'll talk to you next time. 17. PHP Sessions: Welcome back to PHP 101 This lesson. We're gonna get into sessions so sessions in a way or a lot like post and get in that they're super global's that you can use to pass information from page to page. The nice thing about sessions is that you don't have to submit a form or pass the information in the URL. You can actually just set it explicitly in your code. Or you could use information from post or get to such set the sessions and then allow it to persist across multiple pages without having to resubmit that data or pass at him from at data in the post and the get ah parameter. So they're a little bit more persistent is the point than post and get. You can set them and they'll stay until you destroy them. So, um, it can be again. This is This is one of the ways speaking specifically of forms, you can create some really user friendly forms that allow data to persist across multiple if you have a multi page form and so forth. Okay, so a couple notes before we get into actual code here one the contents of the session. So, for example, you see down here we set this session name to John, and the status is dope. So the actual content here, what's in the the session? Super global that's stored on the server. It's not stored with the browser, which is what? What makes it different from a cookie, one of the fundamental things that makes it different from a cookie. Cookies are stored in the browser sessions air stored on the server, usually in a file in your temp folder. Okay, now you can determine that there's Ah configuration in PHP session not saved a path, and you can check to see how it's specifically set for your particular set up using PHP info. But again, usually it's a file in your temp folder where that data saved, and that's how it PHP retrieves it on each request. And the second part of that, then is each session is identified by Session I. D. And that session idea itself is actually stored on the browser, usually as a cookie, but also could be as a in the u. R l as a query parameter K. So the session I D is stored in the browser usually is a cookie, and then that is sent with each request so that PHP knows which session I d this is. PHP takes that. And then it's going to look in the temp folder for that session. I d is gonna grab the contents from that and make it available to your script here for you to use with every request. And so that's what allows it to persist across different pages because it's actually stored in a file on the server and retrieved with every single request. So again, very handy for passing information from page to page and allowing it to persist. All right, so to to start doing this actually three different pages here because we're going to show some of the different things that you can do with sessions. But the first thing that you have to do in order to get this all running is you have to start the session now. This needs to be done before you output anything to the browse or something before you out . Output any code here, you need to start your session, so that's really usually if you're using sessions, this will be the very very first thing that you do is just just start the session that will automatically create a session with a session I d It'll do all this stuff in the back end for saving a file on your temp folder on the contents and so forth. Okay, So once you've got that done sessions start here, which is a PHP function. Then it's really simple toe ad information to that session I d. So you can see that we have session here with the session variable. This is a super global. It's a race just like posts and get. And we can simply add information to it by creating a, uh, an array key and then value here. Okay. And so we've just set to elements within our sessions array right here. And these can be done once you've started the session. These can be done after you've output code and so forth. So if you look over here, we're printing our session before we set the variables. And then we're printing it after so you can see our ray appear is empty before and then after he said, the variables we have those variables now in are already here Okay, so that's how to start a session. That's how to set variables really pretty straight forward to do that. So next we're gonna go and click here and make sure they persist across pages. So this is gonna link us to our second page here and in the second page. All we're doing is we're printing out the session and they were echoing the session data. So you noticed We're not setting in here anywhere. So the only way that these air going to show up that we're gonna have any in data in these is if this information, the data that we stored in our session persists across pages, right? That's the only way it's gonna work. Now, you know, it's again. We have. We start our session. So you have to do that at the top of every page that you're gonna be working with sessions . OK, so that tells PHP to make the information available to this page. Right? So if we click on this page, you can see if we come over here in our print are we have our data were echoing out the data now here. So our data has persisted across pages and you know we didn't submit a form. It's not in the U R L anywhere you can see there's no data in the URL. It's just simply there. Okay, so hopefully that you start to see how how powerful this kind of thing can become. This is kind of one of the turning points when we start getting into building really highly functional applications is being able to work with sessions and cookies and so forth. Right. So from there, then once you've, you know, you've got your session said it's persist across page or echoing it out here and using that information. Then the last thing that you can do is you can destroy. You can actually do two things you can unset, or you can destroy the data in a session. So if we come to our third page here, we're going to click over here in a second. We always again have to start start our session, and then you can. You have two options, depending on what you want to do. So onset Well, it won't destroy the sessions, but it will unset all of the variables that you've set, so it kind of works just like the unset function in PHP. When you call it an Array. This is just specifically for sessions. So that's what session onset does. Session destroy will actually destroy the session. And if we went back to this first page, it would create a whole new session for us to start using. Okay, So if we click here to destroy the session and you'll notice that after we destroy, then and actually let's go ahead and let's do a print are before we destroy it just so you can see this. So we're starting our session. Then we're gonna print out the data that's in it will unset and destroy. And then we're going to pronounce the data then and see the before and after. Now, when you do this, you don't have toe unset and then destroy. I'm just showing you the two different options if you want to, just And if you want to get rid of the data and destroy the session, you could just call session destroy, and that will go ahead and do that for you. So keep that in mind. All right, so here, we're gonna then click to destroy the session. You'll see before we destroyed it. We still have the data there. So now has persisted from the first page to the second page to the third page. But then we destroyed it. And so now our arrays empty. We no longer have that in there. And so if we click here, this is going to take us back to our first page and you'll notice now again, no data in our session, variable to start. We then set our variables again, and then we print it out again, and now we have our data back in there. All right, so that is how you would kind of go about using sessions on some of the different ways that you can been manipulate them and again, very powerful tool. That turning point into being able to build applications where you have data persist across different pages. The example that I like that we're gonna get into more is creating, like, a multipart form where they can move back and forth between the different pages in the form , and the data that they've submitted is still there, right? So if they go back toe, get to page three and then go back to page one. They don't have to re enter everything from Peach one. It's still there because will have started in sessions and will then have echoed it out on that page. Okay, So, again, that's something we're going to get into. Ah, as we go here, but very, very powerful tool here. Um, sessions and cookies are somewhat of the basis of of applications. CMS is logging and registration type applications and so forth. All right, so thanks for watching. We'll talk to the next video. 18. PHP Cookies: Welcome back to PCP 101 This lesson We're gonna get into cookies now. Cookies air. Another way of persisting data across web pages similar to session. The main difference is where the actual contents in the different cookie or the session or stored. So with, as we talked about in the last lesson with a session, the actual contents air stored on the server, usually in a file and with cookies. The data is actually stored of the contents are actually stored with the browser. And we can see that distinction here. If we go into here in chrome, I'm going into settings. Ah, advanced settings, contents, settings, and then all cookies and site data. You can actually see all the cookies on your site here. And if I go to, let's see, just goto mind John Morris online. You could see their 17 cookies here, and we open this up. We can see all of the different cookie. So this is the name of the cookie. We click into this, then we can see the content right here. That content is stored for this is a cookie. That content is actually stored here with the browser now you'll notice that this is the PHP session I d cooking. So like we talked about with sessions, the session I D. Is actually stored as a cookie here and then sent with each request to the server and then the servers able to retrieve the file corresponding that session I D. And make the contents that were stored in that session available. And then PHP can use that. So this is how the kind of the sessions work the cookie to file on the server type connection with actual just cookies. Then the data is actually stored right here in the browser so you can see this one has this name, and here is the content of it, So it's just stored right here. Now the nice thing about cookies. The reason you'll notice a lot of log in systems and stuff use cookies as opposed to sessions. The reason that is and we'll talk a minute. How to do that is because cookies can persist across the browser being closed. You said an actual expiration time and the cookie will be there unless that expiration time passes or the user comes in and clears their cookies here, so That's why cookies air so often used because, ah, log in session can persist across closing the browser and reopening, which is something that a lot of systems like that want. Okay, So getting into that, actually using and setting cookies in order to set a cookie, we're just going to use this function set, cookie, and we're gonna pass in some simple parameters, like sessions start. You need to do this before you output anything to the browser. Otherwise, you're gonna get errors. So this will be one of the first things that you do in your script. If you're using cookies is to actually set the cookie before you output anything to the browser. All right, So the main parameters were and take a look at Here are the name, the value, the time and the domain. So the name is just the name of your cooking. Whatever you want it to be. You saw 17 of those names that could be whatever you want. This case, we're calling it user. Then the value is the actual value you wanna store. So for the session ideal PHP session I d cookie, It was the session I d for the SP lists one. It was the number of the number of the list gave us the list number. It could be whatever it is that you want to store in that cookie. This is where you put that isn't a cookie value. The time is the expiration, like we mentioned. So here we're setting it, too. Time now. Which is what this function pools plus essentially 30 days. Because 86 400 seconds is one day were times in that by 30. So 30 days. So this cookie will expire 30 days from now unless we destroy it beforehand. And then the domain is essentially the directory that this cookie is going to be available for. So if you want it to persist across the entire site and be available across the entire ah website, you would use just the slash like we're doing here. Probably the most common usage. But if you wanted it for a specific directory, you could do something like that, and then it would only be available for that particular directory. All right, so once you have those set, then you just simply pass those into set cookie and the cookie will be set, and that's really pretty is straightforward as that. All right, getting into retrieving a cookie than there's two ways that you can do this. First you can print out the cookie, so you'll notice here. I'm actually just printing out the cookie Super global as a whole. So this is going to show us all the cookies that we have here, which is why we see this PHP session. I d here because that's one of the keys that set. And then we have our user cookie here, the one we just set up here. And you can see it has our content of John Morris and there. So that's one way, probably not the way that you'll do it for production, but for testing. You can do that. You could obviously put the name of the cookie if you only wanted to get that one. He could do well like we're doing down here. Essentially, you could do it like this. And so that's the second way that you can retrieve Cookie information is again use our cookie kind of super global variable here and then pass in the name of the cookie that you want. Want to echo the value of So we passed in cookie name. So it's going to echo out the value of that cookie. So the value is John Morris. And that's why we get this John Morris right here, so pretty straightforward for retrieving and working with cookie information. Next, we will talk about modifying a cookie. So what if you want to modified the data that's in a cookie? And so to do that, what you do is you just essentially set the cookie again with the new information so he can see we're changing this one variable for, uh, cookie value to not John Morris. But then we're passing in the same name the same time, the same domain. The only thing that's changed. Here's this cookie value. So that's modifying a cookie, which is pretty straightforward. Ah, and simple to do. And then we're printing it out so that we can see that information. No. One thing that you want to keep in mind here is that when you're working with cookies, um, sometimes you'll get cached data back, so you may refresh and and you don't see anything change. Make sure you hit F five when you do this. Otherwise, you might get the cached, uh, data back. Um, and it can kind of confuse you, so but you'll notice. Here we've refreshes Page, And now we're getting this new variable that we've set here. So we've ascent. We've effectively and modified this cookie. All right, The next thing that you might want to do then is to delete a cookie. And so, in order to delete a cookie, the way to do that is pretty straightforward. You simply set the cookie. But when you said it, you said it to a time in the past. So let me go ahead, actually comment this out because that will reset our cookie. And it's good comment this out just so we can focus in on our delete cookie here. So again, you just said it to a time in the past. So we're using set cookie. The name of our cookies were not passing in any value because it doesn't matter cause we're about to destroy it and then were setting in our time. But we're setting it to a time in the past, so we're setting it too, like an hour ago. 3600 seconds. So we're basically manually expiring the cookie. That's really how you destroy it. Okay, so if we come back over here and we refresh this and we take a look, you'll notice that now the cookie in our array, our user cookie isn't gone because we've we've manually or proactively expired it. So the cookie will either go away when the time you set expires, or you come in and reset that time to time in the past and the server and PHP recognized that that cookie is now expired so it gets deleted from the browser, right? So, um, that's that's how to delete a cookie. Now, the last thing here I'm going to show you is one way you can check to see if cookies are enabled, and so we'll open this back up because this is where we set our cookie. But essentially, the way to see if cookies are enabled is to try and set a cookie like this, and then run the PHP function, count on cookie and see if it's greater than zero. So if there's anything set right, if there's any variable set in cookie, there's any that would mean that there's some cookies. Air set. There's something in that super global, so you're able to set cookies so and then you would echo cookies enabled. And if it's not, then you, ah, echo cookies not enabled. So that's a quick way. You can check to see if cookies are enable. Just run account on cookie like this. And if it's greater than zero, then thin cookies are enabled. All right, so that is working with cookies again. Very. This is one of those turning points into creating professional applications that actually do you know they're highly functional. So one of the things that you'll definitely want to mess with and make sure you learn all right, Thanks for watching. We'll talk to the next video. 19. Writing Custom PHP Functions: Welcome back, Beach P 101 This lesson. We're gonna be talking about how to create custom function. So we've spent a decent amount of time talking about some of the built in PHP functions that you can call and reference. But you can also create your own custom function so you can define ah, custom function. And when we talk about application building in a very general kind of simplistic sense application building is really the process of you writing custom functions. An application that you build will essentially be a collection of custom functions that you've written. So again, this is one of those things were kind of slowly maneuvering and making that pivot towards building PHP applications that actually do things. Okay, so the syntax, then for creating a custom function, is really pretty straightforward. You write the word function, so that tells PHP. What you're about to do is creating new function, and then you write the name of the function. You put these parentheses whether you have parameters or not, and then you put the's curly brackets. Okay, so if we just comment this out for a second, this is the syntax, this stuff right here that creates the function. So the word function, the name parentheses and then opening and closing curly brackets. That's how you create a custom function. And anything you do inside of here is what your That's the functionality of your function. So this function is pretty simple. All it does is echo out the phrase Hey, boss. And so then and once you've declared this function, then you can call it just like you would have regular PHP function. So this is calling that function now. And if you look over here, you'll see when we call this function eco stuff, we get our output of Hey, boss, it echoes that now, when it comes to the names here, there are different naming conventions that are out there. You'll notice here I'm using all lower case, and I'm using an underscore between words. This is I would call this WordPress syntax. So I work with WordPress all the time, and this is the the naming convention that they recommend and use four their function. So that's why I do this. It just helps to keep consistent for me. You will probably run a cost. Something like this, which is this camel case type function naming. So you can also do it that way if you prefer. Um, you'll see. Really? See that a lot outside of the WordPress community, people doing it that way. Whatever you prefer. I'm gonna stick with what I normally use here. Okay, so but at the end of the day, just do it. How you want to do it? Don't get too caught up in. Everybody will have their opinion on how it should be done. Just do it the way you want to do it and stick to it. Just be consistent in all of your code. All right? So that's how toe the basic syntax of creating a function. The next thing is, as you can, as you probably know, have parameter. So we've seen this with other PHP functions. Right? Was set cookie. We passed in certain parameters in order to set the cookie with the information we wanted. Well, when you create your function, you can also specify parameters that need to be set with you're function and so again, function the name of the function and then in the parentheses. Now you set what the parameters for parameters for this are going to be and it can be multiple. If you want to separate multiple ones here, you just put a comma and then the next parameter you can set us many as you want Here, Um and then when you have those set, you can use them in your code here, so notice we're referencing the function that is set as a parameter here in the actual function. So what that's going to do is if we look down here under my name for this when we're calling this function, we can now pass in the name that we want output. And so you'll see for each one of these John, Jean Jeff we calling the same function before passing into different prominent. We get John, Jane and Jeff. Now. One thing to note is that when you do this, these parameters become required. So if we call this function just like this where we don't actually pass in a parameter and you see, we have a parameter here, then I and here I have errors turned off. But that's going to LA times that will will cause an error. Okay, so you have to keep that in mind when you're working with us that if it has a parameter that's required and you try to to pass that in, then it's going to give you errors, all right, so we'll go ahead and get rid of that. That is adding arguments now you can also, for your arguments, you can have default values. So for here on this full name function receiver, setting a parameter first name and a parameter of lasting. But we're setting the VAT were setting a default for these values. So I'm setting this to equal to John in the last name equal to Morris. And then I'm echoing out first name, last name and then you'll notice down here. I'm not passing in any parameters here, so it's going to use these default. So John Morris and you'll be able to see. And actually this variable name is wrong here. Well, and save that so you'll be able to see that when this outputs is going to use the defaults that I have here. So you'll see it uses John Morris right here. Now if I pass in parameters. So if I do full name, let's do just do John and dough like this. So those parameters because I'm passing in parameters now, those air going toe override the defaults and you can see now for the 1st 1 I get because I'm not passing anything, I get John Morris still. But then I get John Doe for the 2nd 1 So is allows you to create functions that have defaults, but that can be overridden now. Not every function should have defaults, right? Sometimes you have to have something passed in, and so that's how you should leave it. But in some cases, they can have defaults. And so when you can put him in there when it makes sense than you put him in there but that's how you set them. And the defaults could be any sort of data type. It doesn't have to be a string. It could be a number. It could be an array. It could be all sorts of things. So that's how did you set the full arguments? The last thing then here is return values. So in all of these so far we've just been echoing out information. Ah, lot of the functions. You right, though, won't actually do that because they'll be what I call helper functions. So let's say you have kind of a main function that is doing all of the hard work of your application. It's kind of like the controller that's running the different parts, the application. We don't have to write all of your code in that one function. You can piece different parts of what that is doing out to other functions to keep your code cleaner, more module, more scalable, etcetera. And so when you do that, those smaller functions instead of echoing information, they're gonna run to return it back to the other function so you can call you can call functions from inside other functions. So when you do that, you want to return data again. We're going to get into all of that in more detail. I don't want to get you bogged down with that yet, but I want to give you some idea of where you might particular use this. So again you can return information instead of just always echoing it. And so here, what we're doing is we have this multiply function. We have a parameter called number and a parameter called multiplier. So we're just gonna multiply two numbers and so you can see we're doing number times multiplier. So it's just a simple multiplication function here, but we're returning the information here. So if I run, seem, multiply and let's just do two and 50. Our whole 45 would've worked 40. That's fine. Um, so we know this is going to equal to 80. But you noticed this one. I'm just calling the function. I'm not echoing it out. Okay, so if we refresh this, you'll notice that right after John Doe here, we're not getting the number. 40 were getting 66 which is this one. And that's because we're not echoing this in order to echo this, we, actually, because we're returning in our function, we now have to call Echo out here to get it to echo. So what that allows you to do is then let's say you just you want to run this function and you don't want to output anything yet. You just want to know what it is. So you can set the value equal to whatever this function returns. Ah, two times 50 I. And now you can have that value and use it somewhere else. on the rest of your code came. So that's what returning values allows you to do. You can see now that we've added echo to this multiply. Here we get the result of 88 and then we get for the next one. We get to times 33 we get 66 then this multiply one this big number times that big number gives us this value here. Okay, so that's what return values allow you to dio. They allow you to run some functionality, but just return the information so that you can use it later on. You don't have to just echo it right then and there. All right, so that is working with and writing custom functions. This is a big thing. This is one of the main things that you're going to be doing as you build PHP applications . So get some practice with this. Start writing your own functions and messing around with it. Ah, and get comfortable doing this. All right. Thanks for watching. We'll talk to in the next video 20. Scope: welcome back to PHP 101 This lesson we're gonna get into scope. So this is one of the things when you start working with functions Could maybe be a little bit frustrating and confusing, and so we're gonna talk about it. I'm gonna keep it down at this level. When you get into some more advanced stuff, there's other things related to scope that you will want to think about. But I want you to get the base kind of core concept. And so we're going to do that just using a simple function here. So the idea of scope really kind of breaks down into two types of scope that you can have at least a this level, which is global scope and local scope. And so this deals with really inside a function and outside of function. So when we open up a PHP tag like this and we're just writing something right here without going into any sort of function or class or anything like that, this is what we would call global scope. It's outside of any functions that we may have written it in just this kind of global either out here and So Ah, And then when we're inside a function here, this is really what we call local scope, which is again just inside the function itself. So the reason that you need to know this is that there's things that you can and can't access, depending on the scope. And so to show you that you see, I have this variable and this is called variable on global scope. It set too high. I am global yo. And then we have this variable inside this function called variable in local scope. So when working with variables like this, you'll see I'm echoing both of these out, the one in local scope and the one in global scope. But if we look over here, you'll see that the Onley one that's actually being output is the one that I've set in local scope inside of the function. So by default, if you don't do anything and I'll show you how toe manipulate this here in a minute. But if you just do it like this functions, you don't have access to variables in the global scope inside of a function. Okay, so I can't echo out this variable out here as this is now because I don't have access to it , and it works vice versa. So I've set this variable in local scope here, and you see, I'm calling the function and then I'm tryingto echo the variable in local scope here. And it's not I'm not getting a second echo out here. All right, so you can't variables that are set in the local scope you can't access inside of a function, which is local scope and variables that are set any local scope you can't access outside of those functions in a global scope. Okay, so that's why scope is important. You need to know where you're setting your variables and what you have access to. Now there are some ways to manipulate this. So if you want to access a variable that's been set in the global scope, then you could do what's called globalizing. So I can come in here and I can write this keyword global and then the variable that I want toe access from the global scope. Now what this is saying is telling PHP Look, there's a variable set in the global scope called This name I want Oh, that's the one that I want to access and use in my function here. So now if we refresh this and let me f five just to make sure here you can now see that we're getting the output in here Very born global scope because we've globalized it. So you have the option to then do that Ah, and and set the global scope so you can have access to it. You can also do it in reverse. So let's say I want to have access to this outside of my function so I can first globalize it. So now, even though it's not set in a global scope, what I'm doing is I'm telling PHP Hey, I'm you know, I'm gonna create this variable, and I want it to be available in the global scope out here. Okay, so if we refresh that, you'll see that now again, we have that Ah, that available outside. Okay. So you can use the global keyword to give you access to functions that are already set in the global scope that you want to use in your function or to make function, make variables that you create inside of a function available outside in the global scope. Okay, so now you wanna make sure that you're the the reasons and why you're doing that? No. You don't just want to always said everything in global scope. You should have some sort of reason to why you're doing it. So don't just globalize everything. But if you want to be able to access things in different scopes like this, the global keyword is kind of your your savior in in that respect. So again, getting back to the concept of scope outside of all the functions is considered global scope. Inside his consider local scope. By default, you don't have act access to variables that were set in a different scope than what you're in. So if you're in a function, you don't have access to variables in the global scope and vice versa. But you can use the global keyword to get access to variables alone in the global scope, inside a function, or to make variables you've set in your function available outside of your function. All right, so hopefully that gives you a good primer on scope. This is something to be again dealing with as you create applications. Um, you know, if that's still a little confusing. I would just maybe just rewinding and kind of going back through it and writing some code along with it and messing with it and trying different things and will start to become clear to you. All right. Thanks for watching. We'll talk to in the next video. 21. Constants: Welcome back to PHP 101 This lesson we're gonna get into Constance. So constants are a way of defining quote unquote variables that will kind of persist or be available. Ah, in all sorts of different scopes and so forth. A lot of times, what you do with Constance is you create a configuration file that you then include in all your other PHP files. And you can use those Constance than throughout your code. And this becomes handy if you have something. For example, a lot of times, what you see is Aziz database details, and so you can set those in the conflict file. And then you can use them throughout your functions without constantly having to globalize and so forth. And that's one of the big things about Constance is that they're available mawr really more than global scope. There they're available. All scope, right, so there will be available in the global space. They'll be available in the local scope. Um, and so they're easy to use in lots of different places. Now, you don't want to get crazy with constants and just define everything as a constant because there's a lot of cases where you just want a variable to be local, but things that you're going to use ah, lot throughout your application and no a number of different places. You don't wanna have to constantly globalize that you can set those as constants. Right? So to set or create a constant you use the PHP function called define, so that's gonna allow you to define a constant and the first parameter Here is the name of the constant, whatever you want it to be and how you want to reference it. The second, then, is the value of that constant and what you're seeing here. This first definition is what's called a case sensitive definition. So when you set this constant like this and it's kind of standard to capitalize constants, that's kind of something that PHP developers tend to do. When you do that and you set it in this way, then it'll be case sensitive when you call it. So you have to use the exact case that you used here For each each character, the 2nd 1 is a way of defining constant that makes a case insensitive. So you notice what you do. Just add a parameter 1/3 primer called True, and that will make the constant case insensitive. Now I can tell you from my experience, I've pretty much never done that in 10 11 years. I've never had a reason, really. To set a constant is case insensitive? Maybe it exists out there, but it's just never been something that I personally have have done. But that's how you do it, in case you run into a situation where let's something you need to do so this last constant here will actually be able to be called in lower case or any sort of case. It doesn't matter, all right, so that's how you define a constant just given a name and a value and then for referencing them. Then you just echo out or print or or call whatever that constant the name of the constant so name, name, and you'll see. When we do that, it goes out the value of that constant. Then you'll see here, this one, even though he said it is all upper case here. We're calling it all lower case here. We still get it because we sent this one is case insensitive and you'll notice that these air in the global scopes of the constants are available in the global scope. Then, if we jump inside a function, you'll notice we're not globalizing or anything like that. And we're calling name dot Last, you'll notice. Even here we were mixing case with this and then we're calling that function and we're getting the output here. So as you can see, constants are available again in both the global and the local scope. So that's what makes them handy. You don't have to constantly be globalizing and so forth. You can use them throughout your different functions and so forth, and they're easy to define. You see that? I mean, that's a really simple function called there to make them happen. So that is Constance. Thanks for watching We'll talk to in the next video. 22. cURL: Welcome back to PHP 101 This lesson we're getting be getting into using curl on this one of those things that makes you start to feel like kind of, ah, riel developer but also is a handy tool for allowing you to be able to access data across the Web on different websites and so forth without on being able to set options and so forth things like file, get contents and some other methods are generally pretty pretty limited. In the kind of things that you could do. Curl opens up a whole new world. So in this lesson, we're gonna be talking, going through a basic example of using curl kind of the the minimum you need in order to be able to use it. Then we're gonna be going through ah, post example, which is a common thing where you wanna post data to Ah, you are l and get some response back. And then we're gonna be going through an https example and talking about some Gotsche is there as well, because it's a common thing you might need to do. All right, So to start off, let's start with a basic curl example So first, what is Kerr will curl allows you essentially to send requests. You can specify u R l that you want to send requests to and it will. It will capture the response from that that you are l and it will bring. It'll fetch and bring it back to you in html form and allow you Teoh. Then be able to work with it. So in our basic example here we can kind of walk through and see how this works. So the first thing that you need to news you need to initiate initialize curl so that all we're doing is calling this occur. Curl in it function here and we're setting it to the variable ch Let's. That's a fairly common variable used with curl requests. Generally stands for curl handle. So we're setting that variable Curlin ish this This gives us, ah, basically access to be able to set options and get our response and so forth using this handle. Right, So from there, the second thing we need to do is we need to set our options. So the first option that you'll always want essentially is the U R l that you're gonna be sending the request to Okay, so that's pretty much always going to need to be there to do that. We're gonna use this PHP function, curl, underscore, set, opt, And that's gonna allow us to set an option. We're going to specify the handle that we just created appear that we want to set options for now. This comes in handy when you get you can get into some or really complex things where you do multi channel requests so you could actually initialize 234 different channels up here, set options for different ones and run those requests simultaneously so you can get in tow that kind of crazy stuff. But, ah, we want to specify what handle here were setting this option for. So the handle next, the option that were setting Now, these options. You know, there's a ton of these and actually have over here. If we look at you can just kind of Google curl, set, opt. And if you scroll down, you'll see a whole list of the different options that you can set. So there's just a ton of these in here and you'll notice over to the right. Then it tells you what they are and what happens if you said it toe what? And so forth. So obviously, I'm not going to go through all of these. You can use this as a reference documentation when you need to. There's something specific you want to be able to do with a curl request a certain option that you want to be able to set. You can come here and check that out. Right. So here we're just setting the option of the U. R L. This is the girl we want to send a request to, and then we're just specifying the girl so pretty straightforward. The next option is one that will probably most likely be one that you use. So this is called Curl Opt Return transfer. And we're setting that too. True. And what this does is this will return whatever comes back in the response from the U. R l in the quest that we send If we if we don't set this than what curl do, is just automatically output it so often times, you don't want that. What you want to do is you want to capture that request and then you want to do something with it. You want to work with it in some way. So that's what this does. It will return that that response to whatever the variable set the four curl, execute down air curl execs. So we're setting output equal to curl exact Down here. What this does is tells Curl to to return it instead of out putting it to this variable. All right, next is Curl, opt, header. So this is whether to include the whole header in the output. So the whole actual response header which has a whole bunch of information there depending on whether or not you need access to that, is when you would would set the so I showed this one here just because it's one that could kind of go either way. Some request you may need the response header and some requests you might not. So this is how you kind of specify whether you want it or not. So just this curl, opt, underscore header Here. We haven't set to false in my experience, I would say most the time I have this set to false, But there have been a few exceptions where I need access to the response there. Right? So from there, then we just execute the request. We fetched the response and we check for errors. So as I mentioned earlier, we're setting this variable output equal to this curl exact function. And we're passing in our handle. So up to this point, we've initialized curl, we've set some options and here's where we actually execute the request. And because we have curl opt return transfer set to true than whatever comes back from the server, the U R L that we specified is going to be set as a string for this output variable. So then, from there we just checked to see is out foot output. Was it false? Now, one thing toe to know here is if the U. R L you specify is not found, you're actually going to get the not found page of that website back. Okay, so that's if you're getting that back. That means that the whatever you are l your specifying is not found. It could be a little confusing because especially if you're doing like we're doing here, which is printing the output, you'll see a not found page. That's what little were the page that loads will look like And you thinking, Wait a second. I was been working with this page, Wise it off, said not found. What it's actually doing is fetching the URL. It's the wrong URL. So it's grabbing the 404 the not found page for that, returning it and then out putting it to your browser So it makes it look like your page is getting the 44 when it's actually the one you're requesting over here. Just a little got you to pay attention to their, but we'll do a simple check. Just check to see if output is false. If there was, ah, some sort of issue with the request, if there was, then we'll just gonna echo the air so that we can deal with it from there. We're just gonna close and free up the curl handle, so if we need to use it again, it's now free and we can use it. And then we're printing out our output here. So if you notice we just sent are the URL we use was google dot com. So if you look over here in our printing, our output, this is our page. We're actually getting the full kind of google dot com page here Now. Obviously, some of the links to images and so forth don't work because the the girls kind of get messed up. But you can see this is this is pretty much the whole page, that html page that you would get if you went over to Google. So that's the kind of thing that Curl allows you to do now again, you can you can go to whole Web pages like this, or what's often more common is you can send the request to some sort of form that requires post data or a search form that requires get Dad and will return in actual array, uh, format, something you can work with with with PHP. Or you might send it to an XML file and get XML back that you can then work with inside a PHP or Jason. You might send it to something that returns to Jason, and you could work with that. So here I'm just showing a base example where it will actually go and grab the whole Web page, if that's what you tell it to dio, right? So that's what Curl does. That's a basic example of using it now, likely one of the things. As soon as you start messing with curl in your mind that you want to do is you want Oh, wait a second. I can use this to submit data toe post data. So we're gonna walk through an example of that. So the first step here, I just did some basic setups of first thing is, I'm specifying the URL that I want this to, ah, to send this to so the URL that we're gonna set down here in this option so that I want to send the request to Now I've made up a page to to show us this output so that we can we can look at it This page, All this does is print out it just print out, prints out whatever is in the post array. So it's just doing a print are on the pros post super global. So we're going to submit some data using the post method, and you're going to see that what we're gonna get back is the printed out data that this this this script does so that I will show you that you can query a form or a page that will process post data, and you can actually get a response back from it and then do something with that response. Okay, so here's the euro. Now, one thing for no. If you download the source code, I will be taking this. This you Earl is on my dev server, and I'm gonna be taking this file down. You're going to get this output file included with your source code, but you need to upload it to your own server and then specify the girl to your server here because I will be taking this down. So if you leave this u r l like this, you're gonna get ah, Page not found, cause I'm gonna take it down. I ain't so something just to note there. All right, so next thing or setting is our post data. This is the data that we're actually going to send Ah, via curl fee of the post method. Okay, so I just made some stuff up. None of this is required. It all depends on the pagers submitting it to and what it asked for. Then we get back into what we've kind of already done. So next we initialize curl where we set up our curl handle. Next, we're going to set some options. So this is the girl that we're going to submit to which we specified appear online 35 and then we're turning curl the return transfer option to true. We're setting now the some new stuff here. So next we're going to set an option that says we're doing a post. So we're doing a post request. So we gotta tell Curl that So this option curl, opt, underscore Post, and we're set that to true. And then we need to tell it. What What's the data were submitting order. The fields were submitting. What way created that up here in this post data array? So now we set the option curl, opt, underscore post fields and we pass in our post at array here very, very straightforward. When you first look at curl and you see all this stuff, it could be a little bit confusing. But once you get into it, it's like most code. It's really just pretty straightforward. You just got to know which options to use and so forth, right So we're passing in our post out of here, and then we just continue like we did before. We're gonna execute the request and fetch response and check for errors. So output equals curl execs, and we're getting our response as a string through this output variable set to that checking the seas out output is set to false. So were at echoing the error. And then we're closing curl, and we're gonna print our output here and let me go back up here and actually comment this one out. All right, so we're going to come down here, and we're gonna print the output of whatever this page outputs to us. Now, remember, this is just a print our that we're doing on this page. So the data that we submit here is just gonna print it out and send it back to us basically in the crest. So when we rebuild this page once, you once you syncs up here than what we should get back is the same array that we submitted . All right, so let's go ahead and refresh it and you can see that's exactly what we get. We get this array of the exact of data that we submitted Now, depending what you're submitting that to, you might be submitting it to some sort of form that doesn't just print it back. It actually processes. It gives you some sort of result, maybe a search form or whatever. It could be anything so you can get that information back. And then you could do something with it. Or it may be something that creates a Jason response or plenty of different options out there, depending on what you want. A query. But this is how to do it in curl. All right, last example, then, And let's go ahead and comment that one out, we'll go ahead and uncommon, this one. So by the time we get there, it will be ready for us. All right. So the last one that is using curls https. Now, when we go through this, you're gonna see everything's the same except for the girl that you pass. And I'm gonna talk about a gotcha. And why I'm going through this. So were initializing curl just like we did. We're setting. Our options were specifying, Are you Earl returned? Transferring header to setting into zero. This is just like the basic example. Our output, it said to curl exact. We're checking for errors, were closing curl and we're printing our output. So it's just like the basic example. The Onley difference you'll notice is that we're sending it two and h t T P s. You are l. And so when you do that, you can run into some issues. If your server isn't properly configured to handle that, then it could wind up that you get some sort of air. Now, if you go look looking on the Web to resolve that air, what you're most often going to find is some someone telling you to use curl, opt SSL, verify Pierre and setting it toe false. What that essentially does is it disables. The SSL checks came so that can be risky because it can allow for man in the middle attacks . So there are you have to think this through a little bit. So let's say, for example, you're actually Google or you're actually clearing google dot com and your your querying or sending request to slash search, and you just want to get the response back from the search. And so you're gonna you're gonna pass in this search query, and you just want to get Google's response back from that. In that case, if there's a man in the middle attack and someone snatches that information, someone's listening in on your life on the line of whoever submitting the data and they get it. It really doesn't matter because it's a Google search. Anybody can go do that exact same Google search, so chances are it's not a big deal. Okay, so in that case, just using this SSL verify, Pierre would probably be just fine. Now, on the flip side, if the https site that your sending the request to is, say, a bank, no, Obviously, that's not something you're gonna be able to do here. But let's just say it's something sensitive information like ah, bank and you're getting private information will. Now someone using an a man in the middle attack in getting ahold of that information eyes a big, big problem. And so, in that case, using this SSL verify, Pierre would be a big, big issue, something you absolutely would not want to dio. So there's a way around this, and essentially the way around this is to have the proper certificates installed and so in , referenced on your server. Now, that's beyond the scope of what we're talking about here because we're really keeping focus on the PHP. And I don't want this to turn into a three hour thing, but I've included the girl that has instructions in the source code. So you can You can use that to get access these instructions, but I'll click over here and will basically take a look at him. So this is a comment. This is This is also often referenced. Someone will come in and say Use SSL, verify Pierre, and then someone will come in and references comments A No don't. So I want to give you both sides when and when not to. But I also want to show you how to point you to where to do this. So basically the idea here, as if your PHP install doesn't have an up to date. Ah, certificate authority, root certificate bundle. Okay, so I know that sounds a little bit crazy, but if it doesn't have the proper certificate bundle installed for you to be able to do these kinds of h T T P s requests. Then you can download the one of the curl website and save it on your server and then in your PHP, any file reference were the path to that certificate bundle. So that's essentially going to, as I understand is going to tell curl the certificates that it should trust. So when you make this https request, it's going to check because SSL verify appears going to verify that their certificate is valid, and it's going to use this this file right here to know which certificates it should. Um, it should allow essentially right, So that's the way to handle this the proper way. If you're actually doing passing of sensitive information, you don't want to turn SSL, verify, verify. Appear to false. You want to leave the verification in place and then have the proper certificate bundle installed on your server and referenced so that the checks can be done properly and you can make sure you have a proper SSL connection. Okay, so that's the way to handle https requests. I'll tell you, in my experience, most of the curl stuff that I do doesn't involve sensitive information for me personally and so normally turning us to sell, Verify appear to false is just fine because I'm quitting something that is a CPS, but it's it's data that its Google data are. There's something that anybody can riff. There's no need to get all crazy with it. If they had had just a regular Http, I would have just done it that way. But for some reason, maybe that site doesn't or whatever. Okay, so want to make sure that that's clear. So with all that said, that is the down and dirty on running curry request. There are other things that you can do with Curl, obviously, but I want to invite you to dig into it yourself. I don't want to get into a three hour tutorial doing curl, but after this you should have an idea of how it works and and be able to do the things that you want to do inside of Curl. All right, Thanks for watching. We'll talk to the next video 23. File Get Contents: Welcome back, Beach P 101 This lesson. We're gonna get into file, get contents. The reason I wanted to do a tutorial or video on this is because you may be and it does a very similar thing to curl. It's gonna allow you to query or to make a request to an external UL and get some sort of response back from it. And I'm gonna show you how you can do it where you can pass data post or get method, etcetera, and be able to get some sort of response. So it doesn't very, very similar thing to curl. But you may be in a situation where curls not installed on your server, you're not able to install it. And so you need to do this without being able to use Curl. So we're gonna show you how to do that. Right? So the first thing that we have up here starting with Line six is we have some set up. And so the first thing that we're specifying Here's just the U R L that we want to send the request to now you'll notice here I'm using the output PHP file that we had used for the curl tutorial. The curl lesson and the reason I'm doing that is to show you that the two ultimately the response you gonna get back from both of these different ways of doing it. Curl versus file, get content. Content is really the same. It's just a different way of getting there, right, So that's the girl. We specify that here we have, we're creating in a data array here. I'm just passing in some silly data, and there's nothing special about this. This is just the data that we ultimately want to pass through the request to the URL. So if you'll remember from the curl lesson that our output dot PHP file just all it does is print prints out anything that's post any data that's posted to it. So when we send this, what we should get back as we should just get a print out of print are of the data that we sent over. Okay, so we'll check that when we get there. Right Next is really the bulk of this, and this is creating an options ray. And so the way to think about this and there's there's some finer detail in this. And when you get really, really super advanced, you can maybe come back to this. But it's really beyond the scope of what we're covering here. So the way to think about this is this is really we're setting up the data that we want to send to the You are on how we want to send it so much like when you're creating a form and you're going to specify the form type. You know, you can do that, especially if you're uploading files. You have a multipart form than you need to specifically, ah specify the kind of form that it is, but we're also going to specify the method and then the data that we're submitting. And so it's really, really similar to as if you were submitting a form, but we're just doing it all in code. We don't have a form that's being submitted, Um, when we're doing it programmatically here. So it's really again. It's sending data using a Met, a particular method. So when we look at this array, we have this option jeweler array and then inside of it. We have this http element, and that's also Narain that that's what really has ultimately our options. No, The reason that is is because there's other things other ways that you can make requests beyond just h T T P. So you have that option of doing that? But http requests are probably gonna be the most common thing that you do. So that's what we're gonna cover here. Right? So we're sending an A C p T. P. Request. We're setting our header to you know, this for mural encoded. This is really just the standard way of doing it. If you're doing something with files and wanting to send files and so forth, then you'll need to do the multipart form just like you would when you when you did any any form that involves files. You have to do a multipart form. Okay, so, um, this is really just the standard kind of mime type that you would use here are Method is post, just like you would in usually do in some sort of form. And then this content is the actual data that we're going to send through the request and so were built. Were using this function called H T T B h T t p build query, and we're passing in our data, Ray, what essentially that's going to do is create a series of key value pairs, much like you would see in the u. R l parameter and essentially format of how we need it so that we can send it in our request here. Okay, so this all of this is really pretty straight for in fact, most of the time for these requests, this will actually stay the same. What's gonna change is your data array up here and you're you are l Now, again, you may have the header here. Change if you're doing, um ah, multipart form of messing of files and so forth. But for the most part, this is probably all going to stay the same. It's actually just this stuff up here that's going to change the u arial you're submitting to and the data that you're submitting, right? Um, one note for no for debugging purposes, you can set this option called Ignore airs, and you can set it to true. And if you do that, then what will happen is when the response comes back, the PHP will ignore any of the errors that were in the response. Normally, if there's some sort of error, it will just return False. And so you don't have much more information about why it was false. So if you needed a bug that you can turn on ignore airs said it to true. And you'll actually get the full http response back that you can print out and look at and see what actually happened and get some idea of why that that particular request came back false. Okay, so just a side note there. Obviously, when you get in production, you'll probably want toe turn that back off. So you don't have any weirdness popping up in you were output. All right, The next line here is we're gonna create the stream context. Now, this could get a little bit confusing. When you start talking about streams and you start talking about contexts, the way I like to think about it is a stream is really when you're passing data back and forth, okay? And when you're doing that, you want to create a context for that stream. So what that means is you you're going to pass data to the girl that we specified, and you're going to get data back from it and you're gonna have some. You're gonna have some parameters or some options for what you're sending and what you want back, and that's everything that we did up here. So part of the context that we're sending or we're using for this stream is that we're going to use the post method. Part of the context is the data that were submitting via the post method to the URL. Okay, so that's when we're talking about stream context. In a basic sense, that's what we're talking about. So what this function does is that basically takes our options array appear and it creates the stream context that we need. It creates what we need to then pass into file, get contents. At this point, there's not really too much to be honest with you. I want years and years and years without ever really knowing oh are trying to figure out what stream contexts were. Um, it's not something that when you're first learning PHP that you really need to get into much as you get more advanced, there may be some things that come up and then you can dive into it. But just know that this essentially gives us turns our options array into what we need to pass into file get contents here so that we can actually process the request here. And so then that's the next thing that we do is we actually process the request. So we're passing in to file get contents the u R L that we want to send the request to Ah, this false is for include path. So that's an option. When it comes to file, get contents. It has to do with with whether or not you want to use the include path or not. And then we're passing in our context here, which we just created here, which is essentially passing in all of our options so that we can then query this your send the request to this u R l using the post methods, sending the data that we want to send an and get a response back. Right? So we'll get a response back from that, and then we'll check to see if it's if it's false. If it's false, then we're gonna, you know, handle those errors somehow. So you may have some way of handling different errors that you might get back, Um, depending on what they are and what what you're trying to do. So you'd have handle those here and then here. I'm just printing out the result. Okay, so we're just printing out what we got back. So now, if you remember our output that PHP file, all it does is whatever data is posted to it, it just prints it out. So you can see here. We we've sent our request, and we've sent in this data here and you can see what we're getting back here. Is this data printed out? So this request is processing properly. So what you can see here is that now, because we can post data to a particular your l and we can specify what data we want to send. Its much like what we talked about with curl, where we can then go out and we can query different different girls and so forth and past data and get information from them and so forth. And we can do it all without having to have curl actually installed here, so built in right into PHP with file, get contents now you may be asking Should I use curl? Should I use file? Get contents? More often than not, I use Curl. Um uh, from my my kind of perspective curl seems that does seem to be a little bit more flexible. Does seem toe have some more options that make it a little bit easier? Ah, toe work with. And so especially when it comes to air handling and so forth. Um and so I I generally use curl, but I know people who love using file get contents. Um, and so if you want to go that route, that that's probably absolutely fine. So, um, me, personally, I just tend to use curl more than I do file. Get contents often what you'll find if you look at ah request classes, for example. WordPress has its own class for handling each T T P requests like this. Usually what you'll find is they try to use curl first. If curl isn't there than it'll use a backup of file get contents. And then there may be some other stuff that they try to do in order to process the request . Usually, that's what I see when I look at, um, any sort of class that that is designed to manage. Http requests it will try to use curls. The canton. They'll use file, get contents. All right, so that'll do it. That's how to send requests. If you don't have access to curl, be able to pass that and do all the things that you want to do. Thanks for watching We'll talk to in the next video. 24. How to Upload Files In PHP: Welcome back, PHP 101 This lesson. We're gonna get into uploading files, so we're gonna go through the basics of how to do it. And then we're gonna talk about some of the security issues that you want to pay attention to and some of the ways that you can deal with them. I would preface this by saying that much of this will consider much of this an art because there's lots of different, especially when it comes to the security part of it. There's lots of different ways that you could do this. And if you saw five different classes that did this, they likely do it five different ways. So we're gonna go through just some of the basics of this and show you the things that to think about. But there's something that you're gonna add to one of your applications. This will definitely be one of the things that's always an ongoing kind of thing that you're maybe always filling. Let's a little bit Teoh always just make it a little more secure here and there. All right. So that other way first I want to show you a couple things before we get into the code. First off, I have this folder here in my tutorial folder for PHP one a one and I havent upload folder here. Now you'll see over freshness so you can see this folder is empty right now. So we're actually upload a file into it. So I want to show you to start off that it's empty. Second thing I want to show you is before we do this one of the things if you're having issue, try to do this and you have some sort of issue file uploads, maybe disabled. So the way that you can check that is you can create a PHP info file. So you just created a PHP file. Throw in the function PHP info and then load that page and it'll give you a printout like this of everything. All of how Peach piece configured what's enabled disabled etcetera, and you could do a search for file underscore uploads, and it'll tell you if it's on or not, so you can see in my case it's on. And so we're good to go for file uploads. It fears is off than you. You can enable it in your PHP dot any file. So the just just check for that if you're running into any issues in terms of the file uploads, all right, so that stuff out of the way that we can get into actually uploading foul. So I'm gonna go ahead and show you this Working for some, Just going to click choose file. I'm going to select the screenshot dot Pete PNG. So the PNG files there a file that's allowed by this script. So we'll go ahead, hit open here. You can see screenshot up PHP or that P and G loads up here. I'm gonna hit upload, so you can see we get a response here that says your file was uploaded. And so if we come over here and bring our uploads folder back over here, you can see we now have screenshot dot PNG in our folder here. So we have our file upload working, and that's really the basics of it. That's that's how it works. All right, so what do we got going on here? So, first off, the first thing that I wanna show you is in doing this most of the code that I've should you thus far is procedural, meaning it's not inside of a function. It's on site of a class. Um, and I've done that just to kind of keep things really simple, because I really want to focus on the syntax. And I want to focus on the concepts. I don't want to get to two confused by throwing this into a bunch of functions and class methods and so forth in this particular click case, I think it's important to show you this inside of a function, because, in my opinion, it's just much more efficient to do it this way. Inside of a function. It's actually much harder toe, in my opinion, in my experience toe to do it outside of a function. And the reason why is because when we do all of these checks here, you'll notice that, if any, any point, all of these, if there's some something goes wrong, I'm just returning. I'm returning out of the function. Well, you can't do that if you're not inside of a function. No, you could do things like exiting and dying and so forth. But that's not very Ah, it's not a very good user experience. So the ability to return like this and stop processing that point inside of a function is an easy way to make sure we can do all these checks and still give us some way of displaying a friendly user message. All right, so this is inside of a function you can see down here. We have, ah, files super global. So when you upload files, it's just like a post. When you submit a form and you have post here, you have a file super global that you'll have access to. That's gonna have the what's in what has been uploaded and the information about it. So all of your dad is gonna be in this files. Ah, super global. So we're checking to see if files is empty or not. If it's not empty, then we are running our upload file function, which is where everything is done here. And we're just echoing it out because since we're returning these statements here, then we're returning. Whatever. Echoing whatever's returns, we could display their message. So that's why you see here that your file was uploaded. That's what you can see here. If we get through everything and it's successful, that's what we get all right down at the bottom. Here we have a really a fairly standard form. There are a couple really one thing to pay attention to with this, So our form action is blank. So that means it's going to re submit this when this for miss a blow buttons click that's going to submit this to this same page, which is where we have our processing script. If you wanted to put the script in a different file, you just have to specify the location of that file here. We're using the post method, and then here this is the new thing. We have this enclosure type E N C type and multipart slash form data for file upload. That has to be that. So you have to make sure and put this line in here. If you don't put this line in here, then it's not going to to work. The file upload won't work, so make sure you put that in there from there. It's really just a simple form. So we have an input, the type this file. And so that's what makes this choose file. Things show up here. The name of it is file, and then we have a standard submit button. So again, really, fairly standard form the only two things pain, tension to again or this enclosure type and then setting the input to a type of file so that we know that we were gonna upload a file here. All right, so with all that said, what happens when we submit this form is we'll check to see if files is empty or not. If it's not empty, then we're gonna process it using this upload file function here. And so that's what we're going to spend most of our time going through her. All right, So to start off inside of this function, first off, you'll note that the files and posts and getting all these are Super Global's. So they're gonna be available inside of these functions. We don't have to globalize them. We don't have toe do any. You don't pass him in there, just available, and we can use them. So the first thing that we're gonna do is we're gonna set this file's temp name. Now, the way to understand this is really file Uploads happen in two parts when you click the upload, but in. The first thing that happens is that the file is uploaded to a temporary folder on the server. Okay, And that's what this temp name essentially is. This is essentially giving us where that temporary file is. And so the reason they do that is the upload to the temp folder so that you can then do some checking and processing and and looking at that file before you permanently store it. So the soon as the buttons click the files uploaded to a temporary folder and we're gonna do some checking on it, and that's the first step. And then the second step is of all our checks go through, then we're gonna move it into put permanent storage. All right, So, again, this just gives us the name of that temporary file that's in the temporary folder. Okay, here we have our target directory. So this is where ultimately are permanent. Storage is going to be Remember, this is our uploads folder here. So we just putting it in uploads right here. Then we have our target file. So this is where we're ultimately going to store this file permanently. So we have our target directory. So we have up loads. That's where it's going to go and then based name and then files, file and name. Okay, so when you first start working with files, that could be a little bit confusing, cause you have name, and then you have temp name. So again, the name is what What? The name of the file was when it was uploaded. So in this case, screenshot dot ph PNG. The temp name is where it's been stored temporarily. Okay, so you have you have the actual file in the and then you have where it was stored. Okay, so in our permanent storage, we want to save it as the actual name that was uploaded again. Screenshot dot PNG. So that's why the target file is set to name not tempting. Next, we have our max file size. And so this is five million bits, or bytes, which is five megabytes. You can, of course, set this according for whatever you prefer. But that's what I've said here. Allowed file types. So we're gonna do some file type checking they were doing allowed file types are also doing allowed image types, and you have to check them. Ah, two different ways. So we're going to We set thes separately. So we have allowed file types of having an array, and then the only one that we're allowing is this. Pdf So we're allowing pdf uploads. Nothing else. No zips know Jason or anything like that. Here we have allowed image types in an array and the image types. These are essentially constants. Or, you know, these air image type Constance. And so we have Jif. We have J peg me a PNG, those air, the image types that were going to allow to be uploaded. All right, so that's our set up. That gives us kind of everything we need. So we don't have toe right a bunch of code time and time again as we go through this. So the first thing we're gonna do is we're gonna check if it's an image. And if it's one of the image types allowed, right? So what? The way we're going to do that is we're gonna use get image size and what get image size is going to do. It's obviously ah, see what size the file is, but it's also going to is gonna return some information about the file and particularly what we're interested in is is going to give us what image type it is. No, it's important that you do it this way because if you look at files, it'll tell you what it thinks the the file type is, or the mime type of the file that's being uploaded is, but that can be spoofed by hackers. So it's important that you actually explicitly check it with get image size. Okay, so we running, get image size on temp name. So this is the temp file in our temp folder. That's what we're checking. It's sitting there before remove it to permanent storage. So we're running this on it, and saving that is image check and then the the when this image check what you get back as an array of data about the image will the first, the first element at the zero index in the the element at the one index. They're going to be the height than with okay, but the one at the The two index is going to be the what type it is what mime type it is. So that's what we're going to use to check it against what we've allowed. So we're running in array on that, passing in what we got back from. Get image size in the two index and running that against allowed image types. So this is going to check and see if it's an allowed image type. Now, if it's not, if it's not one of these allowed to image types, then we're not quite done because we need toe were also allowing Pdf uploads. Or you could allow zip file uploads or whatever, Jason. File uploads whatever you want. You can add that here however you like, but we need to run that check and see if it's a file type that we've allowed. So to do that, we're going to use this exact function, which essentially allows us kind of like I don't want oversimplified too much, but it's kind of like if you were in the terminal typing, it allows us to execute things on the server. In this particular case, what were basically we're gonna be doing essentially a Byeon Eri check or looking for binary markers in the file that tell us what type of file it is, so is probably not too important, important to get to in depth on this right now. But just know that this is gonna essentially tell you what what mime type it is. And so, you see, we're passing in temp name and then this file check in when you're up. Use that in this exact function like this, it's going to the output of this being run is going to be passed into this file check. Okay, so it's going to check this temp name to see what mime type it is essentially, and it's gonna rip return whatever mime type it is into this file, check variable here so that we can then use it. And this is actually an array. So with that then file check is now an array, and at the zero index is going to tell us what mime type it ISS and so weaken again. Just run in array on that and pass. Check it against are allowed file types. So in this case, it's just a pdf. And so this will check and see if the file is a pdf. So if it's not an image and it's not a pdf, then we can return. This file type is not allowed. So I can show you this. If we go to choose file, we just choose this Jason file here, and we had open and upload. You can see we get this file type is not allowed, which is this air right here? Okay, so that's what all this does. So that's the image checking or really the file checking here. Next thing we need to look at is to check if the foul already exist in our destination folder. So if we have screenshot that PNG already in that folder than way, we don't want to upload the same one we don't know upload over top of it. So if I come here to choose file and you remember, I already have rtf screenshot dot PNG in here. So we come over here and we try to re upload this. Then we're gonna getting an air that says sorry. That file already exists. Okay, Now, in this particular case, I'm keeping it simple and just saying, Hey, sorry that fall already exists. You, in your handling may want to do it a little bit different. I know. For example, wordpress does a little bit different set of saying that founder exists, it will upend some sort of number to the end of it. So it will upload the file, but it'll name it like screenshot Dash one. And then if screenshot dash one exists, will name it screenshot dash to etcetera and it will keep going until it can upload the file until whatever number it adds, the end of it doesn't exist. So you can do that in here. Now, again, I don't wanna get overly confusing and complex for you, so we're just keeping it simple and saying Sorry that that father exists. But this is again a place where you could handle that in whatever way you want it. Next check is we're gonna check the file size because we don't want someone trying to upload a three gigabyte file. There's probably something fairy's going on there, and we don't want to three gigabyte file on their server anyway, So you remember. We set max file size appear to five megabytes, so we're gonna check that. So we're just gonna run file size on the temp, the temp file in our temporary folder. And if the file size of it is greater than Max, file size then we're going to return. Sorry. This fall is too big, Pretty simple and straightforward. Once we get through all of that actually before before we go into storing foul There are some other things that you can do here. So if you look, we're checking to see your checking the file types doing that sort of security check we're checking, Father exists. We're checking the file size. We're doing some checks here, and we're going to one more thing later after we store the file to help us out. But there are a number of things that you could do. There's also virus checking that you can run. Your server supports that Ah, which often they do. Then you conduce virus checking that's available inside of PHP, which is usually not too difficult to do. I've ah seen people recommend when it comes to images that you actually have the G d library installed. You pass it to the G D library and have that library essentially remake the image so that if someone tries to slip a virus into your ah PHP into the comment of of the comments that could be added to files or they try to pass an image file, but it's actually Ph. Or whatever you actually send it to you Don't ever upload the actual file itself. You always send it off to the toe to be processed. And so what? You get back and ultimately store isn't the original file that could have on malicious stuff in it you get, you get it remained. So, um, that's another thing that you can do here. There's there's a number of things that you can do here, but you I think you kind of get the idea. Whatever checks you on a run, you just run on like this. And if they don't come back the way you want him, you return right then and there. So it stops. And the last thing you do is store the file permanently, right? So that's what we're going to get into. And this is the function ultimately that you're after for for file uploads is move uploaded file. And so, if you can see, it's really pretty straightforward. Gonna move uploaded files were using temp name. That's the name of our TEP file and then target file, which is the place we want to store it permanently. And so that's all you've gotta pass to move uploaded file, and it will do it. After that, we're doing one last security thing, which is we are changing the permissions of this file that we just uploaded to 0644 The reason we do that is because of some say someone is able to sneak in a malicious file. Having a 0644 still makes it so it's not execute herbal. I was probably still some way around that. I mean, I'm not a hacker, so I don't know all of the different ways that hackers can can attack you. Um, that's why they're often very successful is because there come up with new things every single day. But again, it's just another security precaution. Make it so it's not execute herbal. So even if they get that PHP file on your server, um, standard conventional thinking is that it can't be executed. They still can't then use it to target your site. All right, so if that all goes through, then we'll return. Your file was uploaded in, if not to them is returning this air. There was problems during your file, right? so that is the nitty gritty of file uploads. Again, the couple things move uploaded file is the function that actually stores that permanently . You want to definitely make sure you do all the security checks that you want to do. And then, as I said before, it's kind of a two part process. Europe, the low the file into temporary storage so that you can look at it and do all of your checks. Once your checks all pass, then you can move it into permanent storage, and now you if you want. So if you say that again something like WordPress, where you upload it, it gets uploaded to the file system. But then it also saves the location in the database so that it can display those images later. At some point, this target file variable is what you're after. That's the permanent location of the file. So that's what you would store in your database. All right again, that's uploading files. Hope you enjoyed it. Talk to you next time 25. Create a Multi-Page Form Using PHP Sessions: Welcome back, PHP 101 This lesson. I'm gonna show you how to build a kind of full fledged contact form or multi a page form using sessions that allows you to capture data or keep data intact across the multiple pages. So when users go, if they go fill out some data and then go back to a previous page, all their data still gonna be there for all their pages. So let me show you just quickly how this works. So I'm gonna enter some unique information here, so I'll just do demo um, Demo and John Morse online dot com and we'll select Go to step two here. Now if I click and I go back to home, you'll notice this information is still here. These aren't tabs. These air, whole new pages. You can see. I'm on index dot PHP here. If I go to step two, you can see here. I'm on page two dot PHP. So these air holy, different forms or different pages. So if I click this and then let's say go back to page two, you see, it's still intact. If I go back to page one still intact when you're working with multi page forms. This is really, really important because the most annoying thing for someone filling out a form is if they have to go back and change something for them to lose all of their data and in particular, your clients, this is going to be something that's gonna set you apart and make them kind of wound. Aw, about how this works and make this an actual viable and product that you could get hired to build for somebody. All right, so once we've done this, we could go to step three. I'll enter some information here, so just do something like this. And when I go to step four, it's going to do a final submit on it. So on the final submit, then once everything is successful with the database, we actually will destroy the session and that'll resettle of the data so that now, if they go back to the original page, their dad, I won't be there anymore because it's been submitted. So if we hit step for here, you can see it's has finished. Your submission was successful in here's information we submit were actually showing them back. The information they they submit number. She had dual this. So now if we go back to the first page, there's nothing there, nothing there and nothing there. I also noticed that when I submit these, I'm not getting errors. I'm not getting validation errors here again for a multi page form that you're gonna have the links up across the top here like this. You may not have those, right, But if you're going to, it's especially important that you don't get validation airs here. Really, you're validating would be done on that very last submission page. That's where you would kind of validate your data and make sure everything that you want is actually submitted. All right, so I imagine that looking at that, you know, you can I don't have to explain that that that's pretty freaking cool. And that's something that you would definitely be able to build for clients that would be interested in. I mean, you talk about something like Wufu forms, which I kind of talked about all the time. This is really what they do. They make building forms like this pretty straightforward and easy. Well, now you have the base code for doing that or we're gonna go through it and then just one last thing before we get into actual code, I'm gonna go over to my sequel here, and I'm gonna show you that that data was actually submitted toe you snippets and then we'll just do select all from MP forms of leaves, the name of the table, right? And so you can see here that what I submitted Demo demo, John Morris online dot com. I take acrobats and acting anywhere, laying some. See? So the data that I submitted, you could see it is actually here in the database, and it wouldn't have showed up on page four of the form being submitted because I don't just show the data that's in the session. I actually query the database for the last submitted information and display it back to them. So when you're debugging and messing with this, that's one way you can verify the divided. The data was actually submitted to the database. All right, so with that all out of the way, that we can kind of dive into the code, and I'm just gonna go through this, I'm gonna go through this really kind of how it was built. Um, and what I think makes the most logical sense. So start off with the conflict file. This is really easy. It's just the database name, the use of the password and the host. Okay, so you would just substitute in whatever your database information is here. Really straightforward. Just a few constants. I'm gonna actually go ahead and close that next. We have the function. So this is where a lot of our stuff is actually gonna be done. I'm gonna come back to this, but I want to give you an overview of all the code first. But I want you to notice that most of this is just helper stuff. So we have on escaping function here. We have, ah, text function for we have a check function here for checking. If on item was one of the check boxes was checked or not, we have a text input. So this is gonna build our text input field check box. Wanna submit one? And then we have our database function. So connecting to the database, inserting the data and showing the results. So we'll walk through all what each one of those does but you can see on from the larger picture these air really just helper functions now. The reason I created functions for taxed and check box and submit is because when you do it like this, then it allows you, especially for a multi page form, and allows you to create consistency across your site in terms of your code, because on every one of these pages I'm not. I'm not recreating the HTML code. I'm calling just calling this function. So if I want to make changes when you start getting in tow forms that have, you know, hundreds and hundreds of fields or potentially 30 or 40 fields, you know, if you want to make a change to who, how something is displayed on all of your text fields, for example, well, you might have to go into four or five different pages and edit Don't 20 or 30 different text fields that gets a bit cumbersome. So I put it into a function so that it's consistent and it's easy to update, and at it again, it's this just another version of the separation of concerns principle that you've probably heard about all right, so we'll come back to this functions one. But next is we've template id this out. So we have a header file and we have a footer file. So in header, you can see this is all just standard stuff outside of were requiring our conflict dot PHP file and we're requiring are functions file at the top of Header. So this is the first stuff that's going to get loaded. We're requiring in those two files because those are going to use those on every page and then in our header, you'll know, notice that we're also doing sessions start. This has to be started. And I talked about this in the sessions lesson, but this has to be started before any output. So we put it in a header we put at the top. It's going to be needed on every page of our form because we're gonna be storing session that ah, really the only one it isn't needed is the very 1st 1 index dot PHP, but reminds will start the session there so that we can get everything going. Get our session I d started so again, all at the top of header. This is really the unique stuff here. This is just for error reporting. I left this on furred bugging. You would actually want to delete this and get rid of these two lines for a production site from there. This is just standard H. Dimon. I am as much as possible gonna focus in on the PHP here because we could spend the next three hours together if we wanted to go through all the each smelling all the CSS. What I will tell you is that this is just bootstrap. Okay, so this is a matter of fact. This is bootstrap that's literally copied from there. Get starting section over on their page. Okay, so I didn't want to do anything Super customer crazy. I just wanted to use something that's easy and available out there. So this is bootstrap. It's It's bootstrapped from their actual getting started section over on Get bootstrap dot com. The only thing I really did here was to remove the the brand thing that shows the logo here . I just didn't seem necessary to me, so I got rid of it. I centered this That's in the CSS file. If you want to look through that here again, I don't want to get bogged down without I really want to focus in on the PHP. So that's our header file or further file is even simpler. I'm just including the bootstrap or J Korean, the bootstrap JavaScript. Now, I'm not using that anywhere here, but I've included it here so that you can see where you'd want included. And if you want to use some of the JavaScript stuff from bootstrap in your form than you could do that closing body tag and closing HTML tags so really straightforward. These air really, really easy. From there, we get into our actual content pages. So, uh, let's go ahead. And I guess I'm gonna have toe toggle tree view since accidentally closed that but in our index dot PHP. So this is our home pages. The page we're on now. Okay, we're including headed up Ph. B. And we're including footed up PHP and these files at the top and at the bottom. So again, kind of basic template ing all of this, Uh, right here again. Bootstrap. So container row column. And then I created a custom container here so I could target a little easier called form container and each three tag for the heading here. And then this is our actual form, and you can see here I'm calling. This is why I was talking about earlier. Instead of creating the HTML for each one of my form inputs. I'm calling this text function. I'm passing in. You know, the name of this particular input field? The i d. The ah label and the placeholder here than the name is emails, ideas, email. The label is your email address. And then enter your email address is the placeholder. You can see all of that, your name and in your name, your email address. All that. Here, submit button, go to step two. Okay. And again, we'll go back to the functions file. That's we're going to spend most for a time. But I want to show you how this is all set up. All right, So then Page Two is really, really similar again. You'll notice all this up. Here is bootstrap down here. We get into or functions, check box and submit. You'll notice for check box. We're actually creating array of the different options. So if we go to page two, that's what actually shows these different options. So if you wanted to add options, you just adding array item so it could be like, um, in terms of interest you sport, for example. Well, let that update. But again, this will automatically add it to it. It'll save it in the database, how it needs to be saved, etcetera. You don't have to do anything else to add an interest here other than add it to the array. Here the sessions will be taken care of and so forth. Now, that's unique for this interest check box here. If you want to add, for example, on page one If you want to add a different input here, then you need to account for it in your sessions arranged so forth. I'll talk about how to do that here in just a little bit. But if we go to page two here, you can even see. Now we have sports. Ah, on here. Okay, All right. So again, we're just calling those functions down here what you'll notice and what's important regarding how to set up this form to get it. Teoh toe hold information from page to page Is that remember in header we're starting the session. So the session has already been started for this page. Now we're checking to see if Pope any data has been posted. So if anything has been posted from page one and we're looking for the name in the email, specifically, those have been posted. We're gonna grab that information, and we're going to store it as session name and session emails, the value of what was submitted. So this is what stores it from page to page is you putting it into a session here and now it will be there from if you go back and for you go back 100 times on the different pages. It'll be there once it's stored in the sessions there. Until that session expires, there is destroyed. Okay, so that's how you save information from Peach Peach. That's why if you add a field to say here, you need to account for it here. For you have a new field called address, for example, you would need to then account for that on page two to store what was submitted from page one into decision. Okay, so this needs to match whatever you have on page one. for your fields. That's that's ultimately what it is, right? So if we go to page three now, you'll see a pattern here again, including header and footer. We have bootstrap we have calling are different functions from functions that PHP for displaying the inputs. And then we're storing interests in a session here. So all of these are stored at in one single array called interests. And so that's what we're storing in the session. That's why there's only one here. Okay, so get it's just being a store that these were being stored in an array instead of as individual items. I would be really cumbersome, especially if you have a lot of these, right. So again, we're just checking to see if Post Post that has been submitted. If it has, then we're storing whatever was submitted from Page two and decisions. You'll notice we don't have to repeat what we did here because that's already in the session. We already have it available. It's already there. We don't have to save it again. Okay, It's the nice thing about sessions, and if we go to Page four were grabbing what was submitted from Page three, which is the address the city of the state and then our we're calling our function insert, which is actually gonna insert it into the database. And we're just passing in our session or a so everything that's now been stored in our session. We're passing it into our insert function to to submit it to the database. Now you'll notice that this isn't I mean, you could Technically, when I was younger, I did this. Unfortunately, when this data is submitted on page one here to page two, you could actually just grab the Post data here instead of storing in a session and putting it into a hidden field in the form down here. So creating a new hidden field called name and email and echoing out the information of post from the post array for name and email into that hidden field so it will resubmit to the next page. I've done that. I've seen that. Ah, I don't recommend it. It's more cumbersome needs to be. But here with sections, we don't have to do any of that. So again, that's the value of sessions. So again we're just passing in our session array and then down here. You'll notice we're checking to see So we're saving. We'll get to this in our functions file when we talk about this but the insert function what it returns is if the insert into the database was successful, it returns the I d. That was crater the idea of the road that was created for submitting the status it returns that if it doesn't, if this doesn't go through, then it just returns false. So we have the idea of of the information that was just submitted stored in this insert i d . Here. So here we're checking to see if that's been said If we have something other than false for insert I d. If we do, then we're going to destroy the session. Okay, so this is where you could do some more advanced air handling Now, I haven't done it here, but this is where you could check if this didn't go through. Then you could maybe display something different or display some sort of air or so forth. But if it does go through, then we're going to destroy the session, and we're gonna call should show results. We're gonna pass in that insert. I D so show results is just gonna go back and query the database for the data we just submitted. Now, the reason that I do it that way is to verify that the data has been actually submitted. So what we show them in in step four is the stuff that's actually in the database. Okay, so that you if you see that information, you know, it's been submitted in the database because it wouldn't have been able to query it otherwise. All right. And so then we're just saying your submission was successful again. We're still inside of this. If statement and then I just loop through. If you remember the last page, it just was on ordered list. I just loop through and displayed it. Um, one thing when I submit the interests these interests remember, it was an array. I just un cereal or I serialized it now for what you're doing. This maybe an area where you can upgrade this. All right, I've left things open for you to be able to upgrade here, because ultimately this is about not just me giving you the code. It's you learning all of the intricacies of this and then starting to build your own stuff and make it better and make it your own. OK, but I want to give you the foundation here. So I serialize the information and just stuck it into the database that may or may not be ideal. Usually is not ideal for what you're after, because when that serialized at is put into a database, you can't then query the database for that data. Or at least it's harder to. So you may actually want to create a whole interest table and create a another trait. The treat. This interests that air submitted here differently from the other data and put put that information to a different table on attach it via relationships, too. The data that was submitted for the actual row for the rest of the data. OK, so you may wanna make this better, but I just serialized that So here I'm just checking to see if the when we're looping through the what was submitted to the database, I'm checking to see if it was the interests, and if it is, I'm un serializing it and then imploding it to make it display correctly, okay, and then down here. I'm just printing it out. All right, so that is the overview of kind of how how it all works. Um, I had styled at CSS there. I'm not even bother reopening it because it's just some basic data that changed the way this looked a little bit. Really want to focus in on the PHP. All right, so all of the heavy lifting is done here, and so we can kind of take a look at this. First off we're using, we've created a function that underscore underscore, which is essentially an escaping function. So we're gonna pass in, you pass in any text of this, and it's going to run it through each team. I'll special characters. Now. That's important. Because let's say someone tries to type in something like this for the name, All right? If we If we don't escape this, then when we goto output it to the page when we displayed at the end, it'll actually run the Java script. So that's what's called a cross site scripting attack. So this is fairly in Oculus, right? This this javascript isn't doing much, but you can imagine someone tryingto hack a site being able to insert their JavaScript into somebody else's site. They could try and do all kinds of various things. So we want to make sure that we account for that. This escaping function is what dude does that passing it through HTML. Special characters will actually escape that data and make sure it doesn't act. The the it'll be stored in the database that can be displayed. Bonus displayed. Uh, it uses essentially each to male entities, and so it won't actually run. The Java script will just display the raw code as if it were in pre tags or code tags. So that's what this does. If you go on YouTube, I have a whole video. You can just ghoul your search like cross state scripting attack or something like their cross site scripting have a whole tutorial where you go into that. All right, next one is this checked function. So all this is going to do is check if the submitted value is in the submitted array and we're using that down here in the check box area here to see if the check box has actually been checked. So that when we go back, if we check one of these and go toe step three and then go back to step two. That's what checks to see if this is actually checked. Okay, so we pass in, you'll see here, we're passing in value, and then we're passing in session interest. So we're checking to see if the value each one of these values has been stored in our session interests array. That's what this is doing. And if so, then we're gonna echo out check, tickles, checked, which makes this become rechecked essentially when the page reloads. All right, next is our text kind of input function. And so this is just creating the H two mile for our text inputs. You can see we have the name of of the input, the i d. The label, the placeholder and the type. So you can actually use this to create different types, like an email or a search, or you are You are out. Whatever. You can use this to create different types by passing in this type parameter here. Okay, so this is really straightforward again. This is all bootstrap html. You can see all I'm doing is echoing what the past information where it belongs in this HTML. So label for you passing that you put the idea of input here. The label here. What tape? It is here. The name, etcetera, etcetera. So that's all we're doing here? One thing you'll notice. Also, I'm using a turn Eri here for value on this text input. So I'm checking to see if this input is set in session. So if there's a session variable that has whatever the name this input is in, In the case of this one, it's called name In the case of this one is called email. If that's been set, if it's been submitted and stored in a session, then we're gonna go ahead and we're gonna echo that out. Now, you notice well, passing this to our escaping function. Okay, so we want to make sure and escape it, but we're just echoing out that information. That's what makes it so that when I put in some information like this and go to step two and then come back, it's in there. This is what does that. Okay. All right. Next is check box again. It's really really similar the east to malice from bootstrap, and we're passing a name I D label and then options. And so then we're just using that data down in here where it's where it's appropriate. So the label on then we're looping through each of the options and we're creating our check box. So check box or procreating our input, we're going out the name of it, the value and then our checked function, as I mentioned earlier. So that's what creates thes different check boxes here. So that's why, since we already account for here, that's why, on page two here, all you have to do is add to the array in order to add one of these items here because which is looping through the array of past options. Whatever was passed in this options already here. So you put 100 of them in there, and we're gonna loop through all of them and displaying, and they will be stored in the session and so forth. You don't have to worry about it with this particular function. Next is the submit function. So again, pretty straightforward. It's a button. It's a submit type or pat allowing to pass in the class, and we're echoing out the value so the name that we wanted toe to say here. Defaults to submit, but we've passed in. If we go to any of these pages and see we're passing and go to step three and then this, um, Rock. Whoa! The right. Great quotes. Right. Side quotes here. All right, so that's the submit function now, kind of the the meat of this is then the database stuff. Okay, so we're connecting to our database. We're creating a new connection, calling it con and connecting to my SQL. I so knew my school. I were passing in our database host use their pass and name from our conflict file that we covered earlier. We're checking to see if the connection was actually made. If it wasn't, then we just gonna go ahead and kill the script there because we can't really do anything. And then here we're going to return our connection so that we can use it. So that's all fairly straightforward. That's just creating the database connection here. We're gonna actually insert the data. So we're casting the data to an array because that's what we need here. We're connecting to our database. We're calling our connection function. So we can get that going. And then here were white listing and converting our variables 22 or converting our session data that was passed that in an array into variable so that we can use it in our prepared statements. Down here, there's several ways that I've seen of doing kind of all of this of taking data that was submitted in an array and turning it into something that could be used in prepared statements. Here. I've written some stuff. That's but I wanted to be really verbose about the way this works, because, ah, frankly, this is a PHP 101 course. So I don't want to get a lot of this is probably ah, maybe a little bit kind of fast and fear hits for some of you. So I don't wanna I didn't want to write something overly clever to show, try and show how smart I am necessarily. I wanted to write something that you could understand what's going on. And then, if you want to make your own stuff clever by all means, go for it, right? So once we connect, what we need to do is white list our data and we need to convert it to variable. So what I mean by White list is we don't want to take any. Remember anybody, any hacker king send queries to page four of your form here. Okay, They could sit on their computer. And just like, you know, we talked about the curl lesson, how you can send post requests to any euro. Well, someone could sit there and do curl requests like that over and over and over again. And there's other tools that allow you to do it even easier than that. So you can't assume that whoever's querying you're hitting your database is gonna be someone who's gone through the process that you've defined. Anybody could do that, and then if it's not set up properly, they can hack your database so white listing. And so they in that they could send, you know, they get sent anything that they want in terms of data to the form. We want to make sure we only collect the data that we want. And so we want the name, the email interest, address the city in the state in this particular set up. So by specifying from our data array what exactly we want and storing it as variables and not doing anything else with the rest of the array were white listing it that's built in white listing so I could send post data to that page for that included something like dog equals roof right and that be an element in the array. Well, it doesn't matter because it's just gonna be discarded. I'm not using it. I'm not specifying it here in this chunk, so I'm not gonna use it through the rest of this code, so it would just be disregarded. So that's That's a white listing to make sure you're on Lee getting the data that you want . And then we're just converting them into variables because that's what are buying Pran function down here. Needs are buying cram method down here needs is these invariable forms, so we just do it all it wants again. I've seen different ways of people doing this. There's other ways that you could go about doing it. I've written it different ways, but this is the most kind of basic and simple, verbose way that you could do it, and it works just fine. There's there's nothing wrong with doing it this way. Um, and a lot of ways is probably easier to read than some fancy little loop thing that does it for you. All right, from there. As I mentioned before, I'm serializing the interests array because you can't store in array in my sequel. So you have to serialize it before you put it in there. And so that's what this does next. We're gonna then actually start getting into inserting this. So the first thing we're gonna do is we're gonna prepare creator prepared statement for a my sequel, Curry, and then we're gonna bind the parameters for it. So you can see the statement equals our connection, our database connection, and then this prepare method. So this is what's gonna prepare. Create the prepared statement. So are prepared. Statement is insert into MP forms and then name, email, interest address. City state. These are the fields that we want to insert into. So these air your database fields, whatever you have in your database, that's what goes here for the values you notice. We put question marks, and that's again. That's what prepared statements are now in module three. I'm gonna go much much deeper into my sequel. So if you're not quite familiar with all this yet, that's fine. We'll get to it. But I wanted to give you some of this here so you could start getting prepped for it. So you notice we're not passing in the variables that we had up here because we're gonna We're gonna use peach peace built in prepared statements. Ah, functionality. To go ahead, we don't want to insert the raw data into the database. We want toe basically look at it first and make sure there's nothing crazy in there. Do any sort of escaping and so forth now with prepared statements. What's nice about those is this will, pretty much I mean, really don't see this changing for the foreseeable future. What changes is in the back end for preventing SQL injection attacks. What changes is the way that PHP itself looks at the data that you're gonna inserts when use prepared statements. You're you're really kind of future proofing your code because the changes, they're gonna happen at a level that doesn't affect your code. It's gonna be underneath the way. PJ PHP essentially binds and executes this query. So this is how you future proof it and make sure it's secure. So anyway, that's why these air question marks not our actual parameters of actual variables. Right, So that make creates are prepared statement, and then here we're gonna bind our parameters. So what we're essentially saying is we created are prepared a statement and we're saying OK , so where the first question mark is in our prepared statement and it could be anywhere along here, but it, But it's always with where you use it is where there's user submitted data. So our database fields, these aren't user submitted. We don't need to do prepared statements with that stuff. The name of our table that's not user submitted. We don't need to worry about that. It's actual stuff that was submitted in our form. And so we're saying where this first question mark is, put the name variable, or the 2nd 1 is, put the email variable where the 3rd 1 is put interest, fourth address, etcetera. So the number that you have here knows there. Six needs to match the number you have here, and they need to be in order of how you want him to show up and they need to match the fields they're gonna go into, because what's essentially gonna happen is this name Field is going to be put in place of this question mark, which is gonna get inserted to into this name field in your database. This email one is going to be put in place to the second question mark, which isn't going to this email field in your database. So all that needs to be in order, and then this is just telling PHP what kind of what kind of data this is so that you can do strange. You can ask their d which is strange or digit her imager. So in this case, these air, all I guess, Yeah. These air, all strings. That's why they're all s okay. So that that tells PHP. Okay, these were all strings and PHP notes. Okay. I need to run a certain ah, certain set of checks because this is streamed out enough. It's injured. Out of those checks are different, but all that happens in the back end, you don't have to do any of that. And this will prevent SQL injection attacks for you. Okay, once that's all done, then we can actually run our execute method, which is gonna execute the query. So this is what's actually gonna run the query, and we're going to get a response back, which is gonna be stored as insert here. And so then what word? What we're doing here is, then we're checking to see if this is true or false, and then we're going to If it's true, this insert I d on this connection. So our connection was our database connection. This insert i D property will give us the last submitted idea of the last submitted row in the database. Okay, so this is going to give us what we just submitted, and it's gonna be stored as this insert idea returned to are to where we actually run this function insert, which we do on page four. So that's how that insert i d gets passed to here. Okay. All right. And then, of course, if it doesn't work, we're going to return False now for showing the results. You can see what we're passing in is the insert. I d. What? We just got up here. So we go to page four. This is running our query if it worked. What we're getting back is the i d of the last submitted row stored is insert i d. We're checking if insert I d is not false, not empty. Essentially. And if it isn't, then right here we're using, We're calling show results and we're passing in that i d that we just got And so show results is gonna take that i d and it's gonna run a query and so it's going to connect to the database. And actually, this should be D but it's gonna connect to the database and then we're gonna preparing Bind again. So now were are prepared statement is select the name email interest address city and state from RMP Forms table where the I d equals question mark, which then we're going to bind with this insert i d the insert i d. We just got back from inserting the data into our database and the reason I changed from STS because the insert ideas and actually is an in injured. It's not a string, so I don't know why I put us there before, but it should be D, which is what it is now. So we're binding our parameter word executing our statement. And now this one is going to be a little bit different because we actually want to get We don't just want tour false back. We want that data because we selected data. So we're creating this response variable. And we're running statement execute here. Her statement get result here. So this is actually going to get the results set. Now, if you're not familiar with this, this is something that you know I've talked about. I know I've talked about before another my sequel Tutorials that I've done. But the what you get back from your query and my sequel isn't the actual data. It's really a resource. And then you use PHP toe essentially decode that resource into something that PHP can use. So running get result here is just going to get the the resource from my sequel. Once we have the resource, then we call fetch array and you can call, fetch, object. You can call, fetch directly, etcetera. But we're calling fetch array, and then you can pass in a constant. And here I'm passing in my school. I associative It could be indexed. Um, So you can get ah associative array back and get an index straight back. Whatever you prefer and we're storing that as results, this is what's going to actually get us an array that we can then work with. And so once we have that, then we can return it. And that's what gets returned here to the show results gets returned here to this results variable. And then you can see here I'm looping through that results of arable and creating the output out here. All right, so, again, this is what allows all of that to happen. All right, so that's all the code again to recap. What's happening is on each page we have a form we're submitting the form that post at is being passed to the next page. So Page One passes the page to page two passes of age three page people three passes to page four. We are grabbing that post that and storing it in a session on each page, so that it then persists. If people go back and forth here, you can see this data is still showing up, so that's what makes it persist. Is storing it in this session and then echoing it out. And then when we get to page four, we're actually submitting it to the database. We're making sure it was submitted. We're creating what was submitted, and we're displaying it to the user. All right, so that I know, I know that's hot and heavy. That's fast and furious. But we're getting into kind of more professional development, getting in tow, getting a little more serious here. So I want to challenge you. I want to throw this at you. There's a lot here, and you know, 26. Designing Your Database and Object Model: back P one, no one in this lesson. We're gonna be getting into database designed. I want to do this before we get into the code, because you can learn all the code and figure out how to do that whole part of it when it comes to my sequel. But if you don't have a good idea of how to structure database, how to put your object model together and really, the main thing that I want you to get out of this lesson is a consistent process for doing that with every application you build. If you don't have that, it can be really hard to build a really good application, a scalable application and easy toe work on an upgrade application because that structure tends to get in the way and the way to really think about this. And I'm gonna get more into this as we go through. But really, you shouldn't start with the code. You actually, your object model really determines your data model or your database structure, which then ultimately determines your code and how you have to write it. So you're the code is actually several layers down the line. There's some things that you need to do before that. And once you do that, the code kind of tends to right itself. And just real quick, A quick story. This was one of the big ah ha moments for me. When I first started built trying to build applications. I didn't understand the different kinds of tables and things that we're gonna talk about here. And I was kind of creating object tables that where I would have maybe 30 columns in the table. And I was trying to put all of the information that was related to a particular object into one table. And then, if you're working with a blogging application, for example, when you have a post record, you might try and put categories in there, and it just starts to get really cumbersome trying to do it all in one table. And so I started actually studying WordPress to see how it was done, because one of the first big applications I built was a CMS, and I started to see that this kind of structure and I looked at some other ones. I started to notice a pattern of how they were done, and then I looked kept going into object modeling and data modelling and ultimately kind of figure out how this all comes together. So I wanted to give you that in this lesson, because I think I know for me was a big ah ha moment. I know for you, especially if you're kind of just new starting out. This could be an ah ha moment for you as well. And I want to help you to be able to build applications there. Scalable. They're flexible. They're easy and fun toe work on instead of being these big, cumbersome things that you have to deal with. All right, so why does this sort of thing matter? Well, the thing to get is that your database structure represents your upper limit in terms of the flexibility, the scalability, the scope, the clarity, the ease of upgrade of your application, your database structures. Really, what determines that it's not your code? Your code flows from both your object model in your database structure. So if you get your database structure right, if you build it four flexibility, four scalability and all those things, then it's gonna make your life a heck of a lot easier And so that's one of the reasons why we want to do this. And as I said, the more modular and flexible your database structure is that the easier it's just gonna be for you to scale your application and getting clear on your data model. Your data base structure first makes writing your code much easier. The code almost kind of writes itself once you have this stuff in place because you you kind of see how you need to connect it all together, and it's your code that ultimately does that. And as they again, as I said, your code is really secondary to your database structure. Ah, this the structure of your database will determine how you're gonna have to code what kind of code you'll have. So what you want to do is make it easier on yourself and you want to develop a database structure that makes the code that you have to write cleaner, clear, more efficient, while keeping it all still really flexible and scalable. So we're gonna talk about your object model, maybe something you may have never worked on or dealt with before. We're going to talk about the three different types of tables that you must have. Ah, and we're gonna talk about your data model and and really, what I want you to get from this whole thing is a consistent, step by step mapping process that you can follow for every up for every application that you build. So you can go through this exact same process for every single application that you build. Now, as you do it, you'll start to see the there's patterns and so you don't have necessary after repeat the work. But it gives you something to rely on when you get into building an application that maybe is new to you. It's a different kind of application. You still have a reliable process that you can work through. All right, so it all starts with an object model. So what is an object model? Well, first, consider that every application you build will ultimately be a collection of objects that have certain properties, can perform certain actions and have certain relationships with other objects. So the object model is the blueprint of your application. It's the objects that you're gonna have in your application. Uh, it's the properties. Those objects, they're gonna have. It's the actions those objects objects contain and the relationships between those objects . So, for example, if you were to consider a blogging application now, a really, really simple blawg application would likely consist of, say, a user object, a post object and a category object. And the post object might have the properties, title content, date status and so on. There's obviously Ton that you could add their, but those are some kind of ones that air tried and true that will usually be there. And the post object may have relationships with the category object. So you may want to say this post belongs to this category or is in this category. That's a kind of relationship came. And so the the user object, as another example, may have properties the properties, user name, password, email, etcetera and be able to perform the actions create post at it post delete, pose, view post. You know, Crete create edit, delete view categories, all all of that stuff. There's lots of actions that the user ultimately could perform. The object model is you laying all of this out, you creating a kind of road map of exactly what objects you're gonna have what their properties air gonna be. The data you're gonna collect on each object, the actions they're gonna be able to take in the relationships they're gonna have with any other objects that you have out there. So if we were to kind of take a look at this, this is an example object model. So you can see on the left side we have our three different objects. We have post user and category. And for post, we have the properties title content, that date status type author, and again, you could go on and on. But properties air really meant to be things that are required for that particular object, meaning they have to be there. That information is required of every single one of these objects that you make. And you either have it entered explicitly by some sort of user or it's generated automatically. So you need a title. You need a content. You can say that you need a date. Ah, status of is it a draft? Is it published the type of object ID type of posters. Now, this is kind of something again unique based off my wordpress experience, because you actually have different post types in WordPress. So you have posts. But you also have pages. They're actually the same object, but they're just a different type. We have the author here and so forth. So again, these are all of the different properties that you might have that air really required for this object to be able, an instance of this object available exist. So in your database, the record for the for each individual post would require it would need tohave be filled in with each one of these. These ah piece of these property elements write a post in terms of action. A post can actually perform in action. It has actions performed on it by the user, so there are none. And then it's possible that it could have relationships with a category. If you're doing tagging, have tags, there's a non argument there for users. You notice we have the author and the properties Ah, area you could technically create. We'll get into this, but you could technically create a relationship table and do it that way. Most of the applications I've seen, though, actually put the author in the post properties, so there's some wiggle room there. But ah, again, you would have different relationships that your post might have with other objects. The user object so its properties are log in. Or you could maybe say that's its user name the password, the name the email again, you could add to that list if you wanted. It's gonna be the primary thing performing actions, so it's gonna be able to create post at it. Post elite pose view posts create categories at it, delete view categories, all sorts of things that the user user is going to be able to do. So most of the biggest chunk for the users probably gonna be the actions block and then relationships that it could have. It could have, ah, relationships with the post, um, again, again, you can either create a relationships table or you can put it in the actual properties of that other object. When you start to have a Mork complex relationship set, that's really when you want to get into a relationship stable and we'll talk about, we're going to talk about the three different types of tables here in a second. So the category object again. The properties I D named slug and so on. Can't perform any actions, and it will almost certainly have relationships with the post object. Okay, so, again, this is really just a map of all of the different objects you're gonna have the properties that you're gonna collect the data here and collect about each one the actions they're gonna be able to perform and the relationships that they will have with other objects in your in your object model. Okay, so this is important to kind of lay out so you can go ahead and print this particular page off. I've given you kind of a simple grid here that you can use to kind of write this down or type it in or however you want to use it. Ah, of course you can make your own. You can use an Excel spreadsheet whenever you're doing your mapping. But really, these are the key things toe kind of know and and lay out ahead of time Before you ever start writing any code so that you have kind of this this this laid out, you know, again, what objects am I gonna have? What are the properties? What are the actions And what are the relationships? This is again kind of the blueprint of your application. All right, so now, once you have that, then you want to map essentially your object model into your data model. So what What is a data model? The data model is your application brute blueprint at the database level. So it deals with how the data will ultimately be stored in your database. So if the object model is kind of, ah is kind of a map of your application of Hey, this is the blueprint of what it's gonna look like your data model is really it's It's a map of your database and were ultimately that data that you're gonna have to collect based on your object model is going to be stored. So essentially, you map your object model to your data model, detailing where the properties, the meta, the relationships, all of that is going to be stored. So, for example, the properties of each instance of your post object may be stored in a table named users. Okay, so earn a table named post. Ah, that would make sense because that's where all your your post that is and may include things like title content, D and status. It may have relationships to the category objects toe stored in a table named post categories, and that would show you that X Y Z Post belongs to X y z category. Okay, so the this gets us to talking about the three different types of tables that you would have in any sort of database structure, the 1st 1 is an object table, and that's going to store all of your properties about a particular object. And that's gonna be really the name of the object. So in this case for the post object and maybe posts or posts and the 2nd 1 you're gonna have is a made a table. So meta is optional data about each one of your objects. So you may have a post meta table, and the reason this is important is because it helps create that first layer of flexibility . Okay, because there may be certain there may be certain instances of a particular object. So there may be certain posts, is it? So you have 10 posts in your blog's. There may be some of the those posts that have certain data and other posts. Don't if you again. If you're familiar with WordPress and you look at ah, good example is from a plug. And actually, if you look at a word WordPress plug in WPS CEO or Yoast Seo. What it allows you to do is specify a focus keyword. It's a S e o Plug in that allows you to specify an SA focus keyword and will analyze your title. Your post content, all the everything, all the data you've entered it will analyze it and give you advice on how to make it more seo friendly. Well, you don't have to enter a focus keyword on each post. You can create a post without entering that focus keyword. So that's optional data. Whereas you can't really create a post without a title, you can. But it'll just create a thing that says, like no title, Hey, or you can't really have a post without content. Yes, it allows you to do that, but it is just It still creates that entry and just leave the blank. Ah, you can't really have it without a date and so forth. So there's some data that is really meant to be there for every object title, content, date, etcetera, author. And then there's some that is purely optional. Really, it could be there doesn't have to be mayor may not the post can or the object can go on without it. That's what you put in a metal table. So again for an object you may have post and post meta table user user met a table category in category met a table here. Those are all things that you could have depending on your object. Do you think you're gonna have that kind of of data for each one of your objects? The third type of table than is a relationships table and the relationships table is how you tie two different objects together. So that's how you tell. That's how you say, Okay, this post belongs to this category, and often times the the only. There's really only two columns in relationships. Table object. One i d. An object to i d. And what that essentially is gonna allow you to do is you're gonna have the i d of your post. Okay, Your post i d. So let's a post record. One is in the first column and the second column you're gonna have category I d. And so you would have you know, the idea of that category, which is one. So you'd have a one and the one you'd have object i d category d. Then you'd have one and one. Or maybe you want to put it in in the second category that you you created. So that would be number two or the one with the idea of two. You put that a to in that column and you would actually have, ah, for Post one. It could have multiple records. So would be the first column. Would you might have the 1st 3 records be one all referencing post I. D. One. And then you'd have different categories for each one of those you might have in the first record, it would be post one category one second record, be post one Category two and then the third record be post one Category three. That would show you what that's telling you is that that post is in all three of these categories Now the reason you do it this way is it's simply a much more flexible way of managing these type of relationships because it doesn't tell you what kind of relationship it is. The context of your application does that. Also, since you're referencing another object, you have a whole other CATIC. You have a whole other table for the categories that you can then put a bunch of property elements in about that category so you can store a ton of data about each one of those categories. If you were to try and take, let's say you say I don't want to do that. I'm going to just say that, you know? Ah, Post one, I'm gonna put a critic category, call him in my post table, right, and I'm gonna list the names of each category that it belongs to. Well, you would have to try and pack all the data about that category into that. Ah, that column. Or, you know, if you if you don't want to create the relationships table, you could You could technically put a one category column and put a 123 in there. Say it's referencing these these category I ds and then have your your category ideas Well , but having their relationships table makes it so that you can easily you don't need to. You don't need to pull the whole record from your database in order to change the categories or mess with that stuff. You have a simple, concise, efficient relationships table that allows you to to change what categories something belongs to and manage it and so forth. And it's just really simple on a lot more flexible. So those are the three kinds of different tables that you could have, and those all go into helping you create your data model. So in order to create your data model, then there's really kind of four steps to it. First, you want to create your object table, so create a table for each object in your object model, whatever they happen to be. So, for example, in our blogging application, you might have ah post table ah users table and A categories table. Now, if you want a pre flick prefix, those like WordPress does feel free to do that, so that would be WP post OBP users WP categories. So that's the first step for every object that you've laid out on your object on your object model. Created table for it and inside of it, then Step number two is to map the properties. So turn the object properties that you laid out in your object model into the columns in your object table. So for a post object, you'd have a table named posts, and the columns in that table would then be title content, date status, etcetera. You also want to make sure to include an i d field that auto increments and we're gonna show I'm gonna get into show nya kind of how to do all this in the code and so forth. But you want to create an I D field auto increments. This allows you decreed an index so that each record that gets stored in your database has in a number associated with it. And that's its I. D. So that becomes important when you start using the the relationships tables and the meta tables and so forth because you're gonna be constantly referencing that I d the idea of the object that you happen to be adding metaphor or building a relationship for right. So again, map those properties in that table. Step number three you wanna creating made a meta table for each object. Now you can analyze this, you can decide whether you need a meta table or not. Some objects might not need a meta table. It might not make sense that there's gonna be this optional data added to it. I would say most cases having a meta table is probably it's probably going to turn out that you'll need one. You'll have certain data. That's just you want to be able to add to some and not others and not have it be kind of a big mess. OK, so create your made a table for for each object, and you know that that's going to allow you to again. That would give you that initial flexibility to application Step four, then create your relationships. So for every object, every two objects, they're going to have a relationship. You need a relationships table. So, for example, if you're gonna have posts and you're gonna have categories, you need a relationships table just for those two. Those two objects so you'd have a post underscore categories table if you're gonna have tags and you're not gonna go all crazy like WordPress dozen creep this whole taxonomy thing to try and smush him altogether. You just gonna have Ah ah. Categories object in a tags object. Well, you would also need a post underscore tags relationship table so you'd have one for posts and categories. You'd have one for posts and tags. If there are any other objects that have any sort of relationship that you need to tie together that you want this object to belong to this other object, you need a relationships table for each one of those relationships. Okay, so create those relationships, table. And then, as I mentioned before, you really normally only gonna have two columns, you're gonna have object, I d object one I d and object to 80 and you're just gonna tie the two together that way. Now it is true that you could create other columns that could add information that describe the relationship in particular. Usually that comes from the context, every application, your code, you don't have to. But sometimes you may need to add some information that describes the relationship itself. Not necessarily not either one of those objects. Any of that stuff should go on the object of the meta table. But if you need to do scribe, the relationship in particular. Then you can add another column that will. Then there would be some way of describing that particular relationship in in each instance of that relationship. I've really only ever seen this, I think once and it was in WordPress and has to do with their term taxonomy lay out the way that they do that, Um, I've seen some auto load stuff and some other applications and so forth, but it's pretty rare. And in each one of those cases on Lee, one extra column that was added. There wasn't a ton of different columns because usually again, you don't need toe. Specify a ton of information about the You don't need to say much about the fact that this post is in this category. It's kind of interesting it or it's not now. There's not a necessary a ton of information to describe about that. So, um, one example of where you may do that is I know in WordPress I think this is actually comes from us. Tesio. I've been running both so long, I don't always know who added what, but ah, you can set a primary category for each post that would be in an example of extra information that's describing the relationship. So the relationship of cattle or post one and category three you would have, Ah, you would have another designation, another column that marks that as primary so they actually use it for when they do. Yost uses it for when they do the breadcrumbs that they automatically add to your WordPress site. And so it has you pick one as the primary, and that's the one that shows in the breadcrumbs. So you may have something like that where again you need something, that extra information that describes that relationship in particular. All right, so that's how you create your ah dot data model. Create the object tables map the properties creature made of tables and the data that would go on those and then create your relationships tables. All right, so here's an example kind of database structure that that you might see so you would have a posts, a post meta and a post categories Tables Post is obviously an object table. Postmodern meta table and then post categories is a relationship table tying post two categories, then user and user meta, and then he would have categories and category meta. And so that's an example of a database structure that you might see for a simple blogging application. Right? So hopefully that that gives you some sense of how to create an object model and then map that to your database structure. So then you know exactly how to lay out your data base. And it's really means really kind of foolproof There's not. It makes the decision making not sitting back trying toe. Oh, I gotta figure out all this stuff I gotta add you. You can sit down and literally map out. This is what my application is gonna have in it and then that maps pretty directly over to your database structure. And then as you want to add to your application, you realize, OK, now I got Teoh. I'm gonna create. I'm gonna add this particular object for whatever reason. Well, now you know. Well, I can just map that through, and I'm gonna need a object table for it. I might you need a meta table for it. What are all the other relationships it's gonna have with other objects? I need relationships. Tables for each one of those relationships so Ah, again, it helps just a systematic way of doing this. Now. One thing to keep in mind with this is if you again. I work with WordPress a lot, so he uses an example. But if you look at its database structure, you're going to see this. You're going to see the posts and post meta and users and user meta. You'll see you won't see categories you'll see terms they calm terms. They lump tags and categories and all that stuff into one and have ah, way of doing that. But as biggest WordPress is on, it was complex in advance, and all the things that it does it has. I think it's 11 tables, so it's 11 tables that power the whole thing and again there use their user, made a post post meta think four of them are just for the way that they do. Ah, terms and do categories and tags and so forth because it's a little different, more complex way of doing it. But so it's really actually a pretty simple and straightforward database structure for what is a really kind of complex and advanced application. So hopefully that helps you if if you're still a little bit unclear, I recommend kind of going back through it in the video and actually following along with that sheet that you printed out. Think of an application you might want to build and go through and actually write it down. That's gonna help you to kind of put some meat on the bones of this and actually get some experience doing. I think once you do that, you'll have a lot clear picture of exactly what you need to do in order to build a scalable , flexible database structure that makes it easy for you to build and grow and scale your application. All right, thanks for watching. We'll talk to you next time. 27. Structure Your Database In PHPMyAdmin: welcome back to PHP 101 This lesson we're gonna be moving on from the first lesson is talking about or object model in our database structure. We're gonna kind of take a look at an actual real world database to try and help put some more meat, give you some more meat in terms of structure in your database. And then I'm gonna talk about data types a little bit because that is one of the things that when you're building application, you start looking at that A types and my sequel and always necessarily clear. And so I want to kind of give you just some basic rules of thumb, um, about how going about using them and so forth and hopefully make that a little bit clear. So I'm inside unexamined all database here. This is actually a WordPress database, and I wanna just kind of go through this and have you note the similarities of some of the stuff that we talked about in less than one. So you notice as I mentioned over there, there are 11 tables that you have here in in a word press. This is what WordPress comes with by default. Now, plug ins will add their own tables. So you may go into yours or someone else doesn't see a bunch more tables. But by default, this is what Wordpress is gonna have. And if you look at these tables there exactly what we talked about. So we have comments and comment made a. We have posts and post meta. We have terms. Ah, and we have user and user made A and then we have some extra ones links options and then for terms you have term relationships and term taxonomy. So again, the foundation of this whole thing is the the object tables, the comp comments, table links, options, posts, terms and users. Okay, those air, the core objects that you're working with inside of WordPress, the other tables again we have, as we described our meta tables are optional data about the different users or in this case , comments. Or you may have met about the post so forth. Okay, so that adds extra additional. And then we have really here we have this one relationships table, which is the term relationships this taxonomy table. It's just unique to the way that WordPress does because it smashes categories and tags and any sort of custom taxonomy that you might create all in tow one. And so, um, this kind of helps handle that. But again, we have just a standard relationships table at the end of the day. So if we click into toe one of these, I'll click into an object table. So click into the post table here and we'll actually take a look at the structure. Then you can see that we have in here are list off properties. Thes would be the properties that we identified in our object, our object model. So of course we have our I. D. You'll notice that this auto increments that's important so that if we go back to browsing , you'll see for each one of these posts each one of these records New post that you create, there's an idea associated with it, and it kind of tends to increment up. So if it's skipping so 12 and then goes day, that's probably cause that post got deleted at some point. I did do that with this particular excite at one point, but you see, it just keeps incriminating up, and so each instance of an object has its own I d that you can then reference as you work with it. So if we go back to the structure, then you have Post author date Ah, content title excerpt. All of this information that the creators award press thought was something that these were all really, really important. They needed to be included in in the actual object table. Right? So if we move on from that, we go to post a meta, then you can see this is a little bit different. So you're gonna have meta i d Which is really so again, you've with post. You have this. You have this I d that auto increments. Well, you're gonna do that with every one of your tables. So post meta you're gonna have an i d for each each meta piece of meta that you add to an object. So this isn't your post i d. This is actually your i d for this table. Your second column is your post I d. And then you third column is Medic E and then met a value. And so what that allows you to do is you can say for this post. I want to create this this ah piece of data, this piece of metadata and I want the value to be this this meta value. So if you look here, what that means is that you can actually create this on the fly. You don't have to. If you want to add a piece of data about this particular post, you don't need to come into your database and then create a new column and update all your code and so forth. You can just make up the name, the name that would normally be. The column name is this medic e name and then the value that would normally be the content in the column. Is this meta value here? Okay, so it makes it really flexible that you can add optional data data kind of on the fly to objects that some may have some may not have on, and it also allows one of things that allows, particularly with WordPress, is it allows ah allows you to create plug ins that you can add data to the objects that Aaron Wordpress without having to update the database structure and WordPress. And so this is kind of one of the doors that that WordPress opened early on that allowed plug ins to become such a big thing inside of that ecosystem. This is one of the ways one of the things that that kind of makes that possible. Okay, So again, just made I d the object i d Medic e and meta value. Ah, if you know, we go to comment, meta you'll see the same thing. Met I d common idea. Now we're referencing a comment medic e meta value and on and on. Okay, so that's what a meta table looks like. And then the last one we have is the relationships table. And so as I kind of mentioned you have your object, i d. In this case Ah, this is gonna be referencing the post and then you have term taxonomy. I d. So again this If you're doing a more standard way of doing this, this would probably be like category I d. Now that has this term order. I think this has something to do with primary versus non primary categories, but I'm not even sure to be honest with you. I don't really know exactly where they use this term order, but a lot of relationships tables won't even have this third column. You just have your object idea in this case, post I. D. And then your category dear here term taxonomy I d. That's what creates the relationship. That's what ties. Ah, that's what ties them together. So ah, it's really that simple. And now you know that this post belongs to this particular category and so that I mean it z really that simple? You have a post table with all of the main properties that you wanna have. You have a meta table that each one of these meta tables again. It's structured pretty much exactly the same. The user meta post meta comment meta. It's all I d object i d medic e meta value. And then you have your relationships table, and these are all pretty much structured the same way you have your object. One. I d object to i. D. If you're tying post two categories would be post i d Category D. If you're trying users to post, it would be user. I d post I d. Whatever the relation nature of the relationship is. So that's how you actually this is kind of looking at it and actually putting some meat on the bones or the theory that we kind of talked about in less than one. Now, on top of that, as you go about actually building your structure and I'm gonna go over here to WP posts, what you'll inevitably run into is that as you create these columns, you have this type. So you have to kind of set some of these settings in here. And so these these type declarations can start to be a little bit tricky and again will be completely transparent with you. I'm not necessarily, um I'm more on the PHP side of it. I'm not necessarily some my SQL geek that just knows everything about my sequel. I know what I need to know in order to do what I gotta do. So what I want to give you is kind of the way I think about all of this, and I'm not going to say that. How I think about it is exactly 100% the perfect way to think about it. It's really just ah model that I use That helps me short cut. Ah, the the way I think about it. Okay, So when you're thinking about my sequel data types for me, there's really kind of three that I think about. OK, so there are numeric types. There are string types and their day and time types. And if you look in here, I think pretty much every one of these is one of those. Okay, so, uh, strings numbers and they in time now the date and time stuff. I'm not gonna talk a lot about here because I think it's pretty self explanatory. And my in fact, if you look at one of these daytime ones, you can see that it has a format for a D in time. Essentially, what that allows you to do is it allows you to store this in my sequel in a way that my sequel understands that it's a date time. So instead of just throwing a UNIX timestamp in there, you can actually store it as a formatted date and time, and that helps you with ordering and searching and all sorts of different things. So if you're gonna be put in date and dates and times, you know, a date time into a column it should be a date Time, um, format. So I'm not gonna spend a lot of time on that. I think that's pretty self explanatory. We'll get into, you know, the code and stuff later, but there's actually date time function or in in my sequel. That makes it easy for you to take whatever you have in PHP and store it in my sequel as a day in time. Okay, so the two that were then concerned most with our numeric and string. And so I'm gonna cover the numeric first and one of the things that you'll notice we have here big in 20 and then we come down here a little bit further. We have into 11 so people try to figure out what should I use. And should they use Big Aunt Ah and so forth? And so what's the difference between the two? Well, the difference is what what seems obvious, and that is the size of of what it can store. So, for example, and can story value up to 2.1 billion and big and construe, or a value that is like 20 digits long? I don't even know the name of it. And so the difference is, is the what it can store the how how big of a number it can store? How big of a value Aken store Now, you might be then tempted to say, Well, let's just you big in for everything. The problem with that is that aunt is actually a four by manager, and bigot is an eight by manager. And what that means is, if you're using big Int where you don't need to, then you're ultimately going to be messing with your performance. Hey, because using it rather than big and can make a significant reduction in dis space so that one change alone, depending on your set up, could save you 10 to 20% and more specifically, If you're using it as a primary key, your foreign keys and indexes reducing it could, uh, it could be up to 50%. And of course, that's going to improve performance. So how do you determine? Think that's a big question. How do you determine should he use insure they use big and so forth? Are you going to store more than 2.1 billion rows in your table? So, for example, for this I d right here. Are you going to storm or than 2.1 billion rows in your table? The answer is probably No. And if it is? Yes. Then you might consider that whatever you like, whatever you're building, my sequel may not be the best Teoh database structure for it. Or, you know, if if that's ah, that's something you're gonna be doing you would wanted dive much more heavily into this, um, than even I would be able to take you. So chances are you're probably not going to be building something with more than 2.1 billion records. Now you see here WORDPRESS uses Big Aunt for its I. D. Is there a chance that anyone WordPress site is gonna have more than 2.1 billion records? Well, it's possible. I would guess that they probably did that just ah, because you have sites like, say, New York Times and some of these bigger sites that may start to at some point reach that upper limit. I don't know exactly why they use big instead, aunt, but they decided to, But that's the difference is it's how much ultimately, how much of this space is going to take up. And so ah, using the right one for the right situation is going to help you save this space and ultimately help with your performance. I would guess most the applications that you're going to build, that it would be sufficient could be wrong about that. But that would be my guess. Okay, so that's really the biggest difference between and Big Aunt all of the different imager data types that are out there tiny. And it has to do with how much dis space it's ultimately going to take up. So you just want to think about what it what it's for, You know, for example, this one's for the I. D. Begin post author. They used begin as well. No. One thing people get confused by is they think that this 20 is some sort of limiter on this data type. It's not. It just has to do with display with. So that really has nothing to do with how much data can be stored in that record. It just has to do with display. Okay, So post parent is begin. Now, this one, if you're gonna have this one, be begin, you pretty much have to have this one be begin. Because if you think you're gonna end up a two more more than 2.1 billion posts in this case, then there's a chance that hit the post parent could be more than higher than 2.1 billion. Okay, so those are kind of, ah, tied together comment Count again. If you think you may have that many posts, Well, chances are if you're gonna have comments you each post If it had one comment, you would be at the exact same limit. A lot of posts have more. You're more likely to hit that number with comment count. Then you really are with with I D. So again, just think about your your application and decide, okay, Should no Should I be using big it Should I be using it and so forth and again it's Ah, it can story value up to 2.1 billion. So you're gonna have to more than 2.1 billion records, so you can decide that. So that's the the imager kind of data types of the numeric data types. The other one is the string one, so with that. We're looking at tax long tax. Bartsch are all of these other ones that that we see here. And so the difference between something like text, medium tax, long text It's the same as what it was with the numerical ones. It's the amount of data that Aken store so long. Text in store more data than text. Ah, a long text in storm word out of the medium text medium text more than text, etcetera. So that's again. You want to look at it. You'll notice here that they're using text for pretty much everything except for post content that has long text. Will that make sense? Because there's a chance that this could be there could be a lot of data in here, so it makes sense to have this one be long. Text title. A title is only gonna be so long. The excerpt WordPress actually cuts until I think, Well, you can put it in manually. But chances are it's not gonna be near as long as the post content. Okay, so, again, that's as you build your application just going through making these decisions, and it's easy to air on the side of all just make it bigger that way I never go over. But that's also going to take up more dis space. That's gonna ah, hurt performance and so forth. So you just want to think that through is you're going for it through it now, one of the things that people I think ask a lot to is what is the difference between these text text on long text Anvar Char Why would I use var char instead of text or whatever? So the difference is is that ah, text and blob and all the other ones they're stored off the table and the table actually just has a pointer to the location of the actual stores to remember This is a This is a computer, this is computer program. So the data, the actual data in the database for text on blob is stored in a different location. It's not actually stored in this table. All there is in the in the table. You don't see this through PHP, my admin. But all there actually is in the table is a pointer to the location of the actual storage, whereas with Var char, it's stored in line with the table. So what that means is var char will be faster when the size of the data that's being stored is reasonable. So if it's going to be something that is predictably shorter, ah, then var char will probably be faster. But if you're looking at something that could be arbitrarily long, you don't like a post content you don't know. You can't predict that. That's going to be short, right? So if it's something that could be arbitrarily long, then or you, you reasonably assume it will be, then you would want to go with one of the text data types. So var char is just used for stuff that's predictably short. So post status? Well, the users of WordPress know that there's Onley. They determine what status is they're going to set right? There's only a handful draft published, I think private, there's a there's a handful of like 45 Maybe so. They know what they're gonna be so they can predict that they're going to be short so they can use far charges. It's faster comments, status, same thing. Peeing status. Post password could reasonably assume that's probably gonna be a little bit short. Post name This is, I think is actually the slug which they're gonna auto create themselves, and it's gonna be tied to the title so they can kind of predict it's probably gonna be a little bit shorter. Ah, the G u i d. Again, I predict that it could be a little bit shorter post type. There's only there's only so many post type. There's, you know, you can create custom post types, but ah, you see this one over here, it's post or its page or whatever it ISS, so they know that it's they can predict. It's probably going to be shorter so they'll use the faster method here again, thes have nothing to do with any sort of limit. These are all about display, okay, so just ignore those altogether. So that's the difference between something like var char tax, medium text, long text. So var char would be used for things like maybe a user name or an email. Ah, country. A password, etcetera text would be used for messages, emails, comments, format attacks each to Mel. Medium text might be for something like, you know, if you have a large ah, you know something in Jason that's really large or something like a short to medium length book would be the size of medium text or a seat. CS v string. Long text would be no textbooks programs. Really, really big long book like lots of content Now again, you can look at WordPress on their choice of why they used what data type for the different things and and maybe disagree, Right? So you may think, Well, why does post title need to be text? It could be something something different or, you know, those post tech content need to be long. Text could be medium text. Or, you know, there's there's there's things you can look at and say, Well, why they do it this way. Ultimately, it was just what they decided. And so again, you have to think that through yourself. But hopefully giving you some of this information helps you determine the difference and what the trade offs are between. You know, making sure you don't someone when they're typing in their their title doesn't hit some sort of arbitrary upper limit of how long their title can be versus. Nobody's ever going to do that. And if I created as long text. The post title life says Long tax. I'm gonna be unnecessarily taking up this space that I don't mean to it. So those air those air kind of the trade offs with all of this different stuff or with Bartsch are yes, it's faster. But if it starts to get really big than it Boggs that it's in line with the database so we can start to block down the database and ends up actually being slower. So do do I want to use VAR chart here? Dont w