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 we use something like text or whatever? All right, so again, that's that's the difference is hopefully that gives you some idea of how to lay out your database. So again, when it comes to getting back to laying all this out like we talked about in less than one , you have your post or you have your object tables. You have your meta tables and you have your relationships tables. You have again examples here of different types of properties you might add to one of your object tables. I've showed you how to how your meta tables will look how relationships tables might look, and then we talked about the different data types that that you might use here, Finally, some of this other stuff, you know? Ah, collation attributes Knoll default. Really? The only one that I pay a ton of detention to is this auto anchor Met one, which for the i D. Is what you need. Okay, um, those that that's going to allow this one. So whenever you create a new record, you don't have to tell it. What idea is it? Just auto. When you add a new record at auto increments and adds that creature you on automatic kind of index there, So ah, we're going to get into Maurin a little bit. We're actually going to get into some code talking about dynamically mentally and dynamically, creating um tables in your in your database and we're gonna I'm gonna kind of show you I'm gonna take everything we've learned in these 1st 2 lessons, kind of condense it down into Here's what I dio And so I think that hopefully will give you and more clear picture than of exactly what you'll need to build do to build your applications. But I wanted to give you all of this information so It's kind of there is background, so that as now we get into some of the code, it'll it'll kind of all start to make sense and click for you. So thanks for watching. We'll talk to next time.
28. Create Tables Dynamically In PHP: Welcome back, PHP 101 This lesson. We're gonna get into some code here and we're gonna talk about I'm gonna be talking about creating tables dynamically. We kind of talked about modeling database design I took inside of database talk about data types. But most of the time, when you're actually gonna do is just create this all in code, and it's gonna be pretty straightforward. Came. So I'm gonna show you were gonna working side of my sequel a little bit. I'm gonna show you how to get all connected up and then how Toe toe to create some tables Because you're gonna need that before you couldn't get into doing all the crowd stuff and prepared statements and so forth inside of my sequel on PHP. All right, so the first thing that we need to take a look at is we need to take a look at getting connected to our database having a database, the tables inside of and so forth. So to do that, you can either use PHP my admin, which a lot of hosts have to have. Ah, PHP my admin. Or you can use the tournament terminal now. I did a little bit with PHP my admin before. So now I'm going to use the term terminal in what we're doing here. Ah, the first thing that you're gonna need, however, is you're gonna need your credentials to be able to really work with my sequel. So the if we look at these credentials here database, user database, password and database host these air often going to be These were going to come from your host. We're gonna create a database, and that will be your database name. But these are going to come from these gonna be kind of standard things that either you have a user has access to all your databases or the host that you use every time you create a new database that creates a new user and so forth. Every host is a little bit different, so it's a little difficult for me to show you exactly how every single one is done. But these credentials again, you're usually going to get from your host. Now I'm gonna show you where I get mine. So this is one of the hosting providers that I have. And if I scroll down, you can see I have this thing right here. This is my sequel databases. So if I head over into it, it gives me some options. First, I can create my databases in here if I want. I don't have to, um, And the way this works, you know, for me, if I when I do you the user name and password and had created I'll actually create the database. A new user Give it all the permissions and a does all of that for you Here. You can also create a standalone my sequel user, where you'll create your own user. And then you can manually, in code or through the terminal, give it permissions and so forth. It's a little more of an advanced thing, but for me, where I get my user name and password, you'll see user name and then a password. And they're both right here and then for me, accessing my PHP, my admin. Actually, click this manage button on that takes me over to PHP. My admin logs me in. And that's how I access it on this particular host. Now, I'm not worried about the password because since we're done here, I'm going to delete this database. But you obviously don't want to share this with with anybody. Ah, that that could get access and so forth. But again, if you're looking for that here than here's your user name and password, if you're using a local host environment all most of the time, the user as root and the password blank is gonna that's what's gonna is gonna be by default because it's a local host type set up if you're getting into something else. Like I've used nitrous in the past actually no longer exists. I'm, um, actually using Cloud nine. Now they're all a little bit different. I can't tell you exactly where that's all gonna be, but you'll get that somewhere from your host. Your application, whatever. All right, so again that your user name password your database host will almost always be local host. The only one I've ever seen this not be that way is Go, Daddy, Go. Daddy will actually give you and I think it's an I P address. So again, you just depending what you're using, you got to kind of figure that part of it out from there. I went ahead and put in this PHP one. A one is the database name I haven't created yet. But we're gonna created here in a second. And then we can take a look at what's going on here. Right? So I'm gonna go down to the turmoil now, here for this, and I'm going to get into my sequel. So you just type in the pseudo and the my sequel command. Now you'll see I have my sequel over here and we can start doing some stuff inside here. So the first thing that I want to do is I just want to do show databases. So it's just show databases and then the semi colon, and that's going to show us the databases that we have. So you can see here. I don't have this PHP 1101 database. So I'm going to created So in order to do it, it's just create database and then the name of the database and then the semicolon. I think I take that right, so we'll hit. Enter there. And it says it didn't give you an error. So it went through. And so now if we showed databases, you can see that we have this PHP 101 database in there. Of course. If we now go on, we say use PHP one a one that's going to allow us to use that database. And then if we do show tables, you see, we have no tables, so it's an empty set, right? So what are code up here then is going to do is actually create those dynamically. You could sit here in the terminal and create a mall and all the, you know, all of the different columns, pills and so forth, but that would be really, really tedious. Okay, so most of the time in your PSP applications, you're gonna be doing this, Um, a V A code. And this could be sort of some via some sort of install script. I've seen it done a number of different ways. You know, Wordpress has its famous five minute install where you ended. Enter the database, details the name, the user name, password, all the stuff that I just showed you that you have to get from the host. You enter that stuff in there, and then you hit go, and it creates all the tables and so forth. Well, this is kind of how they do it. This is what's happening on the the back end. All they've really done that's different from what we're doing is they've kind of created a front and form to enter the information. But really, on the back end, they're gonna be doing the exact same thing that we're doing right? So once you have all your credentials and so forth here, then what we can do is we. First, we need to connect to our We need to connect him to my sequel so you can see where in stance eating a new instance of the my SQL I class were calling that object my SQL I and we're passing in our host, our user name or password and our database name. So that's gonna connect us to my sequel. And it's It's essentially what we did down here in the tournament terminal where we did pseudo my sequel. And then we said, Use PHP one a one. That's essentially what this code is doing right here. Okay, so it's getting us connected. My sequel in its selecting our database for us to be able to use then what we're doing here is we're just checking to see if we connected successfully or not. So we're just checking to see if there's a my my sequel error. If there is, then we're just printing that error out. And then we're exiting because we can't do anything because there was some sort of air. Now this is over here on the right. This is I'm actually this file right here. I'm on that file and I'll refresh this here in a second, but, ah, we, uh actually, if I just do this now, I can tape, echo connected and then exit so we don't create our table, Jenna kind. Want to walk through creating our tables yet? But if we do that, so this is gonna echo that we're If we get past our error, it'll echo connected and it'll exit before it starts creating our table. So if we refresh that, then you could see that we get that connected. So we're all connected up. We're good to go. Giants will get rid of this. Save that, and then what we're doing here is really just kind of what we talked about. So we're gonna create some some some tables and we're gonna create some columns inside of those tables, and we're gonna do it all in code. So you see here we're creating an objects table. We're creating an objects meta table, and we're creating an objects tags, table sellers of the different ones that we decided, ah, to create This is our relationships table down here. Okay, So in order to do that, then we're just creating Ah, my sequel query here. And so the query in order in PHP to be able to create a table is create table. Now, we're doing create table, if not exists, So we don't want to overwrite any or we don't mess with any table that might already exist . So we're just We're gonna create a table if it doesn't exist in the name of it is objects. So if the Objects table doesn't exist in our PHP 101 database than we're going to create it , okay. And then here were passing in what we want our columns to be. So each line is a different column. So you can see the 1st 1 is our i d or passing in big int and then unsigned, not know. So we don't want it can be? No. And then we're gonna auto increment. Okay, Pretty straightforward. Next one is post Underscore title. It's text data type not No, we don't want it to be able to be. No. Next is post content. Long text not know. Next is post name Var char not know Post date, date time Not know. So this is really what we covered in less than two. It's the name of your column. What type? It is some of the miscellaneous settings. And then that's it. You go onto the next one. So you know, it's all these air not know. So we don't We want them. We don't want them to be able to be. No. And then this one is auto increment. Okay, So all very straightforward. The last piece here than is the primary key. So we want to designate what the primary key is for this database. What's the index? Essentially. And so that is our I d field here, and that's pretty much always going to be the case. And then you can see we close the bar, parentheses, we close the barcode Ah, double quotes and then we do our semicolon. So we basically just done kind of a standard. My sequel query here, this is pretty much how every my sequel query is gonna be structured. Ah, is you're gonna have you know, you're gonna have your your double quotes. You're gonna write your all of your query inside of it, and then you end with a semicolon. So all of this inside these quotes is actually the my sequel queried specific toe my sequel . So, um, again, that's gonna create the table, those columns and so forth. So that's the query. And then down here, we're gonna actually run on my sequel Query method. So again, we created me in science. A new instance of that job class into our my ass girl. I object. And now we're running methods off of that. So the first method or this the method we're gonna run here is the query method, which is actually gonna query the database. And it wants a my sequel query or SQL Query. So we passed that in. That's what we're passing objects table in here. And we're just checking to see if that came back as true. Right? So that's an air check. So if there's no errors, then we're gonna print out that table objects was successfully created. Okay, so that's what we're doing here. Next for the meta table. It's really the exact same. So create table, if not exists. Object Meta we have our met i d which is our primary key here. Big ant on sign. Not whole auto increment object. I d begin unsigned. Not know Medic e var char to 55. That doesn't really matter to display that meta value, long text and gay So these are in its here that we talked about. It doesn't have to be begin. It could be just regular into Did a bar chart here for Medicaid? Because I these the keys will when you're creating meta, the keys will be predictably short because you don't have to type in some big, long key in order to access it. Eyes for the value. I used long text. You could probably debate that maybe you get away with text, but this could be arbitrary long. We don't necessarily know what What's gonna go in there again? The primary key is our meta I d up here. Right? So that's the query were running. You'll notice this code and this code are exactly the same. Except that this is the objects met a table variable were referencing here. So we're gonna We're gonna run the query method on it again. We're gonna pass in our query. If there's no errors, were gonna print that. That table was created successfully. All right, Last one. A little redundant here, but sex. Same thing. Create table not exists. Object tag relationships. I d began unsigned, not knoll auto increment, comma object. I d begin unsigned. Not know. Tag. I d. Begin on sight. Not No primary key is our i d appear okay. And we're gonna pass that into our query method. That equals true. We're gonna print out that is created successfully. So again, this is your career coat. You could actually manually type all of this into my sequel down here if you want it. But it's kind of a pain in the butt to see, hear and type it all out. And if you're obviously going to be distributing your application to other people for them to use, well, then they're not gonna want to sit there and do that or going to PHP my men and do it. So you want to create this code for them to be able to do it. All right? So we come back down here and we do show tables just so you can see that again. It's still an empty set. So now if we refresh this, you can see it says objects. Objects made an object tab tag relationships tables were all successfully created. So we go down here and we do show tables. Now you can see we have object made a object tag, relationship and objects. And if we do describe objects, so we're gonna look at our objects table, you can see I d big in 20 on sign. Not no primary key auto increment post title tax post content. Long takes, etcetera. We can do describe object meta, and you can see exactly how we said it. We do describe object tag relationships. You got that right, and you can see again exactly how we said it. So now those tables all exist, So that's how to create ah tables dynamically. Now, when looking at one, that's thing. When we're looking at your ah sequel quote appear again. Most of this stuff I mean this this line right here. I've used this over and over and over and over and over and over. There's I mean, every application I see does this, so there's not necessarily a reason to really overthink it or recreate the wheel for your I d block. You can pretty much use that for every application you're gonna build. Okay? It's also always gonna be the primary key. So this will always be there, these ones in the middle. It depends on your application. And it's all comes back to what we talked about in less than two. About the length of tax versus long tax of arch our daytime stuff, etcetera. So you kind of got to figure that out on your own. What makes sense for you? But I would say Just don't overthink it too much. So hopefully that gives you a clear picture of just how you would go about creating these tables inside a PHP code, the code to do it and then, you know, not overthinking. Gosh, you know, should this be int and should be, you know, 21 I it's not as big of a thing as as, especially when you knew you can kind of make it out to be. So Like I said, I've used the kind of the same thing over and over and over and over again. All sorts of applications. Do you know, if you start getting into some really advanced database structure, the truth is you're going to get beyond, really, where even what I do on a regular basis. And so at that point is kind on you toe to really dig into it and really find that nuance. And there are some my my sequel geeks out there that can tell you all every last thing that you could ever want to know about my sequel. The truth is, for most of the stuff you're going to do, most people you had high building websites for people building, you know, small applications, whatever. It never really gets all that complex. And so this will suit you just fine. So again, just don't overthink it too much. All right? That will do it. Thanks for watching. We'll talk to you next time.
29. Connect to a MySQL Database With PDO and MySQLi: Beach. Be one on one This lesson. I'm gonna show you how to get connected to or database now kind of cover this a little bit less than three. But I want to also show you how to do this inside of PDO, in case you want to use that option. So every one of these lessons I'm gonna show you kind of how to do it in my school. I And then I'm also going to show you PDO that way that you you have both. So, as I mentioned in less than 34 your credentials most of time, your host is going to be local host. Ah, there's exceptions of that. You'll have to obviously check with your host and and see if local host isn't working. Then you would If you're getting ah invalid host or some air related to your host, check with your host, see if they have support documentations or you can just contact them via support about what you're supposed to use or where to find to find that Ah, your database name That will be what you the one that you created. So however you know, you create that whether use the terminal, you went into PHP my admin or you didn't encode. However, that will be the naming your database. And then you use the name of past you usually get from your host. Or if you're a local host, usually route with no password will do the trick. But again, whatever software you happen to be using so again, once you have all that, then we can then again talk about how to connect. Sweet. So we kind of already went over this Ah, in less than three. But we're gonna just for my school. I we're gonna stand seat a new instance of my school. I we're save it as, um this variable my school I we're gonna pass and our host our user name or password in our database name. Um and then we're going to check to see if there's any sort of error in trying to connect. And if so, we're gonna print out that air here, and then we're gonna exit because we can't do anything else. Ah, if there's no error, so this else statement more than we're gonna simply eco connected. So we come over here, we'll just refresh this real quick and you can see we're getting this connected item here. So we're all good to go. We're connected than we could move on from that into running some of our queries, which we're gonna cover in future lessons. All right, So we like I said we kind already covered that, so I went over that quick. Now we're gonna comment that out, and we're gonna come down here, and we're gonna connect via PDO. Now, the difference between my school and PDO is my school is designed specifically for a my a scoop on my sequel database. Whereas PDO you can use a little all sorts of different databases. And so it's a little bit different and how you connect using it because you actually have to specify that you're going to use my sequel here. And so the kind of standard way to use this is to use try catch blocks on. And so we're gonna try and connect, And if not, we're gonna connect, we're gonna catch the exception, and then we're gonna echo out the error. And so, in order to create the ah connection, we're gonna instance eight a new instance of PDO and into it Well, it is very similar in terms were passing in the same information, but we're just specifying what database we want to use here. So in this case, you have to start off and say I want to use my my sequel or, uh, you know, Oracle or whatever. Whatever it is that you're using, you specify that here. So we specify my sequel. We use a colon, and then we do host equals, and we pass in our host. Okay, so these brackets allow us to pass in a variable here, um, without having a content anal that and so host equals our database host. And then we do a semi colon, and then we do db name equals and the name of our database here. And then after that, we can do this is this is kind of the, uh this is kind of the unique part of PDO eyes. These this first little this is you notice This is all in this kind of first comma. This first ah parameter there year. Passing into PDO is my sequel. Colon host the host name semicolon database and database name. That's kind of unique PDO. So once you get through that, then it's comma, then database, user name and password, which is just, like appear database, user name and password. Right. So this is the really unique part here, and it basically comes down to your specifying what day debate. What database kind of database you're gonna be using. Once you have that, then you Ah, you use the connection to set an attributes. And this is essentially setting the era mode. Ah, this is so that we can actually work. If there's an air, we can actually catch it and do something with it. Otherwise, PDO, um, kind of tends to like hot tends to hide it by default, So you're not really going to see an error. So this is saying, Hey, we wanna we want to see the air's Now. Once you get into production code, you may wanna You may consider getting rid of this. It's really up to you. But that's what we're doing here, so that if there is some sort of exception, we can connect it, we can catch it. So then here we were all connected. Then we're gonna echo connected. There was no air or anything like that. Then this will. This will execute if if there is some sort of air, we're catching that error. And then we're gonna echo that air out here. All right, so we save this and then we come back over here and we refresh. You can see it Says connected as well. So we're all connected up here. Now, if I were to come up here and I don't know, maybe do something like this, right and change the name of the user to something we know doesn't exist, Then you can see that's where we get the air. It says access denied for user rule. That's because user route doesn't exist. Okay, so that's what your errors will sort of look like. Um, so that is again, it's pretty straightforward. There's not necessarily is one of things. Don't overthink it. There's not a ton toe to dive in and get about this. It's really just this syntax on PDO that's a little bit different of specifying what database you're actually gonna be connecting to. Well, to a house with you, I've only ever used it to connect to my sequel. I've never used any of these other databases out there, so I can't tell you a lot about how those in particular work. But you know, if if you're here right now, using my seek, when you think maybe down the road there's an opportunity or a chance that you might end up having to you move to some other sort of database, then it might be a good idea for you to do all of this inside of PDO as opposed to my ask July. That way you don't have to go back and rewrite all that code. Really? All you would have to change in terms of If you're using a different database that had the same structure, you'd really only have to change this right here. And you could connect up to it and use it and and would be fine. That's kind of the whole point of of PDO. All right, so that is how to connect to your database using both my SQL I and PDO. Thanks for watching. We'll talk to next time
30. Read Data From a MySQL Database With PDO and MySQLi: back PHP 101 This lesson. We're gonna talk about how to get data from a database with first things that you're going to do inside of working with my sequel. So we'll kind of move quickly through what you already know again at the top Here we have our credentials that we've talked about before. We have our instance e ation of our my SQL i class. We are again checking for arrow, printing out air, printing it out if there's any sort of error, and then we're getting into our query. So the new part here is going to be this line, right? Starting right here. And what we're doing is we're calling our query command off our my school. I object, and we're running an SQL query, and this is a kind of standard get query. So what we're doing is we're running select all from objects, So we're going to select all of the data. All of the columns that are available from the objects table. That's what this says right here. Okay. So again, it was very, very basic query that you would do inside of my sequel. So we past that to the query Command and we store the result in this result Variable here. Now, the thing to know the where people can kind of get tripped up is a lot of people think OK, that query has run. And now I have my result here and that that is, if you look over here to the right, we have this array. That is my array. So now all I need to do is look through that Iran. I'm good to go, but that's not actually how it works. So what this actually returns here is a my my sequel resource, and the data that we need is inside of that resource. But we have to actually pull the data out of the resource first. And so that's why we have these three lines right here. That's essentially what this does is it takes this resource and it uses this method called fetch object. And you could do fetch Assoc. If you wanted Teoh and it's going to fetch the object, it's going to essentially go through that resource and is going to fetch all of the objects that are there. And so that's that's what we need to do. We need to go into that resource, and we need to fetch each. Each record is essentially an object. So we need to go through that resource and fetch all of those records are all of those objects. And then what we're doing is you can see here we're storing those objects into an array. This results array. So this will What this will end up with is an array of all of the different objects that were in this resource. Okay, so that's what this wild loop is for here. And then what we're doing here is we're simply printing out those results. And that's what gives us this over here. Okay, so that's Ah ah, It's pretty straightforward. Especially the sequel. Part of it, really. The big trick here is this Wild Lupin making sure that we pull out the data that we need from the my sequel Resource. Right. So if we close this, then or we comment this out when we come down here to PDO and we'll save that, and then we'll go ahead and refresh this, you can see that we're getting something pretty similar here, and I'll show you what the difference is. Okay, so here. We're gonna do our connection to PDO again, which we kind of already talked about. So we're connecting to PDO were using my sequel. We're making it so we can see if there's any heirs and then we're gonna run our query and you can see here in our query Now, instead of doing select all we're selecting Specific column. So we're selecting I D Post title and post date. So we skipped post content and I did that just seek I could show you the difference between selecting all and selecting specific columns. Now, the best practice is to select the specific columns that you when you create a database, select the specific columns that you want to work with. Don't just do a select all because now you're getting back data that you don't need, and that's gonna hurt performance and so forth. All right, so this is another way of doing the same thing Now all we're doing here is we're simply selecting the columns that we want. We're selecting it from the objects table and then we're ordering it, so we're gonna order it by post date. So we've actually put these in kind of a specific order. And so that shows you the order by and then you would ever call him. You want to order it by right? So once you have that again, this returns a my sequel resource. So we have to do the same thing that we did above. So we're gonna use our wild loop. We're gonna go through that resource and here we're gonna fetch, and we're gonna use ah PDO specific version of fetch object. And so we're essentially doing the same thing. We're fed, fetching each rower record as an PHP object. And we're storing that into this result. A race of what we're getting here is an array of objects that we can then work with, and then we're printing that out. And then, of course, we're doing our catch block. If there's any errors, were gonna print those out. Now, the thing to know about this these two lines here and both here and in the my SQL I This is a really standard way of working with my sequel data. So getting an array of objects is pretty common. That's anymore these days that I would say that's how most ah, most applications do it. You certainly could get an array not of objects, but ah so another associative array. So these could, instead of being objects here, these could be a raise. And that's what would happen if you did fetch a social instead of fetch object. But again anymore, The kind of the standard way of doing it is to fetch them as objects and use them that way . So another one of those things where it's not necessary to recreate the wheel or, you know, overthink this too much. Generally, you'll be able to use the code that you see here kind of across the board in most of the applications that use. I know for the stuff that I do. I mean, there's there's different things, but in terms of just a a kind of standard fetching of data from from, ah, database, I use this over and over and over and over again. OK, so again, that's how to do it in my SQL I and PDO. Thanks for watching. We'll talk to next time
31. Create Data In a MySQL Database With PDO and MySQLi: Welcome back to PHP 101 This lesson. I'm going to show you how to insert data into a data bees using both my s July and PDO. So to start off, we'll go down to a tournament terminal here, and I'll just do a select all from our table that we're working with to show you that right now you can see it's a empty set. That table is empty. And so I'm gonna come over here to the right hand side and run our code here and you'll see that it's just returning a wand for true. So it says it went through. So then we'll run that same query again, and you can see that now. In there, We have demo title. This is demo content. Demo article is the post name and then the post eight. Right? So how do we do this? All right, so at the top again, we'll start off. We have our user credentials. I've kind of covered that. Ah, and in depths up to this point so I won't go through that. And then we have our query. And so the query is what really does all of the work here. So there's a couple kind of key words that were going to use here the first or key phrases and the 1st 1 is insert into So we're going to start off our career e with insert into, and then we're gonna put the name of the table that we want to insert data into. So in this case, it's the name of our tables called objects. And then from there, we're going to list out the columns that we want to insert data into. And so we have post title, post content, post name and post date. And so those air the only four columns in our, uh, in this particular table. And so those are the ones that we list out here. Then we we use this values keyword, and in these parentheses we put in the name or the parameters of the content that we want to insert into the data we want to insert into each one of these columns. So for post title, we have demo title post content. This is demo content, post namely of Demo, article and post. Now we're using a my sequel function called Now, which I'll explain here in just a little bit. So the first big thing to get about this Is that the order in which you put your column names here? It has to match up with the order in which you put your values here. So if I were to switch this around here, so if I were to let's go ahead and just cut this out and let's come over here and switch these around like this This query is not a smart queer, you know, it doesn't know that this is the the content and this is the title. It just goes by what order you put them in. So if I refresh this and then we do a select all again, you can see now it because I switched him, it puts this is demo content into the title on demo title into the post content. So it's based off of what order you put them in. So you need to make sure that you're putting them in the order that in the proper order, your matching the first element in this parentheses here with the 1st 1 in the values print this season so that they match up correctly. Okay, so that's really that's all there is to it. It's really just matching these up and making sure that you have things in the right order . Ah, and we're not getting into prepared statements or any of that stuff yet. I'll get into that in a later video and walk through how to do. Ah, all the different queries were going to write. I really just want to kind of go through the queries that we have here and show you how to do that part of it first, right? So that's that's the bulk of it. The last one here, then, is this. Now function here and so you can see it's now just look looks a lot like a PHP function. But this is a my sequel function. And what this does is this inserts into the database that I'm now. Now you could technically do something like Ah, no, now or now equals time like this and you could grab that time from PHP if you wanted. But my sequel has that already built in. It'll format it into the right, um, into the right format for war. The date, time, the date time, Ah, data type that we use for a post date column here, so it kind of takes care of all of that. So unless you know you're passing in some other date, if the time is gonna be now, then you could just use this now function pretty much all the time, so it makes a little bit easier. You don't have to do all the stuff in PHP to figure out the time and so forth. You can just use this and my sequel. Take care of it. Right? So that's the query. And from there it's really a lot of what we've done before. We in stanciute new instance. In my school, I passed in all of our credentials that we can get access. We check to see if we're connected. If not, we throw an error and then we pass our query, appear into the query method for for my SQL I object. We capture that as a result. And then I just printed out the result here, which is the result of this is going to be just tour false. And so in this case, it's true. And so you could then move on from that with whatever you're gonna do with the rest of your application. If it's false, you could catch that. Maybe troubleshooting or showing air. Whatever. However, you wanna handle that? But the result of this is again going to be true or false in this particular case. All right, so that is how to do it in my school. I Now I'm gonna comment this out and we'll go down and show you in PDO. Now, here's the nice thing about it. What you'll notice is I'm I'm using the same query that I have appear from my ask July. As I am in PDO, I'm using this query the same quarry. You don't have to change the actual SQL syntax when you're using my school, I vs PDO. If you're using this my sequel database now for using something else, that may be different. To be honest with you, I don't really know because I've always just used my sequel. But ah, because we're using the same database in both. You don't have to change the query here. It still works and so make sure this is saved and then we'll refresh this and here with PDO , we're getting back this object and it's showing us what are queer Iwas And so if we come down here and do a select all again, you can see that we added 1/3 line that we didn't have before. So this one was added via PDO. So began its exact same query up here and this is all pretty much the same code that we've been using up to this point in instances. A new instance of PDO. Tell him we're using my sequel Personal Our credentials Ah, this is for heirs here were actually running the query using the quarry method on what we instance heated up here, passing in our query to run it. And then we're printing out the result. With PDO, you actually get what your query was back as the results. So that's a little bit of a cork and then or difference between my school and PDO, and then we're catching any heirs that we may get so not a ton of difference once you kind of have the thing that again, Don't overthink it. Don't you don't have to recreate the wheel. Once you get this kind of connection code down the query code down, you'll notice run inquiries is all just about changing this and then, of course, how you handle it after that. So if you're getting data, you're going to do something different with the data afterwards versus if you're inserting data or deleting data are updating data, etcetera. So you're gonna do something different with it when you get it. But actually, running the queries is all pretty much the same. So again, once you kind of have this down, then it's really, really simple to just use it over and over and over again. All right, so that's how to insert data using my SQL I m PDO. Thanks for watching. We'll talk to you next time.
32. Update Data In a MySQL Database With PDO and MySQLi: looking back. PHP 101 This lesson. We're gonna get into how to update data in your database on So again, it's very It's really straightforward. A lot of code, exactly the same. It all comes down to the difference in the query. We're running here, so our credentials are all the same here. And then when we look at our query again, a lot of the syntax here is pretty straightforward. So it's what you would probably thinker it. It's easier, remember, because it's it makes sense why it is what it is. So we're gonna start off when we're going to say we're gonna update. So we're updating. So we're going to say we're gonna update our table name. So in this case, we're updating the objects table, and then we use the set keyword to say what we're gonna update what we're gonna set as what ? And so you can see here we have post title equals and then we're setting it to test one, and then we have post content equals and we're setting that to test one content. And then we have post name equals and we're setting that to test one. You'll notice here that we're only updating three of the four columns. So when you do an update, you don't have to update everything you own. You only have to update the columns that you want to update. And so that's ah, kind of important to remember as you're going through writing these queries that you don't need to go through an update every single column. If you want to update one record, you can update on Lee the columns that you want to update. All right, so we're updating those three and then we have aware clause here because we need to tell it , you know which of the records we want to update. And this is why we kind of talked about using I ds in the kind of the 1st 2 videos. This is why that's important, because now, instead of having to do some kind of search to find the record we want or whatever, we can just specify the i d of the record we want to update. So we want to update record number three, this one right here. So that's the record we're going to update. All right, so we set that query and once that's done. You'll see here. Stance E 80 new insist on my s July. Check for errors, pass it to the query method and then put out the results. So it's all the rest of the code is exactly the same. The only thing that has changed is this query right here. All right, so if we run this and you'll see it gives us a one, which means true. So you can see, Before I ran a select on you could see this is this number three record says demo title and so forth. So it's the old data we had there. So we'll go ahead and do a select all from objects again. And now you can see that we get it back. And our our third record has been updated to test one test one content and what we set here Okay, so pretty straightforward in how to do that. So let's go ahead and change this now to test too, and will comment all of this My school I code out and will come down here to PDO and literally nothing has changed. Ah, in this code from this the last lesson to this one this is literally the exact same code as it was in the lesson before. And that's because the only thing we've changed is this query. So this is everything that we've done up to this point? Exactly The same. All were changing is the query. So we've updated that. Let's come over here and you can see we get again with PDO. It gives us back what are actual queer Iwas as a result. And so if we come down here, do a select all from objects from our table You can see now this has been updated to test to test to content test underscore too. So it's been updated to what we just changed it to, right? So again, that's how to to update data really straightforward. And you know, this is again We're working through the foundation of how to just do this very basic crowd create, read, update, delete the very basic fundamentals of interacting with the database using both my school I and PDO. Alright, So again, that's how to update data. Thanks for watching. We'll talk to you next time
33. Delete Data In a MySQL Database With PDO and MySQLi: welcome back to gauge P one a one. This lesson we're gonna go through. How did to delete data from a particular table and this will kind of round out again the fundamental interaction of interacting with the database, which is cruddy, create, read, update, delete the thing I want to say before we get into the code here. Since this is the final kind of lesson on that, those those four fundamentals is that is a lot of people one working with database When you're new to it, you think there's a lot of this stuff that that's out there for you to learn and so forth. But the truth is, crowd is really the core of everything. Everything that you do with the database ultimately revolves around creating, reading, updating or deleting those air. The four basic functions that every ah database out there has, and so if you understand that, then you really have the foundation of everything you need to be able to interact with database point being. There's not a ton of other stuff out there for for you to learn anything else out. There is really building on top of this, and it's where you start to get into more complex queries and so forth stuff like that, but understanding the fundamentals of interacting with the database. It's just these four things, and so this is the last one in that scenario. So again, as I've said, kind of harped on all of the code, pretty much stays the same or does stay the same, except for the query rights are credentials of the same. And when we look at the query again, it just kind of makes sense that it is what it is. So the key word or key phrase we use is delete from and then the table we want to delete from. And then we're using Aware Clause where it says where I d equals three. So we're going to delete the third record here. That's what this says we're gonna delete from the table where the I d of the record is equal to three. Now you're wear clause doesn't have to be the i. D. It could be a search that you do. You could say where post title equals demo title. You could do that, but again, this is kind of a foolproof way of doing it If you know the idea of the record, no other record is gonna have that i d. You may have to records that have the same title, but you'll never because of the ah auto incriminating that we do with our databases. And with this I d column, you'll never have to records that have the same I d. So this is a foolproof way, foolproof way to make sure that you're getting the right record. Also important that you have this here because if you were to not have this here, for example, let's say you just just said delete from objects that would delete everything in your database. Fact, that's one of the ways that you can use truncate to delete. But you could also just use delete from objects, which is basically shorthand for delete, all from objects that would delete every record. Do you always need to make sure that you have this where Klaus otherwise you're gonna end up deleting your entire table, right? So once we have that, then again, instance a new instance the my school. I check for errors, pass it to the query method and print out the result. Justus, we've done every single time so far. So we come over here to our page and I'll go ahead and hit. Enter on this. You can see that we get a result that says one so successfully went through. And then let's just do a select all again for to check our table and make sure that was deleted. And you can see that record number three is now deleted. Okay, So really, really straightforward. Let's go ahead and change this to to and we will come down here will comment all this out and we'll do the exact same thing in PDO. So, as I've mentioned, this code has not changed one bit. The only thing that has changed is our query. And so this is just the exact same code that we've used before. So if we come over here, you can see again and gives us our query string. We'll do a select all here and take a look at our database and you can see now we only have one record. So we've successfully deleted record number two. So we were deleted wrecker record two and record three. One important thing to note here now is if you were to go back in and insert a new record, it would not come back with being record number two instead, The way it works is it would come back as record for and so that that kind of helps keep you from, You know, if your for some reason you're referencing those i d s in your application or just it makes it so every new record really is a new record with a new index and so forth. And so, um, let's say somebody linked to, for example, in WordPress if you have um, no, if you have oppose the way they do the u R l is they will they'll use like P uh, they'll use the You are l parameter of p equals one or P equals two to reference the i d from the database of the Post that they wanna pull. Well, let's say someone linked to you know I d equals two, and then you delete that post If you came back in and created a new record. Ah, and it became number two as your your index here your i d. That link would then referenced the new post, which isn't really How you want it to be? So the new post, any new post you'd right. You wouldn't want it to be referenced by links that are telling you, telling the person that's really something else. So ah, having it be a new i d. S having go from 1 to 4 since you deleted two and three. Makes it so that the new idea of the new post would actually before it's a whole brand new I d. So there's some reasons ultimately, the point is there some reasons why you would want to do that and why it works that way. So just something to note as you're working with it, that, um when you delete a record, any new records won't take over those i ds, they'll start new. That will move on from wherever Ah, the last one, waas. So something to keep in mind? All right, that's how to delete data from a database. Yui using my school I and PDO Thanks for watching. We'll talk to next time
34. Writing Prepared Statements In PDO and MySQLi: look back. PHP one a one This lesson. We're gonna talk about prepared statements, which is kind of, ah, hot topic and development thes days since we've moved in PHP from UN prepared statements, two prepared statements and I want to just go through briefly what this is all about in case you're not familiar. So the idea here, the reason that prepared statements were developed and that we want to use prepared statement or statements in our code has to do with SQL injection attacks and prepared statements are now the way that you defeat those. In the past, there were there were some functions in PHP that were developed. Ah, my sequel, Real Escape String and some other options. But the ultimate kind of solution became prepared statements and the reason that is the reason why you want to use these versus every other method, because there are other methods you can use to defeat injection attacks. But the reason you want to use this one is because if you write your code this way, if any new kind of threats surface in terms of injection attacks and so forth, the PHP can be updated and you don't really have to update your code. So you're kind of offloading the protection to PHP itself. And so that makes it so that again, if any new threats emerge and so forth, the the PHP code itself could be updated. And you don't really ever have to change anything inside of your coat. So it serves as kind of a final solution. It's a lot like the password hash, ah, functions or methods that have come out where PHP will actually do all the hashing and checking of passwords for you. Before that, you would you would have to write all that code yourself. Now PHP just has some functions and methods that you can call, and it does it all for you. And it just constantly updates how it does that with the algorithms and so forth so that you don't have to. As the developer, you really don't have to concern yourself too much with it. Same thing here. So we're gonna go through how to do prepared statements in both my SQL I and PDO. They are slightly different. I would say the PDO implementation is probably a little bit better, and you'll see why, as we go through here. But with that out of the way, actually, before I get into it, the point that I want to make here is use prepared statements. There's really no reason this point not to use them. They're supported in any version of PHP that you would try to run these days, and they just they handle all of the issues that will crop up when it comes to injection attacks. So use prepared statements. All right, so without out of the way, then we can go again through our code here. And so the top stuff is again pretty much the same. We have our credentials here. We have our instance sating of my ash July and we're checking for errors. So all that stuff is the same. Here is where we start to get into Ah, what's a little bit different when it comes to prepared statements. So I've said this I d variable here, toe one. This is just referencing the idea of the road that we want to work with, You know, from past lessons that we have one item in our road now And so I'm just referencing that one that we're going to work with. And of course, this is just for tutorial purposes that, based on whatever you're doing in your application, you would kind of change this up. But so that's what this is. The next line here is where we get into actually preparing the statement. And this is when we talk about prepared statements. This is actually the prepared statement, so to speak. So you'll notice that when we do this, it's a little bit different then what we've done in the past. So we first off we're passing our query. Now, instead of passing it straight to my school, I query, We're passing it to prepare, and that's gonna prepare the statement. And so we have select all from objects, which is all what we've done in the past, and then we have where I d equals. But you'll notice instead of actually putting in a wan or the I d were putting in a question mark. And so this is what's called a placeholder. And so it sends that to my SQL I and my ass school, I ducked, does what it needs to do essentially to prepare this statement, and so we're not actually passing the value directly in to the query. That's the first difference. We're putting a placeholder in there, and then we're gonna populated, and we're gonna bind parameters to it. Okay, so that's what this next line is. This next line then binds the parameter that we want to bind to this place holder. So we we we call bind Paramus and were referencing the way this works is it goes an order, just like if you had put before, when we were talking about how when you insert values and you have to put things in the proper order and this is the same way. So when you use buying Paramus, it's going to reference this prepared statement, and it's going to be an order of your question, Mark. So let's say, for example, you had something like this. This is kind of a nonsensical statement, but let's just say you had to question marks in here Que in bright buying program, you would have to reference both of those. So the first, the 1st 1 here this first D would reference this first question mark, and the second D would reference the second question mark. And let's say you would have to have another parameter here or another variable here that would reference that second question mark. And so what this does is it binds this variable to the first question mark and this variable to the second question work. And then this part of it, this d in this d is telling it whether it's ah, imager or a string, So D is first digit er imager. If these were strings, you would have SS for string or depending on what it is you may have DS whatever. But in this case, these air, both imagers. So we're gonna do D d. Okay, so that this is what actually binds these parameters, then to the placeholders. And so that's going to tell it what variable it should use and for for that placeholder. And it's going to do all of then the checking and escape being and all the stuff that that needs to be done to prevent injection attacks. It's going to do all of that stuff for you, so you don't have to do it, okay? And then from there, you really just executed. So we call statement execute, and that's gonna execute the statement and then we can get the result using get the get result method off of our statement. Object here. So again you're preparing the statement you're putting in your placeholders. You're binding the parameters to those placeholders you're executing, and then you're getting your results. The result you get back is gonna be just like before where we talked about how what you're getting is actually ah, my my sequel reference. It's not the actual what you need for PHP. So again, you still have to with this loop through and run, fetch, object on the result and and put that into an array here. And then you see down here, we're putting out the results. And that's what gets us this over here. All right, so one last time just to reiterate you, prepare your statement and put in your place holders. You bind your parameters, you actually you get the result. And then from there, it's just like any other any other query that we've done so far. Now, one thing to mention here is some people, when they start learning prepared statements, is they get kind of prepared statement happy everything has to be prepared, prepared and that they're gonna query for and that's not actually the case. Really? The only thing that you need to prepare in your in your queries is user submitted data. That's the whole point. So you see that we're actually declaring I d toe one here and then we're using it. You were preparing the statement and using that this is something that you actually, this is actually, you don't need to do this, but this is for just for tutorial purposes, so I can show you. But again, you don't really need to do this. You Onley need to do it if it's user submitted data. So if you're just running a query in your code where your there's no user submitted data in it at all, you don't need to do this. Okay? That's just something toe to keep in mind. When you're using this because there's nothing there's no chance for for data to get submitted or injected into it. It's is the code that you yourself have written. Okay, so, again, this doesn't really make sense, but I just wanna do it for ah, so I can show you. So we'll assume that this ideas is a user submitted thing, right? So that's that's getting that's using a select statement here, you'll see down here then. Now I have some other statements that we can take a look at. And so if we look at this, this one here, then we can see that we have. This gives us a little bit more of an idea of something that we might actually do here. So this would really go in the place of where we're doing. Ah, the prepare and the buying cram appear. These two lines, we close these down lines 16 and 17. If we're doing insert, we would essentially take these out and put these in. Okay. And so again, it's the same thing. We're preparing the statement. So we're gonna insert into our objects table and then the values and you'll see we're putting in, which is placeholders. So there's not any actual data that's in here. It's all just placeholders. And then the next line were binding the parameters. So we're assuming, let's we're assuming these air users some into data. So I just made up some stuff code language, official percent. So four of them to represent her four placeholders these could be if this is, ah now some sort of form that people are filling out, it could be name email from from your post or a that Pete that people submit the form. You grab this data from the Post array and it could be, you know, again name, email, address, phone number If that's what you happen to be collecting. Hey, and in that case, you absolutely want to make sure and prepare. Use prepared statements because that's user submitted data. Okay, so this is an example. Often times it's insert an update where you're going to use. You're going to use prepared statements the most, sometimes with with select queries you might sometimes with delete clear queries. Unite You might, although often times that's all done internally. There's really there's not. Oftentimes, there's not user cemented at associate with that, but insert and update, definitely you'll use them quite a bit. Okay, so that's an update there. If we take a look at our next one or that was an insert, our next one is an update, so you can see the syntax is really the same. All we're doing is replacing where we had data before we're replacing it with placeholders . So it's update. Objects set post title equals question marks some two equals Question mark 34 equals. And then we again down here bright, buying the parameters whether they're strings their managers. And then you can either put actual straight data in here or some sort of variable again. Often times this will come from some form, and these will be the elements in your post array. So you just you just put them in here and they'll be prepared and you'll be all good to go . That's all you have to dio. Okay, so then the last one is then our delete. So this is a lot like the select one up here in that this is probably not as common where you would actually use prepared statements simply because often times when you do deletes it's all done internal. But to show you use the same syntax and elite from table name objects where the I d equals and we have a placeholder and we have this idea of one up here that would go in there, we buying the parameter, and then we would go on with our execute, get result, etcetera. Whatever we want to do at that point. All right, So that is ah, prepared statements in my ask July. Now, if we come up here on, we comment that out, we can then come down here and take a look at PDO, and I'll show you how PDO is a little bit different. Okay, so again, we're going to start off in PDO like we normally do. We're also going to use this idea very your above, up here again. We're gonna use that down here. But we instance a new incidence of PDO said our attributes, we can see Ares, and then we're going to connect, and we're gonna prepare our statement again and you'll notice again. The query is all the same except for this part here. And the difference here between my SQL or my sequel and PDO is that instead of using question marks which are essentially anonymous or unnamed, parameters were using named parameters and so we're actually giving this a name. So it has colon. I d. Okay, that's the That is the big difference. And so you can see down here, then I'm actually referencing this I d. And I'm setting it to eight was really should be one. But I can name the parameter on this in this execute command here, I can name the parameter that I want to set here. So what that means is, let's say I had name in there as well as one of the parameters. Well, I could do it in any order I wanted. Okay? I don't have to do it now. I don't have to do it. Put him in this array in the order that they show up in the statement. Okay, so that makes it a little bit more convenient to use. I mean, it's not a huge deal, but because it's a named parameter, I don't have to worry about what order? I put things in this array because I'm explicitly referencing that name here. Okay, so that's the main difference between my s July and PDO. From there, you'll see here, we're gonna use this fetch, and then we use this constant this property here. Fetch object, PDO, fetch object. And that will fetch our array of objects. Were we put those objects into a loop them into an array, we print out the results, etcetera. So the rest is the same. So again, the main difference here between everything that we've done is these two lines. And then the main difference between my school I and PDO is these named parameters. So if we come down here, then we can take a look at some other examples. So insert into objects values again, this is all the same. And then the values we used our name parameters and here we're using this now statement like we did before. So we would come down to our execute method and we pass in the array of data. Then we referenced title content, name and again because it's named parameters. Order here doesn't really matter. And because we're passing in now here and we're not using a prepared statement, we don't have that down here. Of course, we look at update same thing. The code is very, very similar. Except when we get to are actual values, we put in place holders. So title put content name and were binding those parameters down here again. Ah, in the execute method with our ray same thing with delete delete from objects where I d equals placeholder in our X Q block were then giving that name parameter a value, right? So it's not too much of a difference between what you may have done in the past if you've never worked with this in the inserting values and now using prepared statements, you're all you're doing is putting placeholders in where before you were actually putting content in and then you're using in my school, I using buying Paramus and then an execute statement and M PDO, you can do it right in the execute statement within a ray here and then again, unnamed versus named parameters. That's a big difference between my school and PDO when it comes to prepared statements, so use this. It's not if you have a old application or something, it's not too hard to switch to this way of doing it. You know, you gotta go through your coat and do it a little bit, but it's not this just overwhelming thing that can't be done. So ah, definitely use this. If you're building something from scratch starting now, then you should just be doing this from the very beginning. All right, so that's prepared statements. Thanks for watching. We'll talk to next time
35. Create a Database Class: Welcome back to Beach. Be 101 in this lesson. I'm gonna talk to you a little bit about kind of solidifying everything that you've learned up to this point. And the point of this lesson is less about the code itself, because you could do this any number of 100 different ways, and it's more about the idea. And again, this this idea of not overthinking it and not recreating the wheel. And so the idea here is really to create yourself a database class and take everything that you've learned up this to this point and kind of crystallized it into a class that you can then just use over and over and over again. Or even better, in my opinion, is, instead of writing one of these from scratch yourself, use one of the many that are already out there. There's there's tons and tons of classes PHP classes for handling, hand handling, the foundational, cruddy elements that we've been talking about for interacting with the database. There's they're out there for you to use again. I think it's important for, you know, code behind all of this and how it all works. But at the same time when you get into actually building stuff for people, there's no need to recreate the wheel. No need to make a harder than it has to be on yourself. So that's ultimately what I want you to get out of this. But I'm gonna walk through this and show you how I built this one so that you could maybe get inspired about how you want to do your own so you can see appear I've instead of setting variables. I've defined some constants in here. They're just a little bit easier to work with. You have to globalize and do all that when you're working inside of a class and all these different functions. So it's really the same idea is what we were doing before. They're just defined now as Constance and then we are creating were technically see if our class already exists. So if the class doesn't exist, that's what this is right here, this DB class. Then we're going to create a new class called DB and then inside of it, we have all of our methods which we create, just like we do when we create a function in procedural in the procedural way. Except we add this this modifier to it, whether it's public or private, protected, etcetera. I don't want to get too close, too far into you talking about classes. But, ah, that's what this is. These are essentially functions inside of a class and these these become your class methods . All right, so I'm gonna give you a quick run through of this and then we'll talk about some of this stuff more in depth. So we have a number of water public functions, so these air functions that can be used outside of the class itself. So you can instance eight an instance of the class and then call that method You need a public method to be able to do that. Private methods can Onley be called within the class itself. And I'll show you that a little bit here in a second. But these So these are the ones that were mainly concerned with. The 1st 1 is connect. This is just connecting toward database like we've done 100 times. We have a simple query method. So that again we were running query before in some of the stuff that we were doing we created our own query method. We have an insert method to handle all of the inserting for us. We have an update method. If we come down here, we have a get results. So this is to get multiple rows of data and then we have get row. This is to get one row of data. We have a delete method and those are all our public ones that we would actually use when we're using this data base. And then we have these two private functions which can only be called from inside the class itself. And these are really private functions are meant to be helper functions. So you can't call these if you instance e an instance of the class, you can actually call these off of that. So down here where we're calling DB insert db update. You can't do that with these methods. These air on Lee called actually inside of the class itself, and again, they're just helpers. They're just helping us do things in other parts of our code instead of rewriting this code several times. And in fact, if we do ah, search for rough values, you notice that there's three instances of it that it's used. So it's used up here and it's used back up here as well. So that's there just again, just helper hoper methods. All right, so the main ones that matter here are actually before we do that. So the whole idea here is then, once you've built this class, if we come down here, I instance, eat a new instance of the class and then I can call, just call, get results and put a query in here. And I don't have to All the other stuff that we were doing before, where we would constantly have to rewrite the same code over and over and over again. I'll have to do that anymore. I just call this with my query and you'll see up here. I'm getting it. I'm printing it. I'm getting it right here. I refresh this. Nothing's no crazy going on here. It's just it's doing it for me. So this is the whole idea. Everything that you've learned so far can be put into a class crystallized, and then you can just use that over and over and over again. Or like I said, use one that's already out there that that exists. So this makes your coding life a heck of a lot easier. So let's talk about then how to do this. And I'm gonna go through this code. But I'm not gonna go super super in depth explaining everything because I really want you to take this as your challenge from this module is to To write yourself one of these, Even if you're gonna use another one, your challenge is to be able to write a class like this. And so I wanna I wanna leave some blanks out there for you to fill in because that's how you grow is a developer. Okay, so I'm gonna give you access to all this code, of course, but I want you to challenge yourself to try and build one of these on your own. All right. So the first function is our connect function or connect method, and you can see here all that's doing is instance, eating a new instance of my ass July passing in our credentials. And then it's just returning. That's what it's returning. So when you call this method, you'll get back an instance of my SQL I that you're connected and you can use with and you'll see if we come right down here below at the start of pretty much every one of these functions. I'm calling this method right here, and that's just to fire up my SQL I and get us going gets connected to our database and all that. So ah, we just put that into a method. And now we don't have to re type that every time we just call that method. All right, Next one is our query method. So this is just a generic method that we can use for running just generic queries. We're going to get into some specific ones, but we can call this method directly. We can call it from other functions. It's just it's the actual query part that we have we've talked about in the past. So again, we're gonna call our connect methods. We can get a new instance of my school I and then we're simply going to run whatever is passed into query. So this is you noticed this isn't prepared, right? This is Justin in generic query. So this would be something that you would use when you're what you're querying, is all done internally. This you want to use this with user submitted data? Okay, because there's no preparing going on here. There's no escaping none of that. This is all really met for just internal type stuff. And this is probably where most of you view doing more complex theory queries in the application. You're going to use the Cory method because these insert and update our and get row and get results and delete those air met for very specific things. Whereas you start getting in the more complex stuff, you'll just need a generic way So you can see all we're doing is passing this straight into Ah, what is the This is the new instance of my ask July. So now this is calling the my SQL I query method. Okay, so that could be maybe a little bit confusing, but this is our own query method inside of our class db. There's also the query method for my ask July itself that we've been using all along here that we're now referencing here and pulling put putting, passing our query into it. So we're doing everything that we did before where we were passing our queries to the quarry method, my ass. July. But now it's inside of our own quarry function. And then you can see here for the result where looping through were fetching the objects were putting that into an array, and then we're returning the results. So you remember how every time we were constantly always doing this loop? Well, now it's all in the query method. We don't have to do that. It does it itself. And all we have to do is call this method. So we re wrote it once, and now we can use it again and again and again and again. Okay, so that's generic query method. The next one is an insert method. So this is one where you gonna get in prepared statements. Like I said before in the past lessons, You know, insert is where you're gonna usually in certain updated where you're gonna usually get most of your user submitted data. So that's where you're gonna be using prepared statements heavily. So we're gonna we're going to kind of write our own way of doing that so that we can pass data to our insert method and we don't have to write all this code over and over and over again. If you look here, this starts online 23 and ends on line 60. So that's 37 lines of code roughly that we don't have to write over and over and over again , right? So what you pass into it is the table that we want to work with the data and then the format, which is gonna be for binding our parameters. OK, so the first thing we do is we're gonna check for the table or the data and see if they're not set. So if the table is not set and we don't have data past, we can't really do anything. So we're just gonna return false. Next, we're going to connect to the database, and then we are going to Ah, we're going to create our arrays that we're gonna be using for data and format, so this is no necessarily need do this. But this is the best practice so that you don't get any warnings or don't get any notices and and things like that. So we're creating a new ah, we're creating a new variable and we are casting our data and our format to array to make sure, essentially that they're a raise because we need them in a reformat. Next, we're going to build our format string. So what this means is, if you remember from, we'll go ahead and open this back up and go back to the prepared statements here. If you remember from a prepared statements, we have this street. This is actually a string right here that we pass into buying program, and it's formatted in a specific way. Well, we're doing this all dynamically, so we need to build that string with the array with what's been passed to us as an array for this format option here. Right? So we're just using implode and you'll notice there's it's, there's no spaces. There's no commas because back over here, we don't have any of that in here. OK, so it's just looping, essentially like looping through and putting them all into a string. You'll end up with S S s as the whatever's passed into this or format array here. And then I believe when I wrote this, I was using these, Uh ah. I was using these percentage signs along with it. And so I'm ripping those out of there essentially. So if we come down here when we pass in so far, insert. When we passed in the format, I'm using percentages, percentages and up here, I'm just stripping those out. You probably could just not do that. It seems like that would be a smarter way of doing it, of not using those percentage signs on here. But when I built this, for some reason, I did that. So I'm just stripping out those percentage signs here and the next. We have this list, which it's really functions, more of a language construct. But what what list does it? It's a little tricky, but essentially takes in a data that you haven't Honore and it lists. It creates, so to speak, the variables associated with that. So if if you had, that's for this is what is supposed to be the Array. Now we're past that. We have a method here. Let's say you just had an array of you had a ray that had blue, green, red and over here enlist. You put your variables were blue, green, red. It would create an array where the variable blue equals blue and the variable red equals red and green equals green. Okay, so it takes this array and essentially turns them into variables. And what you put here becomes the variable names and what's over here in the array becomes the value of those a rays. So instead of just listing them all out in a line, this makes it is this kind of a short cut? Now you'll notice that we're passing this to prep query our private function and what we're passing is our data that was passed in two to our function here. So to trace this back, we need to go back to the insert method and what was past the data that was passed, which is the second parameter. So if we go to insert right here, the first parameters the table, the second is our actual data. So post title equals abstraction, test post content, etcetera. That's the actual data that we want to insert. And that is what is getting passed to the insert method. And then we're pat, then passing that same data over to prep query. So what is prepped? Query does. Essentially, what prep Corey does is makes it so that it reconfigures it so that we can have a fields, a placeholders and a values variable. So it splits up the fields, the placeholders on the values. It helps us get prepared for a prepared statement. So if we come down to prep query, you can see here fields, placeholders, values. And then what we're doing is we're looping through. We're grabbing all the fields and putting those in one array were grabbing all the values, putting those in another array. And then we're creating a placeholders. Ah, placeholders array that is gonna input are place holders. So you can see we have equals Question mark question Mark. There's some stuff that's going on in here, but that's essentially the idea. So now we've taken our data from down here and broken it. Put all of these into in a field or a put all these into, ah, values array. And then we've created the placeholders that will need to put into our prepared statement. So again, it's just prepping the query so that we can confuse it with prepared statements because we need those things separate with prepared statements. All right, so once we've done that ah, then we're going to pretend the format onto the values array that we just created. And then we're going to prep, prepare or querying up Proper year are created for binding. So here, this is where we're actually you see, we're passing this prayer. Were actually creating are prepared statement and so you can see we're insert into the table that we got from up here. And then we now have our fields separately out. Remember, we have to put the fields in the first set of parentheses and then the values in the second set of parentheses. So that's why we needed to split them up from what we were passed in N v a r ah, in via the method that was passed nvr data parameter. Okay, so that's what this is doing now we have our placeholders, so reviews fields, we've use placeholders and you can imagine Then, down here in this, we're going to use call user funk array because you need to do this in order to for the buying cram toe work. But basically this is just like our are buying parameter statement that we had before, and then we're passing in our values here. So we're binding our values to our placeholders that we created up here now ref values This was our other private function. Essentially, you can't with prepared statements. If you try to pass something in directly, you'll get you'll get an error that says, You know, there's a pass by reference area and so you have to. Everything that you pass into a prepared statement has to be a variable. We can't pass the value in directly, so this essentially just ensures that that happens. That's been mainly the point of it. Okay, so that's what's happening up here with this. With his ref of value statement up here, make sure everything that gets passed in to bind program every value is past in a way that buying Paramjit can use. Otherwise you'll get errors and it won't work right next. Then we just execute the the statement, and we check to see if it was successful. So we see if there any affected rose. If so, then it's true. And if not, we returned false. Of course, you could return different things here if you wanted for your particular database. If there's some sort of error or you want to turn return the queer ear. What the result is, or however you want to do that. But this is just the check to see if was actually inserted, right? So that's our insert function. And again, the nice thing is, if we come down here when we call this insert function or insert method, we put in the table name, we put in an array of our data in a format that's easy for us, Toe to use makes sense, and then we pass in our array of placeholders, so it's really, really easy to use. We don't have to go and write all of that code that that we just wrote over and over and over again. We just call the method, and it's really, really simple, right next up is update. So update is really, really similar, except we need to add in these where kind of clauses. So we need to when you update your updating a specific record. So you need to specify the record so you'll notice here. A lot of this is going to be really similar, so we check for the T su C of table or data or not set. We connect to the database we cast our, ah, cast them to a raise to make sure we have a ray data. That because that's what we will be working with. We create a format just like we did before, but now we apply that same thing to the wear format. And what that is is if we come down here to our actual prepare statement, we're gonna update this table we're gonna set, and we have our placeholders in here. And then we have our wear clause. We have to actually create a where clause where I d equals one. Well, it could be that you may use this. You may pass user submitted data to this, So we're going to go ahead and and prepare that statement, even though I know in the past I said you don't necessarily have to do that, but we don't know what kind of dad is going to get past this. So we're gonna go ahead and prepared just to make make sure. So we're preparing this where I d equals, say, three. That three. That may come from user input, so we're gonna prepare it just to make sure. Right? So that's this is the start of that were again We have to pass in. So the wear format, the wears kind of the wear statement and then or the value and then the wear format is is it a string or an imager? So we have to pass that in. And so all we're doing here is for the wear format. There could be multiple. So we're imploding it. We're getting rid of the percentage signs again and then or format. Eat the format we're going to use equals r wear format. So again, we're using list to create variables out of our fields. Placeholders and values were passing that to prep query. Again, We're passing. Our data were passing in this update modifier. The reason we need that update modifiers become to prep query member this right here. If type equals update, then we need a new equals question, Mark, not just a question, mark, because it's aware statement, so it needs an equals. All right, so if we continue on where we're getting our wear claws and where values and all this set up for looping through and then we're gonna populate thes, we're gonna count, and we're gonna populate these. Okay? So we take Our are where items here that were passed into Where? And let me show you this down here just so it makes a little more sense. And our update, we have again the table, the data that we want toe to send. We have the form out of that data, and then here we have are actual. This is our wear clause. So I d equals one, except we pass it as an array and this is the format of it. Okay, So again, there could you could pass in multiple here if you want it. And that's why we're looping through and doing everything that we're doing because we don't know what's gonna get passed to it. So we're looping through that where array, which was I d ah equals one. And we're breaking apart. That a race where the field we're getting a field and a value. If it's greater than zero, then we're gonna add an and sign. So if we go to a second loop because you with a where clause you may have where I d equals one and pope and post title equals post title or whatever. So you need this and in there between your where closets. So if we have more than one in our array that we're gonna do this, add this and to it, and then we're just creating our wear cloths. So we're gonna have our field, which is I d equals question Mark. And then we have our wear value here. This is the actual, ah value that we got from there. And we're gonna then just count and loop through that until we run out of items in that particular array. Right? So that prepares our wear clause, and then we're going to again. We're preparing our format onto the values here. So we're putting our format onto the values array, were emerging that together, and then we're going to get into our prepared steaming here. So we have update our table name. We're gonna set. We have our placeholders, thes air, all the Remember if we look at here in our update Ah, are all of our you can you can do it several different ways, but, um, we have all of our in our set area here. We have just a bunch of question marks after the equal sign here. So, um, again We're just getting our placeholders in here so that we combined parameters to them. And then where and we're using our wear clause here, here, we're gonna bind the values again. So we're using this call user funk array, toe call it, buying the parameters, passing it toe ref values. And we're passing those values through we're executing, and then we're checking for successful insertion. So, um, again, I know I want to make the point that I know the first time you go through this it's probably gonna be a little bit over your head. That's okay, because A you don't necessarily have to know all this to use this and B, you don't have to write it this way. And more than anything, I want you to just I want you toe have some idea of start to try and write this yourself, and it's not gonna be easy. It's gonna be challenging. And that's the point is that you're gonna grow from this. But I do want to go through it so that as you start building this, it starts toe. Some of it will then start to make sense, you can come back and references and so forth. All right, The next one then is get results and you can see now we can start to leverage some of the other methods that we created. So we're just gonna pass in our query and then we're gonna Ah, we're going to past that to the query method, and it's really straightforward and simple. So there's, you know, this is really, really, uh, simple to use there's not a ton to it get rose the same. Except the only difference is, instead of returning all the results, we're going to just return the first result. Now, this is assuming again, this assumes that you're going to pass in a select statement that has an aware Kloss to it . So you're not going to do if you're going to do select all from, you know, select all from the table and not use the where clause you would use get results. If you're going to do select all from table where I d equals three and your so you're only getting one row, that's where you would use get row. Okay, so this is only gonna turn return the first result so you could pass in a query that selects everything in a particular table, all the roads in a particular table. But you're only going to get the first result back. So use it really? When you get results for that. Next is our delete method. So pretty straightforward. You pass on the table name and the idea of the record you want to delete that should with what we've gone through should start to make sense. So we connect to the database, we prepare our statement here, so we're going to delete from table where I d equals and then we're gonna bind the value. So it's an imager. Its i d were. And again, we're limiting what you can pass to this okay on purpose. So you can only pass one idea at a time. Well, that's on purpose. We're making sure it's an imager. Um, you can only pass you only delete by i d. So it's purposefully limited that way. Delete functions. You kind of tend to want to want to do that because there they could be scary. So we buying the parameter we execute, and we check to see if it was done successfully. So this one's really, really straightforward. This is code that we've kind of done, um, before. And so then we've gone through the rest of this. All right, So and you can see down here, then it gets really easy to use. Get Rose. Select all from objects. Delete objects. Nine insert. We have our data update, etcetera. So the again I know some of this the first time you go through it, you're like, Whoa, You know, most people are like, Whoa, what was all that? That's kind of the point. This this video's really meant more to challenge you. Ah, then it is to necessarily toe teach you absolutely every little little piece of it. I want you to to get out there and try and create this on your own and push yourself and try and stretch yourself a little bit to the point where you can you can do this. And if you can build this, then you can do just about anything out there that you would want to do with PHP. So take it as that kind of of of challenge, Um and then from there, if you want to use what you built bio means otherwise, there's all sorts of different classes out there, I think easy. SQL is probably the most popular WordPress, actually, Ah took easy SQL and kind of tweaked it for its own little version. So it runs kind of a version of Easy SQL. There's all sorts of other ones out there that use prepared statements and so forth so you can build your own just so you know how to do it. And then you can either use it or you can use one of the many others that are out there. All right, so that's abstracting a database creating a database class. Thanks for watching. We'll talk to next time.
36. Submit and HTML Form to a MySQL Database Using PHP: back to PHP 101 this lesson. I'm gonna kind of tie everything together that we've learned in this module and also tie, tie what you've learned he'll a bit to some of what we've been doing in the past are practical practical exercises with working with forms. So you can kind of start to put all of this together both in your mind and in your code base and and start to build something that you can use for getting clients getting work, etcetera. So what we're gonna do here is ah is just a simple form that's going to submit to a my my sequel database and we're gonna use my school. I we're gonna use prepared statements and so forth. And so I'm just gonna kind of walk through this and show you what we've done here. So the first thing that will do is likes to come down here to the terminal. I'm gonna go ahead and get get into, uh, my sequel, and then we're gonna use our PHP one on one database here, and then I'm gonna select all from our users table, which is a table that I created so you can see that there's right. Now that's an empty set. So there's nothing in there, so you can kind of see that Haven't prepped anything. There's nothing in there and so forth. All right, so if we come over here to the right, then what? The way this form works is all go. Just go ahead and put in my name. An email Ah, that we can just work with. And I'll go ahead and hit, submit on this and show you that what we're doing is we're submitting that of the database , and now we're displaying that data as well. So this I'm gonna show you how to submit the form, how to get the data from the form, how to put it in a database and then go back in and grab that data from the database. So it kind of gives you the full circle of what you would do, what you would probably do with a form like this. So just so you can see now that it's in the database, all do. Ah, rerun that query that select all again. And now you can see down here that we have one record in our database and That's the record that we're displaying here. And we're actually pulling this from the database. I'm not grabbing the data that was submitted in via Post, grabbing it from the database, and I'll show you that here. All right, so getting into the code who hadn't closed this out, I don't think we need that anymore. But getting into the code first thing I'm gonna come down and show you is the actual HTML form. Now, like I said, I've kind of created a standard HTML page. I put some styling in here, So in case if you want to use this in what you're doing, it styled up a little bit. Of course, you can feel free to change this or implemented into if you're using bootstrap or whatever . I'm not going to really go through the CSS code here because I want to focus on the PHP part of it. All right, so But this is just all standard kind of header stuff. So if we get down here to the actual form, then we've got a couple things going on. So I've got my form tag. My action is blank, and what's that's gonna do is it's gonna cost this form to cement right back to this same page. So this is a kind of a self submitting form. It's going to submit back to this the same girl now you could Ah, as you probably seen, you can specify the U R l here. So we could do something like process stop PHP. And so this would then send this to that You are l whatever the kind of base you earl you're working with is and then process dot PHP and you could have it processed there. You could absolutely do that. But in this case, I just want to put on one file and show you a self submitting form. So we're gonna leave it blank like this. The method is, of course, post. And then I've just given a class of form. We'll talk about this here after a bit, so just ignore this for a second. This is has to do with our display, and then you can see this is really just a pretty standard form. We have three inputs. The 1st 1 is a text input with the name of name. So this is the this name input Here. I put in a placeholder that says enter your full name. We have an email input. So this is for email address. Here. The name of it is email and again a placeholder. And then finally, are submit button and just the value of submit. So pretty straightforward there. The key thing in terms of PHP with this is really this name designation here because this is going to ultimately be in our post array. This is going to be the key. So we're gonna have a what will happen when we submit this form? Is it will? It will become a part of our post array. And in that array will have ah, all the elements that Aaron, that array will be the form fields from our form. So the key for for each element will be whatever we designated as the name so And then the value, of course, will be whatever submitted into our form. All right, so this is really in terms of peach be the biggest thing that matters. Is this name designation for each input So our keys will be name and email makes makes sense. Right? So when we again when we submit this form, then it's going to submit back to the same page, and it's going to take the data that was submitted from this form and put it into our post super global so that we can access and use it. So that sets us up then for using our PHP. So if we come up here, the first thing that we're gonna do on our PHP is checked to see if there's any data in our post array, because if there's not than none of this applies, we're not gonna send anything to database run to do any of that because there's no data to work with. So we just check to see if it's empty or not. If it's empty than we just don't do anything, we're actually just gonna show the form for them to submit data into. Of course, assuming that data was submitted, then we're going to start off and do a couple things, So the first thing that we need to do is we just need to get the data from our post array now. Ah, lot of times you'll see people do something like Ah, like this, and this is how they all kind of store their store, their data, right? So that's a typical thing that you might might see and that all that is doing is like I said, this name, this key matches up with the field that we set here for name. And of course, you would do the same thing for the email block. Now, what I'm doing here is something a little bit different. So what we're actually doing is instead of just grabbing the raw data because we're working with user submitted data instead of just grabbing it and because we have, ah, form where we we know generally what we're expecting to get. I'm actually filtering it before we ever do anything with it. And so I'm running this PHP function called filter input. And this is a designation telling it essentially what data to grab. So it's the input post eso we want. The Post data were basically grabbing the data from the post array, and the key we're grabbing is the name, because that's what we're sitting here. And we're running this through filter Santa tie string. So essentially, what that's gonna do is it's going to take the data that was submitted in this name block Ah, and is in our post terrain, and it's gonna run it through this filter sanitize string, which is essentially gonna get rid of HTML and PHP. Stuff that we know doesn't belong in the name. So it's just going to kind of do a general cleansing of this data case with that, and then it's once it's cleanses going to return it back and that cleanse data will be set as the name here, so we'll have a a filtered name. Ah, here in our for our name Variable Now for our email were essentially doing the same thing. Except if our email is not valid because the email is kind of a key piece of information, if our emails not valid, we're actually going to just kill the script right there because we want to make sure we get a valid email. So what we're doing here is we're running filter input, just like we did here. So filter input, input post. Now it's the email element, and we're running a different filter, which is validate email. So it's gonna try and validate the email address. But we're putting this in an if statement and so there's a little bit going on here. And also I walk through this one by one. So you've probably seen something like this before. If I get rid of this, email equals and what this does is it says Look, if this filter input returns false meaning, it's not a valid email address, we're just going to die. We're gonna kill the script. So that's what this part does now, adding, this email equals part. What it does is if this, whatever this filter input gets set to or whatever it returns, that's why emails gonna get set to. So if filter input, if it doesn't validate its going to return false. So if it if it doesn't valid e e mails gonna be set toe false and it doesn't matter cause we're killing the script anyway, So it becomes irrelevant if it's false, were just killing the script, and it really doesn't matter what email is sent to. But if this does validate than what filter input returns is the email address, So essentially we're checking the email address to see if it's valid, and if it is, we're setting it to this. We're setting the Eva email email variable to that value to the email address if it's valid . So that's were kind of combining that all into one. So what this these three lines will do essentially, is if if the email is valid, then it will continue. Let it let us continue processing and set this email variable to the value of the email. And if it's not valid, we're gonna kill the script. So that's what these three lines do here, right? So again, and we do that on email dress because it's it's kind of a more important piece of information and and we can we know what it how an email address should be validated. Where is the name? You know, we're just getting rid of some basic stuff. Html PHP. We know that shouldn't be the name, but a name could be, You know, from from there it could be about anything. So we can't do too heavy of a filtering here. All right, Next is is a lot of the stuff that you've been seen in this course. So we're setting our database credentials as we've always done. We're connecting to my sequel with those Ah, with those credentials creating a new instance of mice July. We're checking to see if there's any airs again. This is all the whole code that we've gone over 100 times in this course, and then we're gonna get into preparing our query. So we filtered our data, but we're still gonna use We're still going to to use a prepared statement because we're taking users cemented data. So the first line we're preparing our statement we're using an insert into were inserting into the user's table were inside during a user name and an email and then our values. Of course, we're putting in our placeholders against standard prepared statement like we've done throughout this entire course. Then we're gonna run buying program we have. Both of them are strings. We have a name and an email. So both of those air strings and they were passing in our name and our email that we set up here name and email. So we're gonna buying those parameters again and then run the execute statement so again code that you've seen a bunch inside of this course, right, so that will actually insert the data into our database. And then the next thing we're gonna do is with one nice thing about my My sequel is that you can grab the I. D of the last inserted, uh, row into the database. And so we're going to do that, and it's using this insert i d. Property off of our instance of my SQL I So we set that equal toe i d. And that gives us the road that we just submitted to the database. And what that allows us to do then is query our database for that row and so you can see we're running here. We're just running a generic query. This doesn't involve any user submitted data. I d comes back to us from my sequel, right? So it's not There's no user submitted out of here and all that, so we don't really need to do the prepared statement here. So we're just running a standard query. We're going to select the user name and the user email from the user's table where the i d . Is equal to this. I d that we the last idea that we just submitted. Okay, so that's gonna get us that's gonna allow us to get the data that was just submitted to the database were actually querying the database for that data and pulling it it back. And then, of course, we have to run. Fetch, object. Now, here, you'll note, you remember from some of the other videos we'd actually do a while loop here but here because we're on Lee, looking for one row were only querying four run one row and we only wanna work with one row . So we expect one row back and and we only wanna work with one row. We don't need to loop through. We can just run, fetch object, which is on Lee going to grab the first row of the results. So if for some reason there were three results that came there three roads that were returned from this query, it would still only give us the 1st 1 Well, that's all we want anyway. So again, we're just gonna were gonna We don't need to run through a while, loop here. So we're going to set that that object that gets returned to us that Rowe, that gets returned to us as an object as this user variable here, and the reason we're going to do that And the reason we've grabbed this data is to Teoh. Use this block here to display the data show the data that we just submitted to our users. So that's where when we come down here to this block, this is what we're doing here. So what we're doing is we're just checking to see if user is set. So again, this if this user block IHS said if it's not empty, for some reason or not false, If something didn't happen up here, then we know we can work with that data. And so we're going to then just echo out here. Here's the data you submitted, right, So we're gonna go out that line and then we're gonna echo out the data that was submitted So you can see here user, user name, user user email and all we're really doing is running this function each to mouth special characters was just essentially escaping the output. So if someone did sneak, we filtered, we filtered in the input were escaping in the output. Both tend to accomplish the same thing. But it's important to do both, because that way you kind of covered yourself. So if you look at our security here. We're filtering the data that gets input. We're running, we're using prepared statements and then we're escaping the data that gets output. So we've created several layers of protection and security here to make sure or to do everything that we can to to keep ourselves from from getting any sort of attack here. And so all HTML special characters is going to do Is this gonna is gonna escape the output of user name and use your email so that they can't slip any Java subscript code? And this helps prevent cross site scripting attacks. Right? So in html, special characters were just the were were escaping for utf eight because, um, that's what we're using in terms of what What Ah, look here are character said is utf eight So again, just escaping the data on output. And that's what ultimately echoes this John Morris and John That doesn't exist. Stockholm. All right, so that is how to build a form, submit data to the database and then grab that data back and display it in a secure way. And again, what? You could obviously of course, use that if this makes sense for a project. You, of course. You use this code. Ah, for your product or project. But more importantly, what I want you to get from this is these are the different pieces of that kind of system of a system where you would collect at a and then display it so you don't have to believe it all here in the same file, you probably put this style stuff in a different file. You could probably even have ah, separate page where your PHP code runs. Maybe even Ah, instead of having the data displayed on the same page, you could have a displayed on your on your processing page or however you want to do it. But you can pull this PHP code out. You can pull the html form out. You can pull the style sheet out and use it in your projects or whatever application you happen to be building. So all the code is kind of their the base foundation is there for you to work from, and then you can kind of pull it out and use it how you want, right? So hopefully that gives you a way forward in terms of building a form. And of course, you can add more inputs or change the inputs that your collecting here I have just name and email, but this could be a whole maybe application. Former, a big form and you just add your inputs. And then, of course, you update your database the fields in your database, you cupcake. Update your code to insert that data into the right fields. But this again gives you a foundation of code to see how it works. And then you can add to it or change it how you see fit. All right. So hopefully that get again gives you a way forward. Thanks for watching. We'll talk to next time.
37. BONUS: Michael Phoenix Interview: everybody John Morris here and welcome to an exclusive interview that I'm conducting here with the man, the myth, the legend. Mr. Mike, be Michael Phoenix in this interview. What we're gonna be going through is really Mike's journey from Ah, I guess you could say poor college student is the cliche to now Ah, a six figure coder working for, uh, I think, its fortunes One of fortune's fat 500 fastest growing companies. Ah, doing in in the tech industry and really his journey from that through various other companies. And some of the things that he's been able to do and what I really want Oh, I'm gonna grill him and pull out of is his thinking and the things that he did in In order to be able to go first, get hired, be able to move up ah, and be able to get where he's at now to kind of show you guys the steps that you Congar oh through in order to make coating your career, which is what ultimately all of us want. And hopefully we'll see. We'll see what Mike has to say on the matter. But hopefully see that you're probably already ready. You probably don't have this mountain of skills that you need to tow, learn in order to get there. A lot of it is about just going for it. So with that out of the way, I want to get into asking you some questions. Mike, first off, welcome. Thanks for for doing the interview. Thanks for having me on your platform here. All right. So I want I want to start off and just talk about your story a little bit, and so I wanna I want to start off with really? The before, so to speak. Um, you know where you were at. I know, I know, part of kind of the store in this situation, but, you know, you were I think you were about three years into your computer science degree. Two or three years into it. Um, you were working in the tech industry, um, and and you were kind of, I think, frustrated with the way things were going. So maybe you can tell us kind of how things were before what was going through your mind and what it was. Maybe you were really after. Sure. Yeah. I think the place for me to start would be when I was working at a factory, Um, and also doing freelance web design on the side, Um, and feeling like I wanted to get better at doing web design and technology in general and, um, just kind of feeling at a loss of where to go and what to turn to. So ah, I was at that factory job for, like, six months, and I realized that that was death to me. So I had to make a decision for myself in terms of going after what I felt was my future that, ah, I could actually live with. And from that point, I just made a decision to go back into school. Um, I had three years previous school back from my early late teens, early twenties, and at this time when I was working factory job, I had I had a family, so I had to figure out how toe provide for a family and all that kind of stuff. And, um, at that point just decided to go back into school and do tech and computer science and just went dove headfirst into it and, um, started doing more freelance Web design and got got into that mawr and just started to dedicate myself to Teoh going that direction, you know, not really knowing how are aware what? But just I'm going this direction and I'll figure it out along the way. So, you know, fast forward a couple of years, and now I'm here, And, um, I'm glad I made the decision that I did, and just kind of leaping out on blind faith of toe follow. What I felt was my my destiny. So this week, I'm interested because I think a lot of people might be in the same boat like where you were at confidence wise at that point, when it comes to tech skills and and also like, where you would say you were at knowledge wise in terms of and what you new programming or whatever life confidence, I would say. I mean, every client that I ever got at that point, I was stressed out, you know, because I didnt Truthfully, I didn't believe that I could fulfill what I needed fulfill toe, provide the quality of product and service that I wanted to provide. And why was that? We what? Well, I mean you because you're about a couple of years into a CS degree. Yeah, at that point, um, a lot of things, but I think one of the biggest things was just the nature of who I waas didn't really have a lot of confidence in myself, except maybe basketball. But I had spent a lot of time playing basketball, you know? So if something is new to me, or I haven't necessarily spend a lot of time doing it where I didn't have, ah, a great depth of knowledge in something I was I mean, that just my tendency is to not feel very confident If I didn't know everything about everything, you know, and that's that's how tech was for me is I didn't know everything about everything. So, like, I had a lot of question for myself, and, um, I imagine we'll probably get into how how that shifted for me. But I think the thing toe really point out was that, um not knowing is really what drove my thirst for knowledge to figure it out, which is, which was a large degree. Why I went back to school to get my computer science degree was because I wanted to know. I wanted to figure out how toe you know, how real projects ran and not just go off of what I thought and, you know, figured out how have a curriculum. I guess Teoh to give me a structure for my learning is really the primary reason that I wanted to go get my computer science degree. Now that so at some point at a point there you ended up applying IBM and I believe it was Java application developer. Yes. And so first off, what? You hadn't done it up to that point. So obviously there was something holding you back. What? What? I mean, what was it that was holding you back? Maybe it's some of what you've already talked to, but But what was this holding you back and what change that made you comfortable enough to go ahead and and put in that application? Um, at that time that I was looking for that looking for a job in particular in the tech industry, um, at a company was in my advanced algorithms class AAA advanced algorithms and data structures and going through that class and realizing that the way that I thought about things in general matched up with thinking logically, in terms of laying out an algorithm, they're laying out a data structure. It's just something natural that I realized that I do anyway. And, um, couple that with wanting to enter into the enterprise world, which was something that I felt that I wanted to do. And I knew that, um uh, starting from the bottom up. So I specifically looked for an entry level position because I knew or I guess I felt like I'm not gonna say new, but I felt that where my skill set was at was not to the level to jump into a, you know, a senior level developer position. You know, I felt like I needed to have more experience in the enterprise world working for a company to get that kind of experience. Eso I specifically looked for an entry level position and I applied at IBM. I applied I G and I played into several other places and just kept having interviews and interviews and interviews until I felt comfortable understanding the interview process as well as how to communicate where I was at in relation to the job description of the specific thing I was interviewing for. Yeah, so that's something I would be interested in. Maybe if you, as much as you can describe the the application and interview process, because from what I remember, you did do a decent number and then had some experience with these different different tech companies. So maybe describe a little bit of the application interview process, the kind of things I think people have, you know that they see they see maybe a company like Google and some of the crazy stuff that they do and some of these other companies, and assume that every single tech company is just this crazy interview process. And I'm sure there's plenty of that are. But I don't think that every single one of them is like that. So maybe just described that and shed some light on what the application interview process was like for U. S O. Every interview or every company I applied to, um started out with a phone interview to get basic ah, context for who I was. And there was the again. The positions I was looking for was entry level. So it was basic questions and they were just around. Um, you know what is object oriented programming? Just laying out the differences of that relational databases. All basic things that you wouldn't need to understand. Just getting into coding in general. So it wasn't anything out of the ordinary, and then from there, um, passing those went on to a deeper level tech as well as a deeper personal. So there was, um, both who I was as a person as well as who I was as a developer, um, and that the phone interviews let into on site interviews. Um, and it was at at IBM that I had my on site interview, And, um, at that point, I realized that that that's where I wanted to be because, um, one and Java was a language that I enjoyed coating in, um, as opposed to C plus plus. So I that just felt natural for me. So but still, the the technical aspect of that on site interview, it was still very basic, you know, um, basically, right out right out of class, right out. Ah, abstract class. Right. I'll just basically right out all these things on the board of what you understand or how How you understand how to code these things. So it was still very basic, but it was That's what I was looking for because that's where I felt I was at and wanted for myself, which, I think is, is the important thing to really know is what? What are you looking for as a developer? What do you want to be involved in? What are the things that you want to build to understand? I'm going to be spending, you know, eight, at least eight hours of my day thinking about these things. Probably more than that being at home, still thinking through the logic of of ah, function or whatever, you know. So it's what do I want to engage myself in on this? Was that? So now, uh, what would you say? How much or what do you think? Your skill level cause you got hired at IBM is a job application of over How much java would you say? You knew at that point I had just taken a class on it at school. So, you know, maybe eight weeks worth of reading through a book and doing eight assignments and maybe reading up on like the state of Java in terms of where it's at an enterprise world. How many? How many job applications had you written at that point? Zero. How many job applications have you written to date so far that a zero interesting. So I mean, I'm not trying toe do mean or anything, but it's interesting because I think a lot of developers think that they have to be the master coder. They have to know everything now. Eight weeks at at colleges. It's not nothing through but, you know, having been in the tech industry for over 11 years now, you know, I know. And you having been in a few years now, know that eight weeks especially, You know, if you're not not every day of those eight weeks has spent coding actually coding in the language. It's not a super deep knowledge. It za kind of a cursory knowledge. Yet you were still able to get hired, right? So ah, and maybe you can maybe share your thoughts on this notion that you have to be this super genius code or in order to get hired. I So if if my view is that if you're looking for a senior level position. Yes. You're gonna have to know how to do. Ah, complex technical things, right? Sure, sure. But I guess what I'm specifically talking to I don't mean to cut you off A lot of the people that I think would be listening to this would be people who are in another job in a different industry, rates that they don't like. And they simply want doubt. Yep. So that's why that's why I say specifically senior level position. Because if you're looking for a senior level position, you've been in the industry to a certain extent toe understand a deeper aspect of the technology as well as how projects run in general. But if you're not, that I understand that you're essentially entry level, right? That's where you're entering into this, um, this world from So I I say I say that to say if you're not senior, you know that. So don't expect that look for jobs that are not senior, you know, go for those jobs where you know you can fit into. And then at that point, all you have to have is a basic understanding, the but the key thing to think because I've taken on the role of mentoring junior level developers. Right. So the thing I look for is not deep technical knowledge. I look for an aptitude of learning quickly and a willingness to learn themselves. Right, Because if I have to spend my time teaching someone everything that they need to know, I I'm gonna do it myself. I'm not going to spend the time teaching someone everything. I just do it myself. So it sounds like it sounds like maybe what you're saying that for someone? Oh, who's in a position where they're not in a tech industry And maybe they're, you know, in a corporate job. Or maybe they're in some service job or whatever, and they want to get out of that. They have a job that they really don't like, and they want to get out of that. They're interested in technology, and they want to get into technology that what's important for someone applying for an entry level position like that, who you know, a lot of people you find a lot of entry level positions in the tech field are comparable toe, maybe some higher level salary wise jobs in other industries, Absolutely. So they could switch over to an entry level job and make the same, or maybe even more than where they're at now. But what what? What's important isn't the skill set necessarily, but the personality, the mentality, it's the other things. Reliability, work, FAA, all the things that you would normally think of in any other job. But for some reason, when we get into the tech industry, people tend to think that all that matters is the skill set. No, let me give you an example. So if someone, if I'm, if I'm entering, say, two different people, I give them the same thing. Let's say I give them a piece of sequel and it is maybe 30 lines long. There's several sub queries with some joins, and I say here, this is this could be used to do to pull this data out in such a way and have it structured in certain format. Take it, go do with it and come back to me with what you got. If a month later, one comes back to me and says, now what is this again for? And the other comes back to me and says, Hey, I noticed that doing this. It does this and it comes out with this result. If I change this over here, does this thing the person who comes back to me and says, Hey, I'm this Can you tell me what this does again? I'm gonna be like, Okay, it looks like, um, you didn't really run this. You didn't study what needs to be studied. Not really applying your thinking to this code, whereas the other person, if they come back to me saying asking questions, questions into the deeper aspects of it, telling me what they what, they tested, what they experimented with. I'm gonna go with that person and say, I'm gonna spend my time teaching this person things because that person is the person who can I can give them something small. They're gonna take it and expand on and come back with questions that are going deep in their thinking. So that that's what I mean when I say if I if I have to spend all my time teaching someone everything I'm not I'm not gonna do that. I will help to guide someone's thinking about it, but they have to take the only their initiative and go and run with it and the people who are more willing to do that, I'm more willing to spend the time with to say, Hey, this is kind of where is doing This is where it's at. This is how you can shape it, form it, all that kind of stuff, that willingness in the aptitude to be self managing like that really is the biggest piece . And that's everybody that I work with and everybody that I talked to about this specific thing. That's that's the key thing they look for. Remember that my first project manager, I got onto working for a fortune. If I was on a project and the project was for, Ah, at that time they were in the top. The Fortune 20 right? So there are high level tech company with billions of dollars of revenue. This was my first project, and I was doing I was the lead as a data migration, and this is the first time I had done data migration and he, my project manager, um, I was actually Q A lead at the time and he said, Hey, data migration person left you want to do this. And I was like, Yeah, sure, why not? And like two or three weeks later, he was talking to me about that and how I how I just answered and said Yes. And I said, The way I feel is that I don't necessarily know everything about data migration at that time. I didn't I do now, so I like to think. But I didn't know anything. I had, like, an hour introduction to it with this specific system, and, um, I told him at that time and I said, I don't know, but I will learn. I will figure it out right? So that that's just the way I approach things. And he said back to me at that time, Yeah, that's those are the type of people that I look for as a project manager to bring onto my project, right? I don't care if you don't know this thing. I care that if you are willing to learn it and go after and get it done. So this is This is a project manager who's been working at IBM for years on several high level projects like this is what he's saying to me. He looks where people who have the willingness to learn what they need to learn to figure it out, to dive in and go. So, like, you can take that for what you will. But that's just kind of the nature of this industry is, Yeah, you have to figure things out. Well, the reality is is that I mean, this industry is changing so much that if you're not a dedicated learner, no, even if you know, if you have the skill set today, couple of years from now, that's gonna be, at the very least, outdated if not completely off. So but I want to get into I really want to get into, um, the interview process for a little bit here and talk about, you know, the I want to try and give the people listening, some some tips or some strategies on going through the interview. Maybe some things that you know that you did. And one of the things I talk about all the time, um, is the fact of the the reality. And I've done some videos where have done statistics on this that don't Most companies aren't very good at hiring. There's There's high percentages of bad things happening to companies as a result of bad hires, and it's year after year after year. They think they have these right processes, but they really have a hard time finding the right people are a good fit for the culture of the position and so forth. Example. I always uses a company that hired me back toe work for them when I think five or six months earlier I had walked out on them mid shift of of of one of my shifts of working for them. I had walked out and quit and then six months later they were hiring me back and only hired me back behind me back for a management position now. And this was no small company. This was, Ah, a company that was at that time in the Fortune 500 I believe, least ah, and it was considered one of the 100 best companies to work for a big company. I think a couple $1,000,000,000 in revenue at the time, huge company who should know better. But there's the uh said there's the secret they have that they don't want you to know and they're all the hoops that they jumped through in the interview process is about covering this up, which is the fact that they don't know what the hell they're doing. And they need you more than you need them most. These companies air starving four talent and and so and they're having a hard time finding it all of that to say that I believe when you go into an interview and pretty much any company, you as the interviewee are at an advantage. It's just most people don't know that, right? And so they go in nervous, and they flub it up and make mistakes that ultimately end up costing jobs. So all that to say, I'm interested to know on your perspective, having gone through that process again with several different larger companies. Tech companies, maybe some some what? Your idea of the interview process and some tips that you can give her strategies that you used? Or that you learned going through all those interviews that help you to be a more competent interviewer because the reason specific when I ask you is because a yes, you got hired. But I know the way IBM worked. You had to continually get hired to be on projects. And then you able to get hired at another company, and you've been able to really leverage your position to to increase your status and ultimately your pain so forth. So I'm interested in the strategies you've learned specifically for interviewing for tech position. Um, I come from the position of selling myself. I'm literally think of it as I'm marketing myself going into the interview during the interview, and any time that I'm talking to anybody in that process, I'm continually thinking about how I'm leveraging my way, that I'm communicating to sell myself as as an individual as an employee. And one of the things that I very specifically make sure I do is to put them on the spot toe, ask, answer very direct. And, um, I guess hard questions to say, just like you said, they need me. So I'm going to make sure that I put them on the spot to answer a question to make sure that it's known that Look, I get what's going on here, and I'm not just going to come here and work because I'm desperate. I'm going to come here and work because I want to be here. And I didn't know from you that this is a good company to work for, right? So I have. I think of it this way. I have very high standards, and I make sure that who the people I engage with are also on that same level, or at least willing to come to to meet me at that level in some way, shape or form. And that's Ah, that's a really strong frame, right? That's a way of framing the situation and interacting within that that is a very you're very clear on it. And that's I guess, ultimately, that's that's kind of what I try to point people to is that that's the frame of mind that you wanna have going in. It also happens to be that 99.9 of the percent of the time. It's also true. Yeah, yeah, yeah, There's an example I think of, um I was so I left IBM. I went to different company, and then, um, IBM one wanted me to come back, and there's several different ways to do projects. One is called staff augmentation, where as, um as a vendor who develops things. If you're on a project that stuff staff augmentation, you don't actually own the project process. The client owns the project process. So Project Manager all project management type activities is a client own thing. And I'm just They're kind of in their process. Yeah, you're augmenting their staff, right? Exactly. So they IBM wanted me to come back for a particular project. And, um what through it got through it all that kind of stuff. And one of the questions I asked back to them is, Is this project staff augmentation? And they said yes. And I said, That's a hesitation point for me because I don't like that type of project because I don't have as much ownership, which it also means that I don't have as much responsibility. But I also don't have as much leverage to be able to say this is how it needs to be done. This is the way it's going to be done, and this part of the project that I'm in charge of this is the way we're moving, you know, because I own that. Then if it's stack staff, augmentation is more of Lake. Here's your recommendation Here's your suggestion, You know, Here's A B and C. You can do it this way in that way, but the choice is opening up to you, you know. But if I own it, I own it. We go my direction, you know that. That's the type of situation I want to be in. So I put them on the spot in that way, you know, and it is a very strong position, but that's the That's the thing that many people look for. Is someone to say, Hey, this guy, he's gonna take ownership like he's He's not someone who's gonna back down or he's not gonna be all willy nilly about stuff. He's gonna grab it and go with it on. And that's just the That's the nature of the way that I try to approach all conversations, whether it's for a raise, talking with a client about the way I think something needs to be done. Just if it's something that I have to do with that I'm taking action in, and I feel strongly about the way it needs to be done, that's the way I'm going to stand up, and I'm gonna speak that and if it goes the other way. Great. But my my concerns are gonna be known to be recognized. I'd be interested to know, because again, you know, having the initial hired IBM having worked for where you work now, Um and then the different all the applying that you had to do at all the different positions and so forth when it came to, you know, say, IBM or G or the company work for now, um, and then the different projects that you work for any IBM, what was your like? How much research would you do on a particular company or particular project beforehand? Before the interview to prep, I didn't know what you were going in Quite a bit of research beforehand to understand the company itself and then based on the job description in all the information that I could gather around a particular job that was applying for just to get as much context that I could beforehand going in to be able to have an informed conversation about it, to be able to inform my questions toe to them. The people were interviewing me. So I think that's the most important thing. Some people I think some people get frozen up in interviews to be able to ask questions, you know? So do you have any questions for us? Oh, no. I think I'm good that like that for me. If I'm interviewing someone, that's kind of like next. You know, I want them to ask me questions. I want them to be very forthcoming in where their thoughts are on the process, because I want them to be able to speak up in a project where there's a concern that they're they're facing, where something that they feel is important. I need them to be able to speak up because it is a team like I can't do it all myself on the team, like they need to be able to see that from them. So that point in the interview, where they're asking questions, all the research that I do going into an interview. If I'm fine with one being interviewed, it's to be able to ask those questions to say, Hey, this, like I'm not just sitting here hoping you'll give me a job. I'm here researching. I want to Nome, or can you tell me more? If you can't can you direct me to someone who can tell me more. So it it it puts the it takes the ball out of my court and puts it into them To say you need to take an action now. Yeah, and I think also, you know, four for applying initially for company necessarily applying to be added to projects kind like IBM does it with him. But initially applying to accompany this is also an area, whereas the interview interviewee, you have a massive advantage. And the reason is is when you apply, what they have about you is maybe a one or two page resume lists, um, skills and qualifications, some job history, maybe some references. Most companies, I mean, you know, may be really important positions, especially entry level positions, that they're probably not gonna call the references or talk to them. So they have a very limited amount of information on them. Whereas you, on the other hand, this is a public. I mean, for example, IBM, which a huge company it's been around for however long it has a long history. It has website full of pages of information about who they are, is a company. Their company history, their vision, their mission, their value statements, their culture like they give you. You have a volumes and volumes and volumes of information on these companies available to you, and they have a very small sliver of who you are, which is what the interview process is about. So it puts you at a huge advantage to be able to what I like to do. But I'll let you maybe speak toe some of this, too. But what I like to do is really understand. I believe any company almost really any position. There are thresh, their skill set thresholds. So, for example, an entry level job. The skill set threshold is really, really low. It's almost really just being willing, knowing basic, just concepts of of the tech that you're applying for. You get into senior level positions than you have a threshold. But what's going to decide who gets hired and who doesn't between all the people applying isn't the skill set. It's the culture fit. It's how well you fit with their culture. And that's where you have the huge advantage because you can know beforehand what the culture is, and you can answer questions in a certain way to signal it's almost like a beneficial form of virtue, signalling that you are a good fit for them culturally. And those are the things I've seen it. This is my fact. The job that I got hired back, that I had no business getting hired back because I walked out this. That's how that's a big part of how I got hired back. It was because I could see, as I was giving answers, the different people in the room, and I mean, I went through. I did phone interview, I did that. I went in for in person interviews and it was three different interviews want Two of them were one on one and then the 3rd 1 that was with a group of like, 10 people. It was very intimidating process and everybody I talked to, The questions were all kind of similar. They wanted toe have everybody's input on on me as a person. Everybody I talked to when you do that virtue signalling I could see their eyes flicker just a little bit. They were trying to hide it, but that could see them flinch. So anyway, I think you have a huge advantage because culture is what they really think about most. And you can know an answer questions that signal that you are on this in line with their culture. Yeah, Yeah, that's it's actually something that I did, um, in the, uh I forget what they call it, but is the person interviewing a personal interview or they ask questions about my history ? You know how I think about things this life in general type stuff. And, um, I did that I researched who IBM was as a company and what they kind of stand for. And I learned that they were very progressive in terms of, um, you know, helping out their employees, helping out communities, that they're in all those kinds of things. And, um, I asked several questions in relation to that in terms of what? That particular Because it was in Lansing at the time and what that particular group of people did. There was, like a group of 400 ish people like how they gave back to the community to get an understanding of how they're actually living up to the values that they espouse to write, you know, because that to me is saying to me that's communicating to them that not only do I understand what your values are, I'm asking how you actually implement those values as a company. It's important to me that you're living up to what you say you value exactly. And then she she responded Back to me and, um, a little bit later, she said it. I just I feel like you would be a really good IBM er, you know? Yeah, and that that's a that's Ah, word that is culture to IBM is being an IBM ER, you know, and that's like paying attention to those little things and speaking into them and asking about them and like extrapolating on them in the interview process, helps to gain Teoh build that rapport between you and the interview. Yeah, this goes back to the whole idea that people ultimately make decisions emotionally, and they justify him with logic. And so I think if there's one thing that people could get about about facing interviews, it's that is that when you go into the interview, it's about making the person interviewing you feel like you would be a good hire, not look at the piece of paper and check block Juan and block to block three in Block four . That's that's not what they're doing. They're trying to get a feel for you, and that's an emotional thing. And there are certain things that you conduce you again like this virtue, signalling and understanding culture and, like you said, flipping, flipping the frame and putting the onus on them a little bit in a in a confrontational but just with your frame that that can make them feel like you're not the regular person applying. I don't know how many interviews I've come out of where I talked to the people later, and they're like how we really think that you could be a rock star at this company and a lot of them I didn't It wasn't I didn't have the skill set like again. The company I got hired back to me and I always go back to it because I never should have got hired there, you to get hired. They're not only had I walked out, but also they require that you had a four year degree and it wasn't four year degree or equivalent experience. It was a four year degree. I didn't have a four year degree. Yeah, so there were all sorts of things that should have been red flags for them logically. But because I went through the inter pro process interview process knowing what to do and how to make them feel emotionally, they overlooked it. All right. That's the same thing that happened that I am at that time that I interviewed. I hadn't actually graduated at that point, and that's that was one of the things that they required was bachelor's degree, some computer science or something equivalent to that. I didn't have actually have it. I was in the process of it. I still had, like, about a year left to get my degree, but because of how I presented myself and the way in which I basically owned my presence in the interview that communicated to them that not only did I understand what was going on at that time, but I could regardless of the fact that I didn't have the paper, I still had the level of confidence to be in that room and say, Hey, I believe in myself to be able to do this, you know, and that's also one of the reasons why and look for an entry level job is because that's where I felt I was at the time. You know that? That's I guess that's the one of the other really biggest things to Key in on, I believe, is that, um, you have to know what you want in a job and then just go after that. You know, if you if you if you're not sure that you what you're looking for, whether it's front and Web development databases, mainframes this out of the other. If you don't know what you want and you just want to be involved in tech, that's fine. Go apply at every entry level tech company that you can possibly find and just go through the interviews to go through. The interviews go through the interviews if you get rejected, fine. Great. I got rejected, too, but learn something from everyone that you do make it a point toe, learn something to get better at it, which is ultimately the point that I'm trying to communicate is that you have to figure it out as you go. And that's that's not only the nature of tech But in my opinion, it's a an effective way to live life in the sense that always be improving upon where you're at. Yeah, All right. So now I want a transition into kind of after the interview and after the higher, because I think your story is interesting because it's only been I mean, how long? How long ago was it that you first got hired at IBM in June of 2014? 13? Right, So it's been just over three years.
38. Next Steps: I'm honest there's no more. So online.com, so little housekeeping to finish up this course. If you haven't yet, be sure to head on over to the class area. There is a class section for some, some steps for you to walk through for this course. So be sure to head over in that it's under the discussion in Projects tab that you'll see on the course. Also, if you head over to my profile, be sure to give me a follow on my profile here so you'll be notified when I release new courses. And I also have an ongoing sort of weekly podcast style course called Let's Talk freelance. So if you would like to have sort of access to ongoing training regarding freelancing and online business and so forth. Be sure to check out that. Let's talk freelance course as well. And finally, I do have a daily tips newsletter on my website at John Morris online.com. If you head over there, you can sign up to that mailing list. You will also be put into my own, my very own mobile app, or you'll get access to over 78 hours of free content at the time of this recording related to freelance and so forth as well. So if you're interested in that, BD sure to check that out as well. Again, that's John Morris online.com. All right. Thank you for taking the class. If you enjoyed it, I appreciate you for You. Leave me a review and we'll see you in the next course.