The Beginner's Guide to PHP Mastery | John Morris | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

The Beginner's Guide to PHP Mastery

teacher avatar John Morris, I help freelancers get clients.

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Introduction PHP 101


    • 2.

      What Software Do I Need?


    • 3.

      What IS PHP?


    • 4.

      Hello World


    • 5.

      PHP Variables


    • 6.

      PHP Arrays


    • 7.

      If, Else and Switch


    • 8.

      For and Foreach Loops


    • 9.

      While Loops


    • 10.



    • 11.



    • 12.

      How to Send Email With PHP


    • 13.

      Create a PHP Contact Form


    • 14.

      PHP Operators


    • 15.

      Read, Write and Append Files


    • 16.

      Create Folders


    • 17.

      PHP Sessions


    • 18.

      PHP Cookies


    • 19.

      Writing Custom PHP Functions


    • 20.



    • 21.



    • 22.



    • 23.

      File Get Contents


    • 24.

      How to Upload Files In PHP


    • 25.

      Create a Multi-Page Form Using PHP Sessions


    • 26.

      Designing Your Database and Object Model


    • 27.

      Structure Your Database In PHPMyAdmin


    • 28.

      Create Tables Dynamically In PHP


    • 29.

      Connect to a MySQL Database With PDO and MySQLi


    • 30.

      Read Data From a MySQL Database With PDO and MySQLi


    • 31.

      Create Data In a MySQL Database With PDO and MySQLi


    • 32.

      Update Data In a MySQL Database With PDO and MySQLi


    • 33.

      Delete Data In a MySQL Database With PDO and MySQLi


    • 34.

      Writing Prepared Statements In PDO and MySQLi


    • 35.

      Create a Database Class


    • 36.

      Submit and HTML Form to a MySQL Database Using PHP


    • 37.

      BONUS: Michael Phoenix Interview


    • 38.

      Next Steps


  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

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

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

And, PHP 101 was born.

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

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

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

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

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

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

Meet Your Teacher

Teacher Profile Image

John Morris

I help freelancers get clients.


Click here to start this class

See full profile

Level: Beginner

Class Ratings

Expectations Met?
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


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