PHP For Beginners | 3+ Hour Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on guys welcome to my php crash course for beginners so i know i have a huge javascript audience but i've been getting back into laravel lately and i'm planning some content around that so i wanted to start with uh just a basic fundamentals php course because it's been about five or six years since my last one so this is for anyone that is looking to learn php or for those of you that already know it that want a refresher so we're going to start with the absolute basics and create a sandbox with different files focusing on different aspects of php so that you can learn all the fundamentals of the language and the syntax after that we'll build a small feedback application that will connect to a mysql database and allow us to add feedback as well as read it and display it from the database so there's a github repo in the description i'll have all the time stamps down there as well if you want to skip around so let's go ahead and get into it [Music] this php course is sponsored by lenode lenode is one of the most reputable cloud companies and can be used to host any type of project many cloud hosting companies can get really expensive really quick and one great thing about lenode is their monthly caps which keeps your bill predictable for example the base plan is three cents per hour with a monthly cap of twenty dollars so you never end up racking unexpected charges their control panel makes it easy to deploy and manage your projects you have complete freedom to install anything you want customize your production environment and also get 24 7 email and phone support so if you want to try out lenode absolutely free i'm giving out a link with 100 60 day credit you can visit leno.com traversie or click the link in the description all right so before we get into setting up our environment and learning php i want to talk a little bit about what it is and and how it works so php is a general purpose scripting language geared toward web development so php doesn't run or or get processed in the browser like javascript does it run server side just like python or csharp or any other language now as i said it is geared towards web development it's important to mention that it is used for other things as well but i think that the reason it's used mostly for web development is because of its practicality php is interpreted it doesn't have to be compiled it can be written alongside html using php tags you can load dot php files in the browser to make a request from the server i'll explain more about that in the next slide and it's often used in combination with relational databases such as mysql and we'll learn about that later on as well php is also relatively easy to learn when you compare it to other languages it was the first language that i learned or at least learned enough to actually build anything of value so let's talk a little bit more about how it works as i said php is an interpreted server side language so the php code that you write is processed on the server you can't just write php and put the file on your desktop and open it with chrome or firefox and have it work you can do that with html in css and javascript because those are client-side technologies for php to work on your local machine you have to install php itself along with some kind of web server like apache or nginx so this image here i found on phptutorial.net and i think it's simple enough to understand and it shows what happens when we use php so we can put a url in the address bar with a dot php extension just like we would an html file and then what happens is the browser makes an http request to the server which is running php which then processes all of the the code within your php tags in that file and then it sends back the processed html to be viewed in the browser on the client so it's important to know that php code that you write is not processed or interpreted by anything in the browser it all happens server side now to start developing php on your machine you need to set it up so that your php code can be interpreted and ran and there's a few ways to do that php is often associated with the lamp stack which stands for linux which is an operating system apache which is the web server software mysql which is a database and of course php now when we say linux we're usually talking about your production environment when you buy hosting whether it's cloud hosting or like cpanel or something like that it's usually linux that is that it's running on um there are windows servers but most of the time you're dealing with linux however for development you can use whatever you like so i i'm on a mac windows is fine i just i feel like for beginners when they hear lamp stack and they hear linux they they think that that means they have to use linux which is very far from the truth um now things you need such as php itself uh apache a database like mysql you can install these separately on your system but what i would suggest for for most beginners are software suites like zamp or mamp these are our suites that include everything that you need such as php apache mysql it includes it all in one place one installation and zamp is what i would suggest for beginners the x stands for cross platform and then you have apache mysql php and then perl which is another language completely so you also have mamp and wamp which are similar for mac and windows although i believe those are crafts uh cross platform as well um and you can also use like docker you can use containerization but i definitely wouldn't recommend that for beginners all right and then when you're ready you can set up your production environment which will most likely be a server running some linux distro that has apache installed on it and everything you need but i'm not going to get into deployment in this in this particular video alright so you may have seen people hate on php especially online or probably only online um if i had to describe it in one word why that happens i would say ignorance i think most of the people that hate on it either haven't used it or just want to sound smart or cool by jumping on the bandwagon or are there people that maybe used it a long time ago around version four or so and php is now on version 8 and has made a ton of improvements so my advice would be to ignore those people um don't don't let what other people say get in the way of what you want to learn if you want to learn php then i would highly recommend that you do that and the people that say that it usually seems like it's it's beginners that don't know much and they just want to sound like they're superior or it's developers that have only worked on teams at large companies and failed to see the real benefits of php because working at a large company is not the only way to go when it comes to web development php is what i like to call the blue collar language because it's used a lot by freelancers and small businesses people that that that need to get projects out very quickly for clients you know i have a freelancing background so i started with php i actually learned it before i even learned javascript and before node.js was even a thing now there's two really big reasons to learn php in my opinion and that's wordpress and laravel and they're two very different things but if you know both of these you can build almost anything so wordpress is a content management system built on php and it's extremely popular especially in the world of freelancing so clients love to be able to log in and edit their content and wordpress gives them that ability it's not like cutting edge when it comes to web technologies when it comes to like performance but the truth of the matter is in many situations that isn't really important that's why it's great for small businesses that don't need to handle millions of users every day where wordpress does shine is in practicality and development speed you can create things really fast so if you have like a flower shop where you're building a website maybe they wanna they wanna sell a couple products on their website maybe they wanna blog wordpress is a great fit for that and it can also be used as a headless content management system where you can build an api with it and then have whatever you want on the front end if you want to use react or some other javascript framework and then laravel is extremely pop an extremely powerful framework for building apps and apis so for larger and more customized projects laravel has you covered in that area and some people say php is a very ugly language because of its syntax but even people that say that that hate php still seem to have respect for laravel as a framework because it's very full-featured it's very elegant if you're interested in using front-end frameworks like react then you can build your back-end api with laravel and use whatever you want on the front end so if you do learn php there's a huge chance that you'll be working with at least one of these depending on what you want to build all right so lastly i just want to show you what you'll learn and what we'll be doing for the next three hours or so so the github repo is in the description it'll include all of the code that we write including the sandbox code and the feedback project and remember this is a beginner's course so if you already know php you're probably going to know most of this stuff but a refresher of course never hurts but that's it let's go ahead and get into it alright guys so i have the php crash github repository link in the description that has all the files that we'll be creating and go everything that we'll be going over so it's basically a sandbox for different files to show you different parts of php and then at the end we'll build this little feedback project which will allow us to work fetch data from a mysql database and submit data as well all right and then in the starter files folder is the same files but they're pretty much empty aside from just some comments to kind of help you out um but yeah so if you want to get the starter files you can do that or you can just create the files as we as we move along so as far as php goes as i said in the slides you can't just create a dot php file and just run it from your desktop it's not going to work like that because php is a server side language so you need a web server you also have to have php installed on your system and if you want to use a database like mysql you have to have that installed as well so you could install everything separately and configure it but if you're relatively new to this i would definitely recommend using a software suite like xampp so that's what we're going to use it's xampp and it basically gives you everything you need such as an apache web server php mysql it also gives you a tool called phpmyadmin so that you can manage your database in a graphical user interface in the browser all right there's other other ones as well there's mamp and wamp for mac and windows but i honestly don't really like those ones i've used this for well over a decade uh and then you know once you advance and you you learn more you can start to get into like docker and and create containers and all that but for beginners i think zamp is the best way to get set up on windows or mac if you're on linux i suspect you know what you're doing anyway all right so just download and install it and if you already have php on your system then that's absolutely fine but if you don't i'd suggest installing this once you open it up you're going to see a panel similar to this it's going to look different on windows if you're on mac you're just going to want to click start to start it up and then under services you want to start up your apache server and your mysql database just click start if you're on windows you should see apache in mysql i believe you'll see like some red x's over here just click those red x's to start up to start up those services all right and then if you're on mac you want to go to this network tab and localhost 8080 just click enable so that's going to be your route on windows i believe it's just localhost you don't you don't use port 8080. and then under volumes you'll see here on mac we have this opt lamp folder that's going to be our server folder if you're on windows it's going to be in your c drive and it's going to be in you'll have a folder called xampp xampp so i'm going to click on explore here and it's going to show us a bunch of folders here so all of our apache stuff all of our mysql stuff now the main folder you're going to want to focus on is this htdocs this is your your server document root so any any local websites or projects that you create will go in this folder all right and if you're on windows it's going to be in your c drive and then zamp and then htdocs so if we open that up you can see that xampp does have some default stuff in here in fact if we go to the browser and we go to http localhost now on windows it's just going to be localhost on mac it's going to be port 8080 by default and you're just going to see i actually have this scaled up right now but you're just going to see this welcome page which is actually at localhost slash dashboard it redirects us there by default now from within that htdocs folder if we create a new folder here we can name it whatever we want i'm going to call it phpcrash so if i create a folder in htdocs i should now be able to go to again localhost on windows localhost 8080 on mac and then slash whatever that folder name in this case php crash now it's showing me this because obviously i don't i have nothing in here so what i'm going to do is open up this php crash folder in my text editor which i'm using vs code and i'm going to create a file in here called index.php and if i come back over here and reload we should just see just a blank white screen here alright so it's actually loading this index.php file in fact if i just type in hello and reload you should now see that and i'm going to just scale this back up so what's happening here is it's it's loading the index file just like it would if it were an index.html file uh when you have an index.html that's basically your home page you don't need to specify the file explicitly so that's what it's doing it's loading this and php files can have you can have plain text you can have html so if i were to add an h1 here you'll see that that will parse as an html heading and then any php that i write is going to go within php tags which look like this so the opening tag is going to be an angle bracket question mark php ending is question mark and right angle bracket and here i could write some php code like let's say echo hello and if i come back over here and reload then you should see that it should just say hello so now you know that that php is actually getting parsed on your system all right so before we get started with the code there's one more thing i want to do this is optional for you guys but i want to have it auto reload so basically whenever i save a file i want it to reload automatically so i don't have to click reload and this is again completely optional i just want to do it because it's a tutorial uh so there's a couple things we have to do you're probably if you use vs code you're probably familiar with the live server extension all right so you do want to install that however to use it with php there's a few extra steps we need to do so first would be to install that then we also need the php server extension which is this right here so you would install that as well and then we also need the php intellifence uh yeah this right here intellifence extension so you want to get those installed and then you also need to have the live server extension chrome or firefox extension so if you search for live server i'm on chrome so that's obviously what i would get so you want to install this i already have it installed so once you install that you can click up here and you need to pass in two things here so your actual server address is going to be this here if you called that folder php crash again on windows you probably i think it's just localhost slash and then the live server address by default is port 5500. so you just want to set that up click apply all right and then once you do that we should be able to start up live server by going to you can either right click and say where is it usually it's usually you have a live server command but you can also click right here where it says go live and that's going to open up 5500 in your browser you can just close that and then you just need to start up the php server so right here right click php server project that opens on 3000 you can close that up and now we i think we have to give this one reload and then i should be able to just save and there we go so now i can put anything in here save and now i have auto reload with my php files all right now if you guys want you can like i said before you can get the starter files so if we go right here starter files these are all the files i'll be creating and they're pretty much empty aside from some comments so if you want to either clone the repository or if you're not that familiar with git you can just go to download the zip you can get those if you want but i'm just going to create them as we go along so i'm just going to rename this index.php to 0 1 underscore output dot php and now this is going to break because there's no longer an index so i'm going to go to localhost phpcrash slash01output.php all right i'm actually going to make this just a little bit smaller okay so let's go ahead and i'm just going to close the sidebar up for now so we have our opening php tag and we are just echoing out a string into the browser here we have our closing php tag now before we get into the the different output constructs and functions i just want to talk about the tags so you always need the opening tag the closing one is actually optional unless you have something below it so if i go ahead and save that that's absolutely fine and usually developers will omit the the ending tag unless they need it now where you do need it is if you're going to have some html down here something like this because a lot of times you'll have your html output and then you'll have your php logic up at the top you might fetch something from the database here and then output it down here because you can you can you know you can have another set of php tags down here and you can echo out some variable i don't have a variable now so just echo out one two three and you'll see that that's going to work as well okay so you do need the the ending tag if you have html down here if i remove it then you should see an error now a little side sidebar here if you don't see an error like this this parser if you see if it just says like can't find server or something like that then you probably have your your display errors option turned off which you don't want you want to be able to see your errors in development so what you need to do if you're not seeing this is go to your your folder which on mac you can go to this opt lamp folder if you're on windows then you can go to c drive xampp of course if you're using xampp but you want to go to your etc so etc and then there's a php.ini folder that's basically your php configuration so from here if you search for display underscore errors not that but let's see it should be uncommented so this right here if that says off just turn it to on save the file close it and then just uh just restart apache right here so just restart all right then you should be able to see your errors so like i said we can have our html down here for the for the next few sections or whatever next few files we're not really going to be using any html i want to teach you about the php basic syntax so we don't need that we don't need that now as far as comments go before we get into echo print and all that so comments are simple if you want a single line comment it's just going to be a double forward slash okay so comments are are good for just any information about the file or a specific function or whatever or just code you don't want to run so that's a single line now if you have multiple lines like let's say uh i don't know we'll just go down here and echo out one and echo say echo two if i want to comment out multiple lines uh what i could do is a forward slash asterisk and then wherever i want to end the comment i'd put a an asterisk in forward slash and then under here i could go ahead and type all right so those are comments now as far as printing things out i should probably talk about the semicolon you do need semicolons in php the only time you don't and i wouldn't recommend doing this is if you don't have it and then the very next thing is an ending php tag if i save that notice it still works however if i didn't have this here it's not going to work if i have something under here like that that's not gonna work so i would always recommend using the semicolon even if you're in this this you know this situation here all right so i'm just going to paste in some comments here with the different constructs and functions that we can use to output to the browser and some of these are just used for debugging so echo we just looked at that it can be a string with quotes it can be a number you can also do multiple values so if i wanted to do one two three hello and then maybe a float which is a decimal so i couldn't do that and it's going to output all of them now print is another construct that we can use so we don't need parentheses although you can use parentheses with both echo and print so print is oops i forgot my semicolon up here so print can't do multiple values like if i try to do another one here i'm going to get an error i don't really use print never really have i use echo if i need to if i need to print something out i never really used it but it is available all right now print r is is a function so you want to use parentheses and then you could put whatever in here like a string now print r is used a lot with arrays it'll actually print out arrays so let's say and i'll talk about arrays soon but you can see it actually prints out the values if i were to try and echo out an array like if i say echo and array with one two and three inside of it then it's just going to actually let's get rid of that it's going to give me this array to string conversion and just say array which obviously isn't isn't uh very helpful now the reason this is formatted like this if you're if you're following along it's probably not going to be this nice and neat this is from an extension that i'm using which is called let's see i forget what it's called peach this right here php view so if you want to install that on chrome i'm not sure if it's available for firefox but it will just um it'll help with debugging it'll make things look a little nicer things like this like print are all right now var dump is used a lot with arrays but you can use it with anything so if i put a string of hello it gives me a little bit more information it shows me the value but it also shows me the data type which is a string and it shows me the length of the string which is five all right if i were to print out let's say or var dom true it'll give me the value it also shows me the type which is boolean and i'm going to go over data types in in a couple minutes so that's var dump and then you also have var export which is similar to var dump so we can say var export now if i put a string in here like hello and you'll see it'll print hello but it also prints the quotes so if it's a string it'll it'll print those as well all right so these are usually used for debugging as far as echo you might have like you might have some html and then let's say you fetch from a database up here you might have a an array or something of a blog post or a single user or something like that you might want to output like a title here so you'd have php echo and i'll just say i don't know post one or something like that all right so you might have something like see stuff like this now in this case you might see it without the semicolon because remember you you can withdraw the semicolon if the next thing is the closing php tag but i really wouldn't recommend doing this even though to me it does look a little nicer i'm not a big fan of semicolons but i wouldn't recommend doing it you also might see a shorthand for echoing out single lines like this which is going to be without the echo and without the php you'll see just an equal sign here and if i save that you'll see it gives me the same thing so this looks a little cleaner all right now the server has to i think there's there's an option that needs to be set on the server for this to be able to work though all right so i think that's yeah that's good for output let's start looking at data types and variables so i'm going to create a new file called 02 variables.php i'm calling it variables but we're also going to talk about data types which is very important so let's go to zero to underscore variables dot php so as far as the data types i'm just going to grab those real quick and paste those in uh let's need my php tag of course all right so these are the types we have if you're if you're familiar with javascript then you probably know the six different types php isn't much different just line this up it's going to bother me so we have strings which are series of characters surrounded by quotes and it can be single quotes or double quotes and it can be anything within those quotes letters numbers symbols whatever integers are another type or ins which are whole numbers positive or negative floats are decimal numbers boolean is a true or false value arrays are special variables that can hold more than one value we're going to talk about arrays next and then objects which are created from a class this is object oriented programming we're going to get into much later null is an empty variable and then resource is a special variable that holds a resource as far as the rules go for naming variables i'm just going to look at those real quick so they must be prefixed with a number sign and this i think is one of the reasons that some people say php is an ugly language because of these damn um dollar signs but unfortunately we have to use them for creating variables they must start with a letter or an underscore that's all you can start with you can have numbers in a variable they just can't start with a number all right variables can only contain alphanumeric characters and underscores no other symbols variables are case sensitive so this version of name is not the same as this name these would be two different variables all right so let's start to just create some of these so i'll create a variable called name and we'll put a string in it of brad all right and then we'll just echo out here and we can echo name so pretty simple that is a string we could create another one let's call it age and we'll set it to 30 no just kidding i wish so 40 and that is in int so echo out age and you can see it echoes out a number or an integer sorry and then we have boolean values now when it comes to naming conventions i usually like to to name my variables with camelcase like this where it starts with a lowercase and then each word after that is an uppercase but in php you're going to see a lot of stuff like this the underscore style so when i write php i usually i usually write it like this just to kind of match everything else so i'll just i'll try to remember to write my variables in this style so that would be true now if you echo out a boolean say has kids it's going to show a one i'm sorry not a boolean but a true value it's going to show a one if it's false it's just going to be it's not going to show anything but if i were to use let's say var dump actually i think i already showed you guys this then it's actually going to show me the value and the type all right and then let's say we'll say cash not that kind of cash cash on hand and set that to let's do say i have twenty twenty dollars and seventy five cents so this is a float or a decimal let's say that's a boolean all right and the decimal will print out let's say cash on hand and we get float and 20.75 all right so let's talk about having variables in within strings right so if i want to echo out and i'm going to use a single quote here so you can see what happens let's say i want to say name is age years old like that so if i save that it's actually going to show me the variable name it's not parsing the variable so when we use single quotes like this it's not going to parse the variable what we would need to do in this case is concatenation and if i know a lot of you guys that watch my my videos are javascript developers you know that you can concatenate with a um with a plus sign right so what we would do is we're starting with a variable so no quote and then we would have a plus and then we would start the next you know start that string here well with php it's it's the same thing but instead of a plus it's a period but it works the same way so we would say is and then we would end the string and and concatenate the variable here we would end it and start the string again and if i save that now we get brad as 40 years old unfortunately so that's with with single quotes but this is much easier if you just use double quotes because then you can do name is uh age years old if i do that you'll see let me comment this one out you'll see that will work okay so if you have double quotes you can just put the variable right in but i would recommend using this style so you can have curly braces like this and this is just like what you would do with a javascript javascript literal so that works as well so i would recommend this just because it shows that the variable name is you know sticking out all right and then let's see as far as like arithmetic we can say five we'll say echo 5 plus 5 and that will give me 10. now even if these are strings so we'll make these strings instead of numbers i'm still going to get 10 right with javascript it would concatenate it and it would give me 55. and let me just actually we'll put this into a variable here and then we'll var dump so it'll show us the type of x and you can see that is an int all right and then we can obviously we can do like uh subtraction and echo out five say five times six and we can echo out say 10 divided by 2 gives us 5 and we can use the modulus operator as well so if i say 10 modulus 3 that's going to give me 1 because it's giving me the remainder of 3 into 10. all right and then the last thing i want to show you here are constants now constants are you only want to use these if you know for a fact that they're never going to change so i use them for things like database credentials so to create a constant you actually use the define function you pass in whatever you want to call it let's say host and the convention is to use all uppercase and then the value which would be localhost okay so you might see something like that uh we could do define say db underscore name and then whatever your your database name is we'll say dev db i don't know whatever and then you can print these out so we could say echo host like that and it'll show us localhost all right so these are constants only for things that you know are are never going to change all right so let's see let's create a new file here this is o3 let's call it arrays dot php so we're going to talk about arrays so i'm going to go to 0 3 underscore arrays.php okay so arrays are data types or data structures that hold multiple values and there's different types of arrays let's just say simple array and there's two different ways to create them so the first one let's just create a variable called numbers and i'm just going to set an array of numbers and we can do that with just the square brackets all right so that's an array of numbers now the second way we can create that an array let's call this one fruits we can use the array function like that and then pass in the value so apple uh orange and let's say pear okay so those are the two ways you can create an array it's it's really up to you i like the first method better now we can use uh let's say print r if we want to see what's inside an array so let's say numbers all right and again mine looks nice and neat because of that php view extension that i'm using that chrome extension and then we can also use var dump so let's say var dump which is used a lot in debugging now this shows us a little more information it shows us the data type the length of values in the array and it shows us the values which are 1 4 55 22 and then it also shows us the indexes so each value has an index so 0 1 2 3. now if we want to print out a specific value like let's say from this fruits away we want to print out orange so what we would do we'll say echo and let's say fruits and then square brackets one so if i save that you'll see it'll echo out orange now some of you that are brand new to programming might be asking why why didn't they print out apple that's the first one well arrays are always going to be zero based so apple is going to be 0 right so if i do fruit 0 that's going to show apple so it's 0 1 2 and so on all right now as far as the indexes go a simple array like these it's just going to be numbered 0 and up but you can create what's called an associative array so let's say associative array which where you can create the index so for instance if we have colors we can set that to an array and then you can use number indexes if you want like we could do one and say that is the value of red we could do another number it doesn't matter it could be four and i don't know why you would do this but i'm just showing you that you can and then let's say six and we'll say that that's green all right and then if we want to access let's say blue we would do echo and say colors and we would use the index which in this case is 4 for blue alright now usually in most cases with your associative arrays you're going to have strings as keys because that makes more sense so let's say we have like hex and we want us we want to have an array where we have the the color name so let's say red and then we want the value because red is the index the value is going to be the hex value so number sign f00 all right and then let's do blue so we'll say blue which is going to be number sign 0 f 0 and then green which is going to be number sign and 0 0 f all right and then if we want we can echo out hex and let's say we want to get the hex value for blue we would use the blue index like that now a lot of times you'll use associative arrays for like tabular data especially when you're working with databases if you have blog posts or users or whatever so let's create a variable here we'll just call it person and set it to an associative array and the index is going to be in this case we'll do like first name i will say first name brad and then you might have like last name and then let's say i don't know email all right and then if i wanted to let's say echo out person and i could say first name and obviously i would get brad all right so you might see a lot a lot of a lot of stuff like this when you're dealing with databases and it's basically like a like a javascript object or a python dictionary it's just so it's formatted a little different with the the fat arrow over the colon um and you know brackets over curly braces but same idea and then we have multi-dimensional arrays which are basically arrays within arrays so for instance let's comment that out and let's say we have uh we'll say people and we'll set that to an array and then we'll grab this right here everything that's within the brackets and the brackets themselves and paste that in and then we'll have a comma paste that in and maybe one more and then we'll just change up uh we'll change up the names here so we'll say john doe and john and let's say we have jane all right so now we have a multi multi-dimensional array and if i want to access let's say i want the second person's email so what i would do is echo out people and then that's going to be the the second value which is the one index and then i want the email key to get the email value all right and that gives me john at gmail so a lot of times when you're working with um with apis and php or apis with anything a lot of times you're working with json data that's what you're going to return so we could easily turn this into an array of json objects with the json and code function so let's go ahead and we'll do a var dump here let's say var dump and then json underscore in code and we'll pass in people and see what we get so you see it's an array of json values we have the double quotes around the keys and values and we have the colon in between all right and then there's also json decode if you have a json object and you want to turn it into an associative array all right now later on i'm going to go over different array methods so i can show you how to manipulate them and map through them filter them and so on but we're going to get to that a little bit later so the next thing i want to look at are conditionals so let's say zero four and conditionals.php conditionals or control structures whatever you want to call them and let's change this here to zero for underscore conditionals.php all right close that up so before we get into if statements which are the first type of conditional or control structure that we're going to look at i just want to take a look at the operators that we have so these are used for comparison so if we want to see if something is less than greater than we would use these angle brackets we can also do less than or equal to greater than or equal to equal to would be two equal signs and then identical two would be three equal signs which means that not only the value matches but also the type and then we have not equal to and not identical to all right now if statements are the first type of conditional we're going to look at and if we look at the syntax here basically we just say if we pass in a condition and then we have the code to be executed if that condition is true so let's just do something really simple to start with so we'll set an age variable let's set it to 20 and then we're going to do an if statement with a condition so we'll say if the age is greater than or equal to 18 then we're just going to go ahead and echo out here and we'll say you are say you are old enough to vote okay so now that's going to echo out if i were to change the age to let's say 17 then it's just not going to do anything however if we want it to do something if this if this is false then we could do an if else so we could just add else and we'll say echo and say sorry you are not old enough to vote save that and now it's going to echo that out now you might have a case where you want more than just this one condition so in that case we can use an else if so let's come down here and i'm actually going to set a variable to the php date function which will format a a time date and time and you can pass in different arguments to get like if you want the month name or the hour or the day of the month there's different arguments to pass in you can see it see it here within vs code or in the php docs so for instance if i want the day of the month it's uppercase f so if i were to echo out t here i should get march if i want the day of the month it would be a lowercase j and so on what i want to get is the hour of the day so that's going to be an uppercase h so it gives me 15. and i want to have a condition here to to echo oops to echo out something depending on the time of day if it's morning say good morning or if it's noon good afternoon if it's night good evening so let's say if that value is we'll see if it's less than 12 then that would be morning so we'll echo out say good morning all right if i save that it's not going to show me anything because i'm at 15 right now it's it's going to use the current date and time by default now we could put an else here and say echo good evening all right and i actually get that because it's again it's 15. but let's say i wanted to echo good afternoon if it was less than 17 but greater than 12. so what i would do is right here we could put an else if and then a condition and this would have to close all right so it'll be formatted like this so i'll say else if and then if t is less than 17 then let's go ahead and echo good afternoon actually make that uppercase okay so i'll save that and now since i'm at 15 it's going to show me good afternoon all right if it was less than 12 though it would show me good morning so if i set this to let's say 3 and save that's going to be good morning if it's above 17 so if it's at 18 it'll show me good evening and if it's at 15 which is what i'm at now it shows me good afternoon you can have as many else if as you want now when you're doing when you have an if statement or conditional it doesn't have to always be something with an operator a lot of times we're just seeing if something exists so i mean i could even just say if true then echo one two three and that will echo out so let me just comment this a lot in in a lot of cases we're going to be dealing with databases and you'll have arrays of data so let's say we have a posts array with we'll just put first post in here and i might want to check to see if that if there's anything in that post array and there's a few ways to do that one is the empty function so if i say if empty posts then it's going to check to see if it is empty if i save that it's not going to echo out one 2 3 because it's not empty however we could put exclamation in front of it so this represents not so we're saying if it's not empty and then let's say if it's not empty we'll echo out the first element so we'll say the array name and then the zero index so now we echo out first post and if you want to have an else here then let's do we'll say echo and just say no posts all right so if i go up here and we get rid of this and this is an empty array then it's going to show no posts and we'll just go ahead and put that back now i want to talk about ternary operators so this here takes up what one two three four five lines a ternary only takes up one line we can do the same exact thing so i'm gonna comment that out and i'm gonna just say echo and then the condition which is gonna be not empty post so that's the condition the ternary is just a question mark so we're saying if not empty post then let's say posts zero and we have the echo here so what we're saying is echo post zero or the first item if this is not empty else so the else is a colon and then we can just say no post so that's going to do the same exact thing that this did just all in one line and a lot of times you might not be echoing something out but but putting something into a variable so let's say first post and then we could actually take this and put that right in there so in this case we're just setting a variable and looking at the condition if if it's true this this is what we'll be putting this variable if not then that will in fact we'll just echo out first post and save and we still see it echoes out first post now sometimes you might not have an else and if i were to actually let's just copy this down if i were to just take off the else here like that and comment that out that's not going to work because when we have a turner we have to have an else now we could do this we could say else null which will just it won't have a value it'll be null but we can also use what's called the coalescing operator which i believe was added in php 7.4 7.34 something like that so that's actually just double question mark so let's say uh first post and i'm gonna say equal to now in this case we don't have to use the not empty so we can just say post zero and then the coalescing operator and then null so if we echo out first post there we go we just get first post if there isn't anything in here then it's just going to be no all right this value is now no so yeah you can use that as well and then the last thing i want to look at as far as conditionals go are switches so if you have something like this where you have a bunch of else ifs it might be cleaner to use to use a switch so let's go ahead and create a variable here i'll just call it fave color and set it to a string of red and then to create a switch we're going to say switch and we're going to pass the condition in here which in our case is fave color all right and then we want to open our curly braces and we're going to set a case so we'll say in the case where it is red then here we use the semicolon colon and what we want to do if it's red so i'm just going to echo out your favorite color is red okay so really simple and then we can add a break here we need to add a break here and then a new case so let's say for the case where it's blue then we want to echo out we'll just grab this here so we're going to echo out your favorite color is blue and then we might have another case where let's say it's green so go ahead and just format this correctly green so you can add as many cases as you want here for for this value and then at the end you want to add a default so default is if it doesn't match any of those in this case we'll just say echo and say your favorite color is not red green or blue all right so go ahead and save that and we get your favorite color is red so if that's blue your favorite color is blue if we say yellow which is not in the case then it's going to run the default all right so it's just if you have a ton of different possibilities i think it's a little cleaner than doing uh a you know a bunch of elsif's but it's really it's up to you it's preference all right so now we're going to move on to loops so i'm going to create a new file here called zero five underscore loops dot php and let's go over here and set this to 0 5 underscore loops all right so loops are are basically used to execute a piece of code while a specific condition is met and it'll keep executing that code until it's no longer met and there's a few different types of loops so the first one we're going to look at is a for loop so if we look at the syntax here we say 4 and we pass in three things we pass in an initializer so we set a variable we pass in the condition okay and as long as that condition is met this code will will keep executing and then we have an increment where we can take that initialized variable and increment it by whatever usually one so let's go ahead and say 4 and for the initialize we're going to create a variable called x and set it to 0. now these are separated by semicolons not commas like a regular function so make sure you use a semicolon and then for the condition let's say as long as x is less than we'll say less than or equal to 10 okay and then we want the increment which we can say x plus plus which is going to increment it by 1. now in here you can obviously do whatever you want i'm just going to echo out x now if we save that we're going to see 0 through 10. the reason we see that is it starts at 0 because that's what we set it to and then the condition we said as long as it's less or less than or equal to 10 then run this code and then it stopped at 10 because the condition was no longer met if i set this to just less than 10 and not less than or equal it's only going to go to nine and then i can also start it wherever i want so if i want to start it at five i'll set the initialize to five and now we just get five through nine all right and and then you can do anything you want obviously in here i'm just echoing it out we could just we could echo out like number space and then we could concatenate x onto it and then concatenate a line break make it look a little nicer now we get number 0 through 10. all right so that's a for loop the next one is a while loop so it's copy and a copy let's comment that out and the while loop is pretty simple we just say while and then we have a condition and as long as that condition is met the code will be executed so let's take our let's create a variable of x set it to 1 and then we're going to say while and for the condition we'll say while x is less than or equal to let's say 15 then we're just going to echo same thing we'll say number and then we'll concatenate x and line break save that now it's going to give me a never-ending loop it's just going to say number one and it's going to be a never-ending loop until the browser crashes the reason for that is because i don't have an increment in here so x is always one so this is always going to pass and this is always going to run so we have to make sure that we increment that so let's say x plus plus and if we come back over here now it's just going to go 1 through 15. okay you can also do x equals x plus one okay we're just we're incrementing it by one either way either way you write it so the next one is one that isn't well at least for me isn't used to very much which is a do while loop i think i've only used do while a couple times in my life but basically we say do and then we have a a block of code and then we have the while at the end with the condition now the big difference between this and a while loop is this code here is always going to be executed even if this this doesn't match uh it'll always run because it runs before we we actually check the condition so for example let's see we have the x okay we'll just set x again equal to one and then we'll say do and this is where we do the code that we want executed so again we'll just say number and x all right and then we want to make sure that we increment as well so let's say x plus plus otherwise we'll get a never ending loop and then here is where we add the while so we want to say while and then our condition where i'll just say x we'll say x is less than or equal to five and we'll save that and we get one through five it starts at one and this is going to run until um you know five now again this is always going to run even if this doesn't match so if i put a six here that doesn't match this condition right it's six is greater than five so if i save it it still is going to run once because this is executed before the while loop all right and there might be certain situations where you need that functionality but i honestly haven't run into it much all right so that's a do while now the next one is a for each which is specific oops let me just grab it which is specifically for arrays and i would say this is what in real php development you'll use this more than any of the other ones because you're constantly dealing with arrays whether it's data from a database or from a file or whatever so what i'm going to do is add i'll create an array of let's say posts and i'll just set it to strings we'll say first first post second post and third post all right so we have an array now you don't have to use a for each you can use a for loop or a while or whatever in fact i'm going to show you the for loop first and then i'll show you the four each which is much easier so we could set an initializer here let's say x equals zero and then for the condition what we need to do is say if x is uh is less than if it's less than the length of the array and we can get the the length of the array with the count function so we can say count posts okay and then we just want to increment x by one so basically what we're saying here is uh run the function run the the whatever we put in here the code block as long as x is less than the the entire length of the array so it'll stop once it gets to you know three or however long the array is and then let's echo here uh and if we want to echo the value of of each element here then we would say posts and we would use x as the index okay so if i save that we see first post second post third post all right so that's using a for loop and i'll keep that there i'll just uh comment that out now to do the same thing with a four each is much easier we can just say four each and we pass in the array name and then as and then whatever we want to use for the the single each each one so in this case post makes sense or we could do item or something like that and then we can just go ahead and echo and instead of doing the post x or whatever we can just say post so i'll save that and we get the same exact thing and then if you wanted to get the index let's just comment that out and paste that in so if you wanted the index which in this case is 0 1 2 we could say as index and then just use an arrow like that and we could use that now down here so maybe say index and we could do hyphen in between them and then we'll add a line break okay so now we get 0 1 2 because those are that's the index if you wanted it to be one two three you could just simply add one to the index and if you have an associative array so something like this then we would do four each and say person as and we want the key so we can access the key and the value all right then we could go ahead and echo and we'll say key and then just have a hyphen and the value and let's put a line break here all right so we could get the key and the value all right so the next thing i want to look at is functions so let's create 0 6 underscore functions and over here let's go to 0 6 underscore functions so functions are basically blocks of code that you can name and you can run anywhere so to give you an example to create a function we need our php tags to create a function we use the function keyword and then we name it whatever we want so i'm going to call this a register user and then parentheses curly braces and then whatever you want that block of code to run whatever you want this function to do will go in here so something called register user would probably connect with a database and insert some data but we're not doing that yet so i'm just going to echo say user registered all right and then if i save it this doesn't run right away because i haven't called it anywhere all i've done is is declared it if i want it to run i need to call it so we say register user with parentheses and now the code inside will run now before i get into arguments i want to first talk about scope because functions have their own scope if i were to create a variable in here in the function scope like say x equals 10 and then i try to come out here and i try to use that variable if i echo x i'm going to get a warning that says undefined variable x because i declared that variable in the function scope this is the global scope and then at the same time if i were to create a function in the global scope like let's say y equals 12 and then inside the function scope i say echo y i also get an undefined variable however if you do want to use a global variable within a function what you can do is you can say global and then just put the function i'm sorry the variable name and now if i save you'll see it actually is echoing out 12 or echoing out y all right so just know that functions have their own scope this x value this x would not work in another function either because each one has its own scope so i just wanted to mention that let's go ahead and clear this up now now as far as arguments go let's say register user we want that to take in uh an email and then maybe we just say let's say echo and say email and then registered right now this will give me an error because it says too few arguments to function the the register user function takes in one argument but i'm not i'm passing zero in that's why i'm getting that error so i could pass in say brad and now it'll say brad registered because i pass in that argument now you're going to hear the word argument and parameters and sometimes people get those mixed up understandably so this is an argument when you're defining the function and you're defining what goes into it this is an argument when you're passing whatever it is into it as an argument that's a parameter okay i mean some people will call this a parameter or this argument but technically that's that's the difference between the two now this function here is echoing out something it's all it's doing is printing something usually when you have a function you have a return value so i'm going to just comment that out and let's create another function here called sum and let's say it takes in n1 so it takes in two numbers n1 and n2 and instead of echoing i'm going to say return and i want to return n1 plus n2 so the sum of those two all right and then down here i'll call sum now if i save that actually we have to pass in arguments or pass in the parameters and it's not it's not showing me on the screen because return isn't going to print it out what it's doing is is returning it so that this now holds that value so i could echo out this and then it will give me 10. all right another thing i could do is is put this into a variable so i could say a variable of i don't know i'm not going to use sum because that's the function name so i could say number and set that to the return value of that sum and now if i echo number i get 10. okay so a lot of times you'll you'll return something from a function now we all can we can also have default values actually i'll just use this sum right here so let's say i do want to be able to call sum without passing anything in if i do that right now then we get this too few arguments because it expects to two parameters to be passed in now what we can do is just set an equal sign here and let's say i want the default of this to be four and i want the default of this to be five meaning that if i don't pass these in these are this is what will be used four and five so it's the same as me doing this all right so if i now if i save that i get nine without having to pass anything in so basically the the arguments here are optional so that's how you can set default values now another thing we can do is we can have anonymous functions and pass them set them to variables so for instance i could say subtract and i can actually set that to a function and let's just say that takes in number one and number two and then we want that to return number one subtract and number two and then when we do it like this when we have this anonymous function and it's anonymous because it's not a named function but we're setting it to a variable so we actually do have to have our semicolon here all right then what i could do is echo out subtract and i could pass in here let's do 10 5 and i'm just going to comment this echo out and we get 5. all right so you can do that as well now in a situation like this we can also do arrow functions if we have a single line return like that actually i'll just copy this and let's change this to we'll say multiply instead of subtraction we'll do multiplication all right now now i can turn this into an arrow function by doing a couple things first off instead of function we can do fn and then after the arguments we put a fat arrow which is the equal sign and then brackets and then since we have this one single line return we don't need we can get rid of the return statement and then also the curly braces and then yep up here like that and that is a shorter way of doing it than doing it like this and just to test it out we could say echo multiply and we'll say 9 and 9 save and we get 81. all right so we have anonymous functions arrow functions default values we talked about scope so i think that should do it for functions so the next thing i want to do is look at array functions so these are going to be built-in php functions that that are used for basically working with arrays in array data let's say zero seven okay and then we'll just open that up over here zero seven and array functions all right all right so let's put in our php tags and and this stuff is going to be useful for just getting information on a specific array or adding to it removing to it manipulating the data filtering through it whatever it is that you want to do there's there's a function to do pretty much anything with the data inside of an array so let's create something simple we'll just do fruits and have some strings in here so we'll do apple orange and let's do pear all right so simple array of fruits so the first thing we're going to look at is how to get the length of an array because that you'll need that in many cases and we can do that with count so we have the count function we just pass in fruits and save that we get three all right now if you want to search let's say search array for a specific value you can use the in array function and that will give you a true or false if the value is there or not i'm actually going to use a var dump here so we can see that the data type as well and let's say in underscore array first thing we're going to pass in is the value we're looking for let's say apple and the second is going to be the array itself so we'll say fruits and if i save that we get a boolean value of true because apple is in the array if i put apples and save then we're going to get false all right so you would usually have that in an if statement or something if you're looking for a certain value now if we want to add to an array let's say add to array there's a few ways to do this the first one let's say fruits and then we can just set some empty brackets to a specific value like let's see what we'll do grape so if we do that and then down here i'll just do a print r of the fruits array and save that now you can see that grape has been added on it's going to automatically be the next one so another way we can do the same thing is with array push so if i say array underscore push and that's going to take in the array which is fruits and then anything after that will get added to the array for example we can do i don't know blueberry so if we do that you'll see that'll get added and we can keep going here i could add strawberry and that will get added to the end as well now if we want to add to the beginning we can use array underscore unshift all right and and a lot of this is similar to javascript in javascript we would do the array name dot push or dot on shift we don't have that syntax in php we just have the the function that we pass and pass it into so unshift same thing it's going to take the the name of the array and then let's add we'll say mango you'll see that will add it on to the beginning so to remove from an array say remove from array there's a few methods we can use so array pop this will take off take off from the end so whatever the last value is so if we say array pop fruits you'll see that strawberry now goes away it takes off from the end if we want to take off from the beginning we can say array shift and pass in fruits and now mango will go away okay so that will take off the beginning and end now if you want a specific element gone you can use unset so unset fruits and let's see we'll do pair which is the index of two so if i do that then it's going to remove it it also removes the index as well so now it's the index is 0 1 3 4. all right so i'm going to i'm going to just comment out the unset now if we want to break this into chunks we can do that with uh with a ray chunk let's say we want to split it into two chunks or chunks of two we'll say chunked uh we'll call this chunked array hooray and set that to array underscore chunk because we can set we can set it to a variable and then we pass in fruits and say we want two and then let's go ahead and we'll do a print r of say chunked array just comment this out and my semicolon okay so now what's happened is we have these chunks that are in into twos so we have the first two apple orange second two now since there's only five the this this chunk here is only going to have one in it and if we wanted to set it into chunks of three we could do that as well but i'll save it to two all right let's see what else if we want to concatenate arrays we can do that so let's comment these out and let's let's deal with a new array so i'm just going to create a couple arrays of numbers so we'll say a ir1 and set that to 1 2 and 3 and then let's do a r2 and set that to uh we'll do four five and six and we have a function let's do array merge so i'll actually set it to a new array so we'll say array 3 and then set that to array underscore merge and let's pass in array 1 and array 2 and then we'll print print r array three and you can see that that just just put them together merge them together so now we have one through six all right now you can also use the spread operator which if you're if you're familiar with javascript you probably have used quite a bit so let's do array four and i'm going to set that i'm going to set that to just some brackets and if i want to take the values that are in this array one what i can do is use the spread which is three dots just like in javascript and we can say array one all right now if i print out array 4 it should just be 1 2 3. now if i want to add the values for a2 i could do the spread operator and then just do array 2 and then that will put them together all right so basically this is this is these are doing the same thing now the next one i want to show you is combine so let's do actually we'll create a new array here or two new arrays let's say a equals and we'll set this to some colors so we'll say green and let's say red and yellow okay and then another array we'll call b and we're going to set that to avocado and apple and banana and you can probably already tell what i'm going to do here so we'll set a c array and use array underscore combine and we can pass in array a and array b and then let's go ahead and print our the c array and what it's going to do is take the a array and use those as keys and then the b use those as values so we have green avocado red apple yellow banana so that's what array array combined does now if we want an array of keys let's say keys and set that to array underscore keys and we'll use the c array and let's come down here and print our the keys array and save and now we just have an array of the keys all right so remember c was the green avocado red apple yellow banana so it just took just the keys and put it into an array if we want to flip the array we could do we could use array flipped so we'll create a variable here called flipped and set that to array underscore flip and let's pass in here we'll just use the c here and then let's print that out so print our flipped so now the the uh what was originally the values are now the keys and the keys are the values so that's what flipped array flip does all right now i'm going to create i'm going to use the range function to create a ring an array with a range of numbers so we'll say numbers we'll set that to range and what that does is it takes in the start and the the end so i'm going to say i want an array of numbers from 1 to 20. so if i echo out or not echo but print r numbers it'll give me an array of 1 to 20. now let's take this numbers array and let's create a new array using map where we can use these values in each in each item in the new array so what i'll do is create a variable called new numbers and we're going to set that to array underscore map which is very similar to the javascript high order array method of map if you're familiar with that all right and then what this takes in is first a function so we'll say function and we'll say for each number we'll call it number and then we want to return from this this is going to be a return for each item in the array we'll make it a string that says number and then we'll have we'll have the actual variable whatever that is for each iteration and then this also needs to take in the original array which is called numbers okay so what it's doing is taking in the original numbers array which is this right here and we're mapping through and each item of this new array is going to be a string well it's going to have number in a string and then that the actual variable the actual item here all right and then we can print our our new numbers and now you can see we get an array with the string number 1 to 20. so that's array map now we also have array filter which works in a similar way but we can make a condition so if we want to strip out let's say we only want one through ten of uh of the original numbers array so we could say let's say less we'll call this less than 10. and we'll set that to array underscore filter now filter takes in the original array and then it takes in a function now i could do a regular function but you can also do an arrow function so we could say fn and say for each number and then we want to return where the number is less than 10 and we'll say less than or equal to 10. okay now we need to echo this out or print this out so let's say print less than 10 and you can see we now have an array of 1 through 10. all right and then the last one i want to show you is reduce so reduce is good for like adding all the numbers together if we wanted to get the sum so let's say sum and set that to array reduce and we'll pass in the original array of numbers and then we'll do an arrow function now this reduce closure takes in uh takes in two things takes in the carry so the carry holds the the return value of the previous iteration so carry and then number and then let's do we're doing an arrow function so we just want to take the carry value and add the number to it so what this should do is give us a sum let's do a var dump of the sum of all the values okay so it gives us a num an int of 210. all right so basically the carry it it'll start at zero and then through each iteration we add whatever the current number is so it was basically 0 through 20. if i change this array to 0 through 30 then it'll change the sum to 465. okay and there's other functions like this but i'm like obviously i can't go through everything if you look in the repository this page has the link to the docs where it shows all the array functions if you want to get into some other ones now we also have string functions so i want to start to look at some of those so 0 8 underscore string underscore functions dot php okay we'll change this to zero eight underscore string functions all right so let's go ahead and just uh we'll create a string here i'll put it in a variable just called string and we'll set it to hello world now i'm just going to paste these in and just quickly go over them because it's i don't really want to type all of these out that'll probably be very boring for you guys so if i save this we just get a whole bunch of junk here but strlen so string length will give us the length of a string you can see it gives us 11. next one if we want to find the position of the first occurrence so str pos that's going to find the first occurrence of the letter o which would be this one here and that prints out four okay if we want the last occurrence so in this right here it's s t r r p o s and that's going to give us 7 which is this this o here the last occurrence if we want to reverse a string we can use str rev that'll just simply reverse it if we want to convert all characters to lower case we can use str2 lower and at the same time we can use str2 upper to make everything uppercase if you want to make just the first character of each word uppercase we can use uc words and then if you want to replace basically a sub string so in this case if we want to change world to everyone we can use str replace pass in what we want to replace and then what we want to replace it with and then the string okay if we want to return a portion of a string specified by the offset and length we can do that so in this case we're getting from the zero position to five which would give us just hello all right so b give us from zero one two three four five and then if we wanted to get world we could go from five on so this next one saying substring and just putting in five in there and it will give us whatever is after that we also have starts with an ends with which gives us a true or false so we can pass the string in and say does it start with hello that gives us a true which i'm just echoing yes and then what it ends with you could put world you could put just ld or d and those would all be true as well all right now uh a couple other important string functions that we can use are html entities and html special chars which are very similar and they're used to uh to not they're used for security really and they won't parse the html so for instance if i put if i do string two here and i set that to let's do i don't know h1 and we'll say hello okay and then if i echo that out let's say echo string two then that's going to give me an h1 and the browser is going to parse the h1 now this if we have forms on our website this opens us up to attacks because if i put a string in here let's say let's do a script and in here i put some javascript i'm just going to do an alert but let's say that someone passes this into a form and that it ultimately ends up on the page so let's save that and you'll see it actually it runs the javascript so if we don't want that to happen we can use html special chars so if i wrap that around it and we go ahead and reload this now it's just going to show the script all right and if we were to look at the actual code here let's see so uh yeah so it's actually just going to spell out script here and then html entities pretty much does the same thing there's a little difference there but i usually use special chars and we'll be using that later on when we work with forms now we also have for we can do formatted strings with printf so this is also good for when you have outside data when when you have users providing data so for instance we can have in here and we use what are called specifiers so we can still do percent s which is a specifier for a string and you can check the php documentation for to see all of them but let's say percent s likes 2 and then we'll do we'll do percent s and then we can pass in as our second argument here we'll do we'll say brad and then our third will say code so if i save that we're going to get brad likes to code so this is going to replace this and this will replace this and we can obviously do this as many times as we want and then let's say we want to do like this time i'm going to use let's see we'll do 1 plus 1 equals and then i'm going to use a specifier of percent d and then as a second argument we'll say one plus one without quotes which will actually you know actually do the math so if we save that we get one plus one equals two so this here is going to replace this and if i wanted that to be a float i could use the f s f specifier if i save that now you see it's actually formatted as a float all right so i think i think that's it for for string functions so just keep it as a reference and obviously the the github repo has everything nice and neat so you can refer back to it if you want to so let's create a new file here we're going to do 0 9 and this is going to be on super globals so let's say superglobals.php and then in the browser we're going to go to let's say zero nine underscore superglobals dot php all right so i'm just going to paste this in and go over it so super globals are built in variables that are always available in all scopes and they're all used for very different things so i'm just going to quickly go over them but i'm going to have a separate section on most of these so we can see how to work with them so they're all going to be arrays the and they all start with this money sign underscore and then all upper case so the get super global contains information about variables passed through a url or a form so you can actually pass variables in a url and i'm going to show you that in the next video or next section rather you can also get values from a form if the ac if the method is set to get if the method is set to post you can get values through post through a form uh if you have cookies you can get those those will be put in this array in this cookie super global if you have session variables they'll be put in the session super global server contains information about the server environment we'll look at that in a second env is environment variables files is used when you upload a files and we're going to go over that soon as well request is used for variables passed through a form or url so you can use it kind of as a replacement for get and post all right and i know that doesn't explain a lot but again i'm going to go through most of these in in separate sections so let's take a look at server because there's not really anything else in any of these other ones right now they're used for specific situations which we're not using right now so just to show you server because that gives you information about your current uh you know the server we're running and it's going to be an associative array with a bunch of keys and values so rather than doing that because we can't really see anything let's end php and we'll create some html and i'm actually going to just paste this in okay so here we have an unordered list with a bunch of list items with these different values so server http host is for me localhost 8080. and these can be really useful when you're when you're writing your your scripts when you're writing your your apps document root so if you need to get the document root you can use that for me it's this opt lamp htdocs the server name local host the server port 8080 php self this is very useful especially if you're like submitting a form to the same file you can use this that's going to be the current directory and file the request uri is going to be the the url or the uri that you're on um server software so from see we're running apache with php8 with openssl uh http user agent can give you information on the client so what browser they're using i'm using chrome uh says i'm on i'm on a mac and then remote address will give you the the client's ip address for me i'm on i'm accessing from my local host and then remote port as well all right and there's others but these are some of them the more common uh useful values in the server super global so the next thing i want to look at is the get and post super global so i'm going to create a new file here called 10 underscore get underscore post dot php and then let's go to that in the browser here so 10 underscore get underscore post all right so get and post actually let me put my php tag in here so getting posts are used to get data through the url so you can get with get you can get through urls and forms with posts you can get data through just forms so to give you an example here let's actually close that and put a link in here and i want this link to go to the the page that i'm on which i could manually put in here or i could echo out as we saw in the last section we have server and then we can do php underscore self that'll give us the current file that uh that we're in so we just want to close that up and i'll just say click all right so if i click this it's just going to bring me to this file now i can pass variables or pass data through a url and it doesn't have to be a link to this same page it could be to any page so it's a way to pass data to different pages so what i'm going to do here is put a question mark and this is the same as going up here after the php and then putting question mark all right but what i'm going to do is say question mark equals i'm sorry not equals question mark name equals and then brad all right now if i click that it doesn't do anything but if i come up here and i echo out and we're going to use the get super global and say name okay so if i do that now you'll see that brad shows up if i change this to john and i click this and come to the page with john in the url as the name then that's going to show there as well now if i wanted to add more to this this is actually called a query string so if i add more data i can do an ampersand and then i can pass let's say age we'll set age to 30 and then up here we'll go ahead and copy that down and then let's look at age okay so right now it's not defined because it's not in the url if you look up here it's just name equals john but if i click this it has the name and the age so now we get john 30. so now let's take a look at forms so i'm going to create a form here now the action is the file that we want to submit this form to so we could put you know whatever action.php if that's the php file we want to submit to i want to submit to the same exact file so we can actually use this this php self we can add that as the action all right and then inside the form here let's add some fields we'll just do a div and then let's say label and we'll have let's say name and then under that we'll have our input and that's going to have a type of text and we're going to give this a name attribute of name okay now i'm just going to we'll just grab this here and copy that down and then let's make this say age okay text is fine and then name we want to say age and then let's add a submit so we'll say input with the type of submit the value will say submit and let's also give it a name of submit all right so now we have this little form now your form can be it can be either a get request or a post request by default it's going to be get so you don't actually have to add a method like this if it's going to be get you can like you can say get it doesn't have to be uppercase i usually just use uppercase though and then if you want it to be post you would say post which i'll get to in a minute but let's say we want this to be a get form and since i have the name attributes of name and age it should replace these two when i submit the form so if i set this to brad and let's say age 40 and submit you'll see that it will now replace those get variables so again get can be used for urls and forms posts can only be used for forms now one of the issues with the get is if we look up here in the url anything that is in the form that has a name those values are going to be displayed up here you can see we have the name we have the age and then we have submit so if you're using the form to actually post data like to a server if you're adding a blog post if it's a login form anything like that you're going to want to you're going to want to be a post request it's much more secure so what we would do is change this to a post the only thing i'd use a get for in a for a form would probably be like a search if you're just searching and you're not actually submitting any data then that could be a get form but anything else i'd use post so now all we would have to do to get excuse me to get the data of this form is instead of use get we would use post so underscore posts like that now then it's not a fine it's not defined you can see undefined array key name and age but if i add something in here let's say john and let's say 30 and submit now it's going to show john 30. all right and if i reload those values should still be there if i just go to the page like if i go to the end and just hit enter then those aren't going to be defined so to get around this if you have a form and you want to use these values and of course you don't want these errors to show this is where you can use different functions like empty a lot of times you'll see the function is set so we can say is set and what we would do for the form is or what i like to do is to see if the button is set if the submit and we did give it a name so we can access it with post submit so let's say if is set post and we want the submit value all right so if that is set then let's take this and let's move this up here so that way when we first come to the page it doesn't give us an error and then we can set something in here we'll say sarah 25 and submit and now it's going to show on the page and a lot of times you you'll submit forms you submit it and they'll go to a database and get stored there and so on and we're going to get to that a little later now since we're getting into forms i think this is a good time to talk about sanitizing your inputs and talk a little bit about security so what i'm going to do here is actually copy all this and then i'm going to create a new file and call it 11 underscore we'll say sanitize underscore inputs.php okay and we'll come over here and let's go to 11 underscore sanitize inputs okay so we'll paste that in so we have the same exact thing but i'm just going to get rid of the link here we don't need that and as i showed you earlier if we put a script in here and submit since i'm echoing out echoing that out on the page it's going to get executed and it could do some nasty stuff to our website you could put some harmful javascript in there so you need to protect against that you need to realize if the if it's a form where they're submitting maybe it's a comment on a blog post or something and then it's going to be put on the page you have to take note of that and protect against that so one thing you could do is you could set these into variables and we could use html special chars and then wrap that post variable you do the same with age so say age equals and then html special chars okay so now if we were to echo out uh echo name here you see that it's still in there it's still in the the post value it's just going to show on the screen right it's not going to execute and then do whatever so that's one way and then also when you have your action down here the php self this could be vulnerable as well so a lot of times you'll see you'll surround that with html special chars so it doesn't hurt to do that now there's other ways to do this as well i like to use the filter input function so i'm going to replace this with filter let's say filter input and then i'm going to pass in here input underscore post so if it's a get form you could you would use input underscore get and then the second is going to be whatever the key is whatever you know name i should say whatever the name is name age etc so we don't need to have this post thing we would just put a string with whatever post value we want to check which is or sanitize which is name then we would add the whatever the the sanitation rule is now they used to have a filter let's see it should show up here so filter underscore and then sanitize you'll see there's a whole bunch of different ones here this is the one we used to use sanitized string but this is deprecated so what i would use now is this is sanitized special chars all right so that's that's what i would do instead of using this html special chars so let's copy that paste that in here and just change that to age alright so now if i save that we get the same result so if you put an alert in here you know it's just going to show on the page if i put a name all right so that's a that's a way that you can sanitize your your inputs there's also filter var which i might as well show you so i'm just going to comment this out for a minute and uh and filter var doesn't have to be used with inputs it can be used with anything you can just use it for regular like validation so i could say name and set to filter var and the var we want to pass in would be our post value so we would do post name like that and then we could specify again say filter underscore sanitize special chars and now if we save that and i put the script in here that shouldn't it shouldn't run so you can use this as well but uh but i prefer to use filter input like this all right so i think that we'll we'll get more into this when we create our form for that when we get into the database but now what i want to look at are cookies so let's create a file here let's call it 12 underscore cookies.php and then let's go over here and let's say 12 underscore cookies dot cookies dot php okay so we'll close that up close that up all right so most of you probably know what cookies are there it's a mechanism for storing data in the remote browser and thus tracking or identifying return users you can set specific data to be stored in the browser and then retrieve it when the user visits the site again now cookies like it says are stored in the browser they're stored on the client so you don't really want to have sensitive data in cookies uh if you have sensitive data you're you should store them in a session which i'm going to talk about in the next section so cookies are good for if you just want to remember the user's first name or their email address some not login info but just information that can identify them that you can say hello brad when they come back to the website or something like that so to set a cookie is really easy we have a function called set cookie and what this is going to take in is first of all the key so let's say we want to store a name so we'll give it the key of name and then next would be the value so i'll say brad and this would probably come this could come from a form or a database whatever i'm just passing in a string of brad then we want to put when do we want this to expire so we could use the time function which gives us a time stamp the current time stamp and let's say we want to add on to that let's say eight what is the seconds eight six four zero zero so eight eighty six four 400 seconds that's one day so this right here would set a cookie for one day after that then this would expire if we want to do let's say 30 days then we could multiply it by 30. okay so that will set the cookie and actually know what i'll do is just save that and then have the file run and then show you that in the chrome tools or firefox whatever dev tools you're using you can go to application and if we go down to see right here cookies and our local host you can see that there's been a cookie set with the name of name and the value of brad now let's say we want to get that cookie and use it we could first check for it we could say if set and then cookie name so if that's set then let's just echo out cookie and name all right so save that and now you see that we're echoing out brad and if i wanted to let's comment that out we could actually delete it from here so if i say delete and reload the page then that's not there but if i uncomment this it's going to set it again and there it is now if we want to delete a cookie all we have to do is set it and set it to the pass so we can say set cookie and let's say we want to set name to nothing so we want to empty it and then we could do time and let's say minus 864 so that will unset it so if i save that and i reload technically it does get set but then it gets unset all right so now i want to look at sessions so let's close that up and let's create a new file here called one three underscore sessions dot php and then in the browser we'll go to one three underscore sessions all right so sessions let me just grab my definition here so sessions are a way to store information in variables to be used across multiple pages unlike cookies sessions are stored on the server so that's something to really um you really need to understand is the data that is set in a cookie is on the user system it's in the client when you set a session it's on the server so it's more secure okay and you can use it well i mean you could use cookies across multiple pages as well um but sessions is where you would store like a user's id or something like that so that you can then use that id to get their blog post or their recipes or whatever it is that you're you know you have in the database and what i want to do here is make it so we can have a login form and when we submit that the username gets saved to a session on the server and we can use that session variable on a separate page on a dashboard page and we can say welcome and then username all right and then i'll show you how to destroy a session by logging out and destroying the username session so let's copy from the sanitize input so i'm going to grab this if statement and everything below that and let's paste that in and we'll change let's change this up to user name username and the type is going to be yeah text and then username and then let's make this a password okay so we'll do password the type also and oops and the name i'm yeah the name password all right so we're submitting that and we'll go ahead and we'll use the filter for the username but not for the password we don't want to we don't want to sanitize the password that's not going to be put in a page or anything like that so i'll set that to post and password and of course you would never if if we were using a database you would never store a plain text password in a database you would hash it and then what we'll do is check to see if the username is equal to we'll just say john and let's see if the password is equal to password if that matches then we'll go ahead and set the session variable so we do that by taking the session super global and set the key which i'm going to set username and set that to the username that's passed in which should be john if it's going to pass and then after that i want to redirect so we can redirect from within php with the header function and what we do is pass in a string of location colon and then wherever we want to go which i'm going to say php slash crash and i don't want this file to be mixed with the others so i'm going to have a folder called extras and then we'll have a file called dashboard.php okay and then let's do an else so else we'll just go ahead and echo i will say incorrect incorrect login whatever all right so let's save that now before we do anything i'm going to create the dashboard page right here dashboard.php so in a folder called extras so inside extras let's create dashboard.php now what we want to do in here is check for that for the session variable now whenever we use session values we have to have a session started and the function for that is called session start so we have to have it here i'm going to put it up at the very top here session underscore start and wherever you want to use that session value you have to have that session start on so we want to put it here as well now later on i'm going to show you how you can include php files into one another and usually you'll have like you might have a header.php file that's on it's going to be on every url that you go to and that's where you you could have your session start so you could only you only you only have to have it once on something like that you're not going to have to put session star at the top of you know all of your pages if you have a header include so just keep that in mind now we want to check here we want to say if is set the session say underscore session and we're checking for user username all right so if that is set then let's echo we'll do an h1 and say welcome concatenate here you could also just use double quotes if you want and then session let's close that up and the username okay and then we'll just add our h1 ending h1 and let's do an else so else we'll just echo in h1 and we'll just say welcome guest yeah i'll just do that so let's save that now if i just go if i go to the url bar up here and i go to the dashboard let's see let's go dashboard so i didn't log in yet oh it's i'm sorry it's extras slash extras slash dashboard so i get welcome guest um and we could put a link here to go back as well let's say echo and we'll do a href equals let's see so slash php dash crash slash 13 underscore sessions.php actually to use double quotes here and say home right so now we have a home now let's try our login so what should happen is if we match we'll get the username put into a session we'll get redirected to the dashboard it'll check for the session username and then it'll output welcome welcome john so if we try to log in with something that doesn't match this john and password then we're just going to get incorrect login if we do john and password we should get redirected this is just my lastpass thing and now you see welcome john okay so this value right here is stored in a session variable now let's let's destroy a session so we can do that we'll create a log out link so right up here let's say echo and we'll do a href and let's set that to um say logo.php and say logout whoops i keep forgetting to use double quotes here and semicolon all right so this logo php let's create that within extras and this is where we're gonna do the the session destroy now we do have to do our session start here if we're gonna work with it say so we'll say session start and then to destroy we simply say session underscore destroy and then let's let's redirect back to um to the whole to the home page so we'll say header and in here let's say location and set that to slash php crash and slash 13 underscore sessions dot php alright so now we'll click log out and i don't think it brought us to the right place oh two a's in here there we go all right so the session should have got destroyed and we can check that by going back to the dashboard i should have just put a link but we'll say slash extras slash dashboard and there we go so it says welcome guest so the session was destroyed all right and obviously you know it can get a lot more complicated than that this is just kind of the easiest example that that um i could think of okay so the next thing i want to look at is file handling not file uploading we'll do that next but file handling meaning if we want to open a file and display the content if we want to save to a file stuff like that so let's create a new file here we'll call it uh let's see where 13 14 underscore and we'll call this file let's say file handling all right and then let's go to that in the browser so let's see 14. all right so file handling um let me just grab the comment here so file handling is the ability to read and write files on the server php has built-in functions for reading and writing files all right so that's what we're going to look at now i'm going to create a file in extras and i'm just going to call it users.txt i mean you could call us anything and then we'll just put some names in here whatever it doesn't really matter what you put in here but i'm just gonna say it's a text file of users all right and then what we'll do is set a variable here of file and i'm going to set that to extras slash users.txt i'm going to show you some different functions that we can use with files so one if we want to check to see if it actually exists we can say if file exists and pass in the file name which i have in a variable and we do have a read file function we could use so we could pass that and say file so what that does is it reads the file and you can see we we're printing out the contents of it and the 26 here that i believe that's the number of bytes of the file now this this function isn't very flexible so we're going to comment that one out and let's create a variable called handle so this is going to be like a pointer to the file and we're going to set this to a function of f open so file open which will take in the file and it also takes in a mode so you can have read write and a bunch of other ones if you want to look at the docs but we're going to open this for reading and then let's set contents and set that to f read so f read is going to take in that handle that we just created so if we look at f read here it takes in the stream which is the handle and then the length so for the length we can use the file size so file size and then just pass in the file here all right so that'll open it now we want to close the file with fclose so we pass in the handle for that all right and then we're going to echo out the contents okay so there we go we have our contents so basically we create that handle or pointer we get the contents and then we close the file and then we do what we want with the contents now let's let's look at writing so we'll say else if the whoops else if the file does not exist so i'm actually going to delete it so we're going to delete users.txt and in our else here again we're going to create a handle and we're going to use f open but this time we're going to add a mode of w instead of r so we're writing instead of reading and then we can create contents and this time we're going to set the contents so i'll say brad now if we want to have if we want to have these on separate lines what we can use is the php we can do php underscore eol so that will basically give us a line break within the file say sarah and we'll do php underscore eol let's close that up and then we'll do one more let's say mike alright so that's going to be our contents and then let's do f write so that's this is what we use if we want to write to a file we pass in the handle and we pass in the contents then we want to close with fclose and pass in the handle all right so i'm going to go ahead and save this and now if i look in over here you'll see users.txt and it wrote brad sarah and mike on separate lines and if i reload this again the file is going to be there and it's going to show me the contents all right so that's basic file handling now we're going to look at file uploading so i'm going to create 15 underscore file underscore upload dot php and let's go over here to oops 15 file upload all right so let's talk about file uploading so we'll start off by just creating a form let's say file upload and then form now the action i'm just going to go ahead and echo out using my server super global and we want php underscore self and of course you can submit to a different phone you can submit to a different file if you'd like now for the for the method for method we're going to say post now if you are uploading a file there's a special attribute you have to add on to the form called ink type and what you want to set that to is multi-part slash form data okay if you try to upload a file without that attribute it's not going to work so in the form uh let's say we'll just do select image to upload and it doesn't have to be an image but we're going to set it so that it does have to be we're going to run some checks when we actually submit the file so let's say input it's going to have a type of file and we're going to give it a name of upload and we don't really need an id so that'll be our file input now let's do a submit so we'll do submit and let's give it a value of submit and we're also going to give this a name of submit as well okay so now we have our file upload form and you should be able to at least just choose a file let's see i'm gonna go to i think yeah i have a php logo in downloads so i can go ahead and choose that and it'll actually show the file name here if i submit it just basically it submits to to this page but we don't have anything here so it doesn't actually do anything so let's go up here and let's add our php tags we want our ending tag as well and the first thing i'm going to do is check to see if the form is submitted so we'll say if is set and let's check the post we'll check the post value of submit so basically if that button is is clicked and they submit the form then this is where we use the files super global so i'm first going to check to see if a file was actually uploaded we can do that by doing if and let's do if not empty and then we want to do our files super global and then what goes in here is going to be the name so i chose to use upload so we're going to use that and then this is gonna have a bunch of properties on it we're gonna check for name okay so we'll say if that is not empty then we'll do whatever else so else then the way i'm going to handle this is just have a message so we'll say message and let's set that to we'll do a paragraph i'm just going to put uh i'm just going to put some inline styles here of course you could create a style sheet if you want but we'll stay we'll say color and set that to set that to red yes we'll say color red and whoops get that backwards color red and then inside here we'll just say please choose a file and then we'll end that paragraph all right so that's our message and then what we'll do is come down here and where we want to display that we'll just go right above the form let's say php echo and we'll use the coalescing operator here so we can say message and then use the coalescing operator and then null so what this will do is if there is a message then it will echo it all right so let's save that and then let's try to submit this without a file and we get please choose a file all right so if we do choose a file then what do we want to do let's first of all just do a print r of what files looks like so we can see just what that contains so i'm going to choose a file here and i'm going to submit it and let's see what that gives us so we have an object that has or an array that has a name a full path a type a temp name temporary name an error value and a size okay so we have all this this data to work with so what we'll do is put some of this into variables so for instance file name let's set that to files and then we want the upload file and we want the name okay so what i'm doing here is just taking this this value right here it's going to be phplogo.png all right and then there's a couple other things that we're going to want so the file size let's do file size and we just want to change this key right here to um size okay so that's going to get this value right here then we want the temp temp name so this right here i'm going to call file let's say file underscore tmp and we want to set this over here to tmp underscore name and then that should be it as far as what we want from that files array so we can get rid of that let's get rid of this print r as well so we're putting all that stuff into variables now i want to get the target directory where do where do we want to actually upload this so let's say target say target dir and we're going to set that to a string of uploads so a folder called uploads and then we're going to put the uh in here let's say file file underscore name oops file underscore name okay so that will be the target directory and i do want to um i do want to make sure that it's an image so what i'll do is up here let's set a variable here of allowed we'll say allowed ext so allowed extensions and we'll set that to an array i'll just i'll use this method here of creating an array and let's do png so jpeg jr jpg let's do jpeg and let's do gif okay so those are the allowed extensions now it's not going to stop any other files right now because all i did was put this into an array so now we want to come down here we want to get the file extension of the uploaded file so let's say get file extension and we can do this by using let's create a variable first file ext and then we can use the explode function so what that'll do is it'll it'll create an array from a string so the name in this case is phplogo.png so explode will take in where do we want to separate this by and obviously we want to separate it by the period so let's pass that in which is just a string of a period and then the actual file name which is going to be this right here all right so what we want to do is get the the end of it right so the end of the array which will be png so let's say file extension and we're going to set that to first of all make it lower so we'll do string to lower which we looked at in our string function section and then we can use end and get the end of that file extension so that should give us png in fact let's go ahead and just echo out file extension and then let's and there it is because it's already submitted when it reloaded and we're getting png so now that we have that let's check for that and make sure that the file is an image so actually we'll just say validate file file extension and remember when we were looking at array methods there was an in underscore array function and what that takes in is is first what what are you looking for in the array so in our case it's going to be the file extension which in this case is png and then it's going to take in the array that you're searching which is going to be allowed extensions okay so that will give us either a true or false so let's do the else here all right so if it's not then let's just take this message right here and let's paste that in and instead of please choose a file let's say invalid we'll say invalid file type okay and if it is an image i also want to limit the size and we have the the variable for the file size right here so let's say if and we'll say if the file underscore size so if that is less than or equal to and let's stand this is going to be in in bytes so let's do one megabytes which is going to be uh one with six zeros so we'll say if it's less than or equal to that then um we'll keep going else then let's take this message and paste that in and we'll say the file is too large okay now if everything works out then to actually upload the file we use a function called move uploaded file that's going to take in the file temp the temporary location and then it's going to take in the target directory which for us is going to be a folder called uploads and that should do it then we can have a success message so actually yeah so we'll just set message and let's set that actually i'll have it i'll just paste it in and let's just set the color to green and we'll say file uploaded all right so no such directory okay so we're getting this because um there was no directory of uploads did it create it no it didn't um so i'm just gonna just completely reset this file by just hitting enter up here and then let's create the uploads folder so we'll create a folder called uploads and let's try it out so we'll choose a file let's grab it and submit all right so it looks like i'm getting a permission denied so it didn't get uploaded so this is just because on my system the the uploads folder doesn't have the correct permissions so let's see to get around this let's see i'm just gonna off screen i'm just gonna open up a terminal and navigate to this directory let's see okay so now i have my terminal open and you might not have this problem it might have just worked fine for you but uh on my system the permissions aren't correct for me to be able to do this so i'm just gonna do chmod because i'm in my php crash folder i'm gonna do chmod 777 and uploads and then let's see if that will work and it did it just resubmitted and got uploaded all right so if i delete this and let's come back over here and go back to this page just to kind of try this again let's try to upload something that's not an image yep so we get invalid file type for that that was a zip file let's do the logo and submit we get file upload file uploaded and now you can see over here it's in the uploads folder all right so let's see the next thing we're going to look at are exceptions and and try catch blocks so basically handling errors so i'm going to create a new file here called 16 underscore exceptions.php and let's go to that file so 16 underscore exceptions so i'm just going to paste this in and this is from the documentation so php has an exception model similar to that of other programming languages an exception can be thrown and caught within php code may be surrounded in a try block to facilitate the catching of potential exceptions and each try must have at least one corresponding catch or finally block so if you're familiar with like try-catch in javascript or another language this is going to be really easy but let's go ahead and create a function of inverse which takes in a single argument and this is actually from the documentation but we just want to return one divided by that value that ever what's passed in so let's do a check here and let's say if we'll say if not x then we want to throw a new exception so we'll say throw new exception and then we can pass in a message message in here we'll say division division by zero okay so if we passes zero in we should throw this exception so let's come down here and let's echo um inverse so if i put like i don't know 10 in here let's save that and we get 0.1 so if i pass in 0 and i save we get a fatal error okay so it stops our script and we get our uncaught exception with division by zero so right now it's it's uncaught and the way we can deal with this is by using this function inside of a try catch block so let's say try and open up our parentheses and then we have a catch here and inside this catch arguments we want to say exception and then a variable that we want to use so we'll say e and then let's move this down here actually you know what i'll do is uh we'll call this twice and we'll have one of them work so we'll pass five in so this is the one this inverse zero this will throw an exception and then down here we can catch it and we can do what we want so in this case we'll just say echo caught caught exception and then we'll also show that message so we have e and that has on it a value of get message like that and then we'll just have uh just a space all right um semicolon there so let's try that out we'll go ahead and save now notice it it this inverse 5 is executed we get 0.2 and then this throws the exception and we catch it here so the script doesn't stop and give us a fatal error in fact just to show you that it doesn't stop let's do a hello world down here and we should still see that okay if we just call this inverse and we don't have it in here and we just have it like that then it's just mess it's we get a fatal error and everything just stops so that's how you can throw exceptions we can also have a finally block so a finally is executed regardless of whether an exception is thrown or not and we can add a finally just by adding on to this finally like that and we can do whatever we want here i'll just echo i will say first finally okay so if we run this we should see first finally and then if we have another try put this down here and get rid of the five there so this will also throw an exception and we'll say second finally and you'll see that as well so it's just a way it's just a a way to handle errors elegantly and not have fatal errors and and if you need your script to keep executing then it'll do that all right so the last five sandbox file that i want to create and topic that i want to talk about before we do our little simple feedback project is object oriented programming and classes so let's create a new file here we'll call it 17 underscore oop dot php and then let's open that over here okay so o p no it's what is it 17 underscore oop all right so let me just paste in what i have here so from php5 version 5 onwards you can write php in either a procedural way which is what we've been doing up to this point or an object-oriented way and if you have experience with like java or c-sharp then this should be easy for you when it comes to creating classes object-oriented programming and php consists of classes that can hold properties and methods which are basically just variables and and functions within a class and then you can create objects from the class so let's start off here we'll just create a class of user all right so we just format it like this and then we can have properties in here so i'm going to say public and let's say a user has a name and let's say a user has an email all right and maybe maybe a password so public password so these are this is a class and these are properties so actually just go ahead and put a comment here so properties are variables or let's say attributes so attributes that belong to a class so we have three here now obviously this this class doesn't do much but we can go ahead and create or instantiate a user object so let's say instantiate instantiate uh a user object and we do that by creating a variable so let's say user one let's set that to new so we use the the new keyword and then the name of the class okay so user1 equals new user now if i do let's say a var dump of user one you can see that it's an object and it's a user object it also shows the properties although there's nothing in them they're all null but it shows name email and password all right now the way these are where this is set to public i could come down here and say user one and then i could say the name is equal to brad and if i save that now you'll see here in name i have a string of brad and i think this is probably a good time to mention axis modifiers so let me just paste this in here so this there's public private and protected since these are public they can be accessed from anywhere so i'm using them outside of the class here right i set it here and if i want i can also echo i could echo user or user one name here as well all right now if i set these to private you'll see i get an error that says cannot access private property so private can only be accessed inside the class so if i create another a function or a method inside this class i can use them and then protected if you can see if i go ahead and set these to protected it's going to give me the same error except it says protected so i still can't access them outside of the class but i can access them inside a class that inherits this one because you can have other classes that inherit parent classes but let's go ahead and just set these to public for now so i'm gonna let's see get rid of that and that and also that now a method let's come down here let's say a method is a it's basically just a function that belongs to a class so i'm going to create a function here or a method and call it set name and i'll say it'll take in a name now i want to set this this name property to whatever's passed in here so in that case we can use the this keyword so i can say this name and and this keyword can confuse a lot of people when you're just starting out with object-oriented programming but it basically just means the current object so when i say this name and then i instantiate a user of user one this just pertains to that that object all right so i'm going to say this name equals the name that's passed in so now if i come down here and i say user one and just like we can access properties we can access methods as well so we'll say set name and i'll just pass in brad and then we'll save that we'll do a var dump and now you can see name is set to brad so i was able to access that method and this just it pertains to the to the current object i could also create another user so let's say user two and let's say user two set underscore name and we'll set that one to john and then if i var dump two save that now you'll see john okay so that's what a method is it's just it's a function within a class that and you can access properties from within that method you can also you can also access other methods now this is called what's what's called a setter so we're setting a property we might want one to get a property as well instead of using it directly so we could say like get name and then we'll just return from that this name and then if i come down here let's go ahead and just echo out user one say get name and we'll also do user 2 get name and we get brad and john all right now when you instantiate an object there's a way that you can you can assign these properties without having to do these functions or without assigning them directly and that's through what's called a constructor so let's go right here and i'm going to say a a constructor is a function or a method that runs when an object is created all right so it runs automatically and the way we create it is we say public function and then we call it double underscore construct i like to have my parentheses up here and what this will do is it'll take in actually before we do any of that let's just echo we'll say uh constructor ran and i'm going to save this and you're going to see constructor ran twice because i initialized or instantiated two user objects i'm just going to get rid of this all right so i didn't print anything out down here it's just that the constructor ran because i initialized two objects if i did another one let's say three then you're going to see it run three times so these constructors can take in arguments so let's say name say name email and password all right and what we want to do here is set those values that are passed in to the properties so we'll say this name equals name and we'll do the same for email and the same for password okay so now come down here and when we instantiate a user we can now pass in the values so brad brad at gmail and then i don't know some kind of password and then we'll do this user here so john john at gmail and uh password okay so now we'll go ahead and echo out say user one and let's echo out the email and we'll do echo user and say user two and we'll get the name so we save that we get user one's email and user two's name all right so we have classes properties methods constructors instantiation so let's look at inheritance so say inheritance now we have our user class but let's say that we want to have a sub class of employee so we'll go ahead and say class we'll call it employee and the way that we ex or the way that we inherit from a parent class is with extends so this class is going to extend the user class actually let's make this an uppercase e so we're going to extend the user class and then we'll have our our constructor so we'll say public function we construct and we're still going to pass in our name email and our password but let's say that employees can also have a title so let's also pass in a title and then inside the constructor we don't have to do this again so we don't have to assign all of these if we want to if we want to do this whatever is happening in the parent class constructor we can use the parent so we can say parent double colon and then construct and then we just pass in name email and password okay so it's similar to the super function if you're familiar with in like react if you're in class-based functions and then since we have a title we do have to pass in we do have to set the title here and that's only going to be for employees not for for users and then what we'll do is create a method here let's say public function we'll say get title and we'll just return and let's say this title okay so now let's go ahead and instantiate a new employee so we'll say employee one and let's set that to new employee and we're gonna pass in the name we'll say sarah and what else email so sarah at gmail and then the password her password and then uh the title so we'll say manager all right so we set employee one now let's go ahead and echo and we're gonna echo employee one and then the get get title function so if we save that you'll see that it's outputting manager let me just comment those out all right so now it's showing manager so that's how we can extend or we can inherit from apparent classes using extends and then to run the constructor for the parent class we use this here and then we can continue to create new properties and methods for that what's called a subclass all right so now we're going to work on the feedback project so if you go to the the link in the description the github repo and actually i don't want to leave that page let's open it up here and you go to the php crash again links in the description you'll see that there's a starter files folder right here the final feedback project here you can see are the php files but in the starter files you have a feedback folder that just has a couple html files so what i want to do is take this little html website and turn it into a php app where we can submit feedback to a database and we can fetch it and show it on the page so these are the fold files i'm going to go ahead and just pull into here now if you want to create a new project in your htdocs file you can do that and then open it up in your nvs code if you want the live server thing though remember to run live server run the php server and then also you're going to have to change this to whatever you call that project in your hd docs but yeah i'm just going to grab it i have my starter files here and i'm going to bring the feedback folder over to here all right so now if we look in feedback i have my html files in fact i should be able to go now to php crash slash feedback and let me just scale this down and you should see something like this all right so it's just a form with a name email feedback we also have the feedback page right now everything is just hard coded in the html but we're going to fix that so yes now we're not paying attention to anything outside of this feedback folder so if we open up the index.html there's no css file because i'm actually using bootstrap i'm just including the cdn so i'm including bootstrap remotely and just using bootstrap classes down here to design it so let's start off by renaming these files so index.html is going to now become index.php okay that's going to break for a second but that's all right this is going to be feedback.php and this is going to be about dot php and if we go over to the url you'll see it's it says says index.html but we can change that to dot php or we can just get rid of the index and it should load all right so i want to talk about including php files into one another because right now you see we have three separate pages and we could have you know five 10 20 pages and we have the same thing from the doctype down through the whole all this navigation okay if we go to feedback well this isn't going to work right now because we're still going to html but uh this is on every page so what i'd like to do is in our feedback folder let's create a folder called inc for includes and we're going to create a file in here called header.php and let's create another one in there called footer.php alright so obviously you can kind of see where i'm going here i want to put the header stuff in this header file so let's go back to index.php and we want to get from the doctype so all the like all the head stuff and then let's go down through the navigation excuse me even the main tag and then this first div with the container this is on every single all three pages so what i'm going to do is cut that and i'm going to put that in the header.php and save that and this is very similar to if you have any experience with a front-end framework like react you have your you know you have your header component and you that's sure that's displayed on every page you don't have the same code on every page so now let's do the footer so back in the index page we're going to get from the bottom up to the last div so this right here we're going to cut that and we're going to put that in footer.php all right so we can close those two files up for now and now in the index if i save this we lost the header and footer so i want to bring it in so to bring one php file into another there's a couple couples functions we can use one is include so let's say include and we want in the ink folder slash header.php and if we save that now we have the header back if i copy this bring it down here paste that in and then we also want to include the footer now we have the footer so include is one now require is another one we can use if i save that'll work as well and the difference between include and require is if this isn't found like if i change it to header one if we use require it just kind of stops everything all right if i use include then you'll see it doesn't it still loads the rest of the page so i usually prefer include uh there's also require once which will require it and it'll it'll check and if it does if it's already there it won't try to require it again so you'll see include require and require once i usually use include so whoops what am i doing save that now we want to do the same thing with the rest of the pages so let's go to about php and let's get from the doctype down to the let's see the main tag and then the first div cut that or delete it and then put the header and then at the bottom we want to get up through the last div paste that in footer and i don't have prettier setup to format my php um so it might not be you know formatted the cleanest way and then feedback we're going to do the same thing let's go down to let's see down to this first div here paste that in and then let's get from here up to the last div it's going to be footer dot php all right so now we have the header and footer on all the files let's change the navigation now we only have to change it once in the header.php so let's see and close that up and right now everything's going to dot html we're just going to change that to dot php all right so change that and feedback.php about php and now we should be able to navigate around okay so now these are no longer html files or php files so let's see the next thing i want to do so we have our form on the home page right on the index page we have our name fields so name email we know that we can submit this to the page we can get the the data from the post variables but what we want to do with that instead of like just putting it out on the screen like we've been doing we want to use a database so i think now is a good time to to do that to set up our database so if you're using xampp like i am then you already have mysql set up or installed if you aren't if you just have apache running php then you have to install mysql on your own now there's a lot of different tools to work with mysql of course you have the command line but there's a tool that comes with xampp called phpmyadmin which which we're going to use which is a graphical user interface that runs in the browser so that we can work with our database so if we go to our local hosts on mac it's going to be 8080 and then slash phpmyadmin so here we can see the databases on the side by default you'll have like this information schema and so on and what we want to do is go to this databases tab and we can create our new database i'm just going to call it php underscore dev click create and then we want to create a table so mysql is a relational database meaning we have tables of columns and rows all right so the columns are the fields that we want in our table so i'm going to say that table name is going to be feedback and i want to have five columns and you can add you can add more or remove them if you want after but let's say five for now and click go and then we have this interface where we can add some fields or add some columns so the first thing we want is an id and you're almost always going to have an id which is going to be your your primary key and i'll show you how to set that in a second but under type we have all these different types int is an integer or number varchar is a variable character this is often used for short fields like emails names titles text is often used for like blog post bodies longer text date you have other number types floats decimals booleans all kinds of stuff so we're going to choose int for the id the length is this is the max length of the number in this case we'll just say 11 and then we want to make this auto increment so we check this ai and that just means that when we submit in this case a feedback post it'll automatically get assigned an id it'll start at one and then each one we add will add one to that and it's also going to get set as a primary key because this is going to be the unique identifier for our records for our feedback so you'll never have two of the same id and then we're going to have a name field because if we look at our form let's see i'm just going to move this over here so if we look at our form we have a name email and feedback which i'll call this body so we definitely want those and then i also want a date so let's say name is going to be a varchar variable character the max length will do 255. and then that's all we need there then email also varchar 255 and then we'll do body for the feedback which will be a text field and uh then we'll do date and we'll set that to let's do date time and you can also set default values so i'm going to set a default as the current time stamp meaning that when a record's inserted into the database it'll get the current time by default for this field so now we can click on save and if you need to add more you can just add more columns click go we're going to click save and now we have our table structure so if we want to actually insert data into the database we can we can actually do it from here ultimately we want our app to do it but we can click on the insert tab here the id you're not never going to put in that will get put in automatically for name let's say john doe and then email and then for the body so this is a feedback i'll just say traversing media is a good source to learn from i don't know i guess that's good and the date will be the current time stamp so if we click go it says one row inserted and it also gives us the actual sql so sql stands for crusted structured query language and that's what mysql uses as well as just about all relational databases such as postgres oracle so sql this is this is how we insert into a certain table we put the fields then we put the values i do have an sql crash course if you want to learn more about the syntax but this isn't you know we're not going to dive deep into sql syntax so now if we go back to browse we should see our one fee our one record here now we also need to create a user for this database so if we go make sure you're in the in the php dev database and if you go to privileges you'll see there is a root user but i'm not going to use that i'm going to click this add user account and for user name i'm going to say brad and under host i'm going to choose local so localhost and then password i'll just do one through six and let's see down here we're going to grant all privileges because you can have a user just be able to read data or maybe create but not update stuff like that so we're just going to grant all and then that should do it we'll click go and you can see the sql that was ran create user brad and grant privileges etc and now if we go to user accounts you should see the brad user or obviously whatever you called it and trading call it brad or maybe you did but it doesn't matter so now we want to connect to it through our applications so in the feedback folder we want to create a folder called config and inside config we'll have a file called database.php now there's two main ways or two main apis or drivers that we use to connect to databases with like vanilla php and that is mysqli which is what we'll be using and pdo which stands for i think php data objects so pdo is it is my preference if i were to build a real application and a lot of that is because you can use any data any relational database well not any but there i think it's up to 12 different databases postgres and some others mysqli you're limited to mysql um now mysqli is a bit easier so that's what we'll be using i do have a crash course on pdo if you want to check that out though so in our database.php we're going to first of all just define some constants remember back in the beginning we learned how to do this define and i'm going to call it db underscore host and i'm using constants because these aren't going to change and if they do it's because you're going into production or whatever so let's do host and then we'll have a couple more we need the user so my user's name is brad and we also want the password so db pass and then we also want the db name which for me is php underscore dev okay then we want to create our connection so we're going to use again mysqli so i'm going to create a variable called con and set that to a new instance of mysqli so we're doing here exactly what we're doing when we created a user class and then instantiated a user with new user only now we're creating a connection instance with mysqli and this takes in a few things takes in the db host the db user the pass and the db underscore name all right so that'll create our connection now we want to check the connection so we can say if and then on that connection object that we instantiated there's a property called connect error so we want to check for that if that exists then we're going to call the die function which just cuts everything off and then say connection failed and then we'll just concatenate onto that that connection object and then the connect connect error property which will be some kind of message and then just for now we're going to echo out connected just so we can see if it worked so save that and it's not going to do anything just yet because we need to include it now we should include it in the header because the header is is on every page right if we go to home feedback or about the header is going to display so this is where we can include that file so let's say include inc slash i'm sorry not inc config slash database dot php and we'll save that and there we go we see connected now if you seek some kind of like access denied or something like that then you did something with the user so make sure that you have the user set to grant all privileges on that database all right so now that we know we're connected we can get rid of that echo and we can close this file up so the first thing i think i'd like to do here is is um get the data from the database select it and show it on this page this feedback page alright so let's actually insert one more uh one more piece of feedback so if we go to our phpdev feedback table we can click on insert and let's add one more here we'll say i don't know sally smith email and we'll have her say brad traversy is terrible and click go all right so now if we look at the feedback table we now have two entries here all right so let's put that back over here now to fetch the data let's go to our feedback.php because that's where we're going to want to put this and let's actually start with just some hard-coded data up here before we actually fetch it from the database so we'll open up some php tags and i'm just going to paste in a hard-coded array of feedback items so just a multi-dimensional array that has an id and name email and body so down here let's go under the h2 actually i'm going to say past feedback so under that i first want to check to see if there is any feedback so we'll open up our php tag and we'll say if and then empty and then feedback the name of our array now when you're working within html like within templates like this instead of doing the curly braces we can actually just do it like this and then to end it we could say php end if so that's how we do it when we're working within html files like this now if it's empty then let's do let's do a paragraph i'll give it a class of lead and mt three and we'll say there is no feedback all right so if i save that we're not going to see a change but if i were to temporarily just empty this array now you'll see it says there is no feedback but i'm going to put that back and now what i want to do is loop through that and then output a card for each one so we actually only need one of these cards so let's see card body that ends there get rid of that we just want to basically wrap a for each around this because for each one we want to display a cod so let's say php and we'll say four each remember that's what we use to loop through an array we say feedback as and we'll just call it item feedback item put a colon here and then where we want to end it let's just indent this and then we want to end it here so php end for each like that now we're gonna see the same exact thing the reason for that is we have three items up here right three feedbacks and we're saying for each one show a card so we're still seeing three here now the data is the same because we haven't changed anything within here so what we'll do is change up this take this text out of here and let's say php echo and we want to get the item because that's what we used right here is the word item we this could be anything and then in in brackets we want to get the field that we want in this case is going to be the body all right so if i go ahead and i save that now we see just the body three times right we see all three of these and we see what's in this in these values if i wanted to show the the names i could do that that's going to show me each name so let's kind of format this a little bit so we'll have we'll have the body and then there's a couple classes i want to add here so next to this my3 i'm going to do a width so w dash 75 which will just make them a little wider and then on the card body i'm going to add a text dash center okay and then let's go under the body here and i'm going to have a class of text dash secondary and a class of mt2 which is margin top and then in here i'm just going to put we'll say buy and then we'll have the person's name so php echo and then item and name and remember we're not doing anything with the database just yet we'll get to that so that should show the name so underneath it you can see each person's name we could put the email but i'm not going to do that we'll just have it so it's in the database and then we'll have the date under that but i didn't put a date in this array so we'll just leave it like this for now and what we're going to do now is get rid of this we'll just set yeah we'll just get rid of this whole thing for now and we want to fetch from the database so this is actually going to be pretty easy we're first going to create a variable with sql this is going to have our actual sql code in it just like i showed you within phpmyadmin where we did it showed like the insert into or the select from so we're going to say select all and this doesn't have to be uppercase it's just kind of a convention we're going to select all fields from the feedback table okay so that's our sql then we want to say result so the result of that we can get with a function called mysql i oops my sqli and then underscore query and then we want to pass in two things the connection which we're getting because we're including the uh the header which is including the database and then the sql that we just wrote so that's going to include that okay and then we're going to create our feedback variable again except instead of a hard-coded array we're going to set it to mysql i underscore fetch all and there's a bunch of these you can see you can fetch data as different things if you want like a regular array associative array objects whatever we're going to say fetch all and then pass in the result and then we're going to specify here mysql i underscore a soch because we want this to be an associative array just like i just had hard-coded except it's going to come from the database so now i'm going to save and there we go so now we're seeing the the items that we have inside of our database these two items here from sally and john and then there is also a date in the database so where it says by name after the name i'm just going to say on and then the date so php echo item and date and then close that up so now it says by sally smith on and then the date you can use date format as if you want it to look a little better all right so very very simple to get the data from the database and and obviously when you get more into php and you start learning object oriented programming you're going to have special classes and and objects to to fetch from the database like you might have in this case a feedback class where you could call feedback get or something to get the data but again this is for absolute beginners so this is kind of the easiest way to do it so now we want to do that from the home page we want to be able to also submit data so let's open up index.php so we already know like how to submit data to a form and stuff so again you want to have your name attributes for each field and then we're going to go up here and let's open up a new set of php tags and the first thing i'm going to do is just set some variables and i'm going to set these all to empty values and and i want to show you a shortcut so instead of doing like name email equals nothing because we want to set these to nothing by default we can actually do this we can say name equals email equals body equals nothing okay so that's a shorthand for just setting them all to an empty string and then for error handling i'm going to say name error equals email error equals and body error equals nothing because what i want to do is if like if we don't have a name or something and we submit i want to show the error underneath using some of the bootstrap classes so it'll have a red outline with the error all right so we'll do some form validation so let's say form submit and remember how we were checking it we were saying if is set and then we're checking the post so post uh submit and actually before we do that we should probably make sure our form is all set we need the method to be post okay method post and then in the action let's say php echo let's use our html special chars and inside that we're going to put our server super global and we want to cert we want this to be php underscore self all right so it'll submit to this same file now up here let's validate the name so we'll say if let's do if empty and check the post name so if that's empty then i'm going to set the name error so now we're getting into some of the logic and this is of course you'll be doing a lot of this in not only php but in programming in general so we'll set the name error to a string of name is required all right then else meaning that if it's if it's not empty then let's create a var or not create but take our variable name which up here is set to nothing and let's let's um sanitize it so we'll say filter underscore input and we've learned all this stuff already we're going to say input underscore post and the the key is name so we want to say name and then the sanitize is going to be filter underscore sanitize and we're going to use full special chars all right so that'll get the name and we want to do the same thing for like the email and uh the email and the body so we'll just copy this this whole if statement here and let's paste that in twice so this next one is gonna do the email i'm just gonna replace those so post email email error is gonna be email is required and then if it is there then we're gonna set that we just wanna sanitize email and we're going to use instead of that we're going to use the filter filter underscore sanitize email okay so that'll do that then we want the body so let's see this body body error we'll just say feedback is required and change this and that to body now real quick just to kind of check to see then the error or the name we'll just say echo name error and let's say echo name all right and those are both empty by default so if i type something in the name field and click send i should see it echo out right here if i don't put anything in and i click send i'm going to see name is required same thing with uh email if i were to output those but i don't want to just simply output the message at the top i want to be a little more creative using utilizing bootstrap because with bootstrap which is a css framework in this input this name input where we have form control we can add the class of is dash invalid and if we have that class it makes the box red and we can also go underneath the input still still within this div this mb3 div we have the label and input we're going to go right under the input and we're going to add a class of invalid dash feedback and then in there we're going to output let's say php echo the name error all right so let's save that and now you'll see name is required now if this class this invalid is not here then it doesn't show the red border and it also doesn't show whatever is in here so this is invalid needs to be in a condition saying if the name error exists then have that class show so what we'll do is come in here and we'll put in a php open up a php tag and then we'll check for the name error actually we'll use a ternary so we'll say echo and say if not name error so if not name error then we can say is invalid we'll have a class of is dash invalid which will make it red else then we'll just say null and then close the php tags all right so let's save that let's see if that works if i click send without putting anything in no that doesn't work this in oh wait a minute no it's if is name error if is name error there we go so if i put something in here and yeah so that works but if i don't put something in it doesn't okay so now we can do this with the rest of these fields so let's copy this echo right here and let's put that right under the form right after the form control here except we want to change this one to email error because now we're checking the email and then we also want the the div with the invalid feedback to be right under the input and just replace that with um email okay so let's see let's try that yep so if we put something in here and submit it has to be an email address so if we submit okay that goes away if i put something in here that goes away so now for the body we want to do the same thing so let's copy this right here under the form control let's see echo name error is valid now we want to grab that and then put that right in here and we want to just change email error to body error and then let's see we want to put this invalid feedback under the text area and make that body error feedback is required good and if we have something in all of them then it passes if we have nothing then we get our errors cool and i know that it's it's you know this is all kind of crammed together lots of html mixed with php but i wanted to make it a little interesting rather than just having one message up at the top so now we have the error checking we can submit we can do the error checking now after that what we'll do is just make sure that there's no errors so let's let's do an if statement here we're going to say if empty name error and if empty email error and if empty body error okay so if all of that is true if if they're all empty that means there's no errors so we can move on then we want to add to the database so we can do that it's kind of the same thing we create our sql i'm going to put this into quotes we're going to say insert into and then the table name which is feedback and then we want to put in parentheses here the fields that we want to add to which are name email and body and then we want to specify the values so values and then open up some parentheses and then in quotes we're going to put the first one the first variable of name second one of email so this is data that's coming from the form and then also body all right and then we want to just end that so that's our sql now we want to do an if and again we're going to use mysqli underscore query and inside there we're going to have our connection and then our sql statement that i just wrote okay and then if this is true then it means it's successful okay so that is success oops so that's success then i'm going to re i'm going to redirect remember we use header to redirect and say location location and we're going to set that to feedback dot php which is the page that shows all the feedback and then else so if something goes wrong then we have an error so we'll just echo out here let's say error and i'm going to just concatenate onto here mysql i underscore error which takes in the connection variable so that will basically say what's wrong all right so let's try this out i'm just gonna um come up here and refresh this so let's put a name in here we'll say i think we already used john let's say jack jack johnson and travis c media is a nice guy a noose guy i'm a travis media is a new sky all right so let's send that and there we go we get redirected and you can see right here traversing media is a nice guy by jack johnson so now we have the ability to add to our database through this form and then it shows up here okay and hopefully you can just kind of see how this works we create our sql same sql that you would use in your terminal or in phpmyadmin whatever and then we run it with this mysql query in this case we're just saying if it if something if it happens how it's supposed to then we just redirect if not then we show the error all right and then we have our error checking down here as well so if we try to submit without the the data then we get these red boxes with the the error messages all right so this was a long ass crash course i did not expect it to be this long so if you followed along through the whole thing and code it along with me then you're awesome you're probably like like five percent of people that watch this uh so i really appreciate it i hope that you learned a lot from it from here i would probably recommend looking more into object oriented programming i think it's a lot neater in php because php can be kind of messy you can just look at this and it's not the the best looking code right so when you when you work with object oriented programming you're creating classes you create a model view controller structure to your application it just makes things look a little better so i do have a course on udemy it's a it's an object oriented model view controller course where we actually build a php framework so that's probably where i would suggest going to next maybe watching like the pdo crash course i have first maybe another simple php project i do have one where we create a rest api with just vanilla php that's something you could check out um i had some cool guests on that did some php videos so it's up to you where you want to go from here but i just hope that you learned at least something from from this one so thanks for watching and i'll see you soon
Info
Channel: Traversy Media
Views: 754,842
Rating: undefined out of 5
Keywords: php, learn php, php tutorial, php mysql, php course, lamp stack, xampp, laravel, wordpress, php for beginners
Id: BUCiSSyIGGU
Channel Id: undefined
Length: 195min 36sec (11736 seconds)
Published: Wed Mar 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.