PHP Programming Language Tutorial – Full Course

PHP Programming Language Tutorial – Full Course
Spread the love

my name’s mike in this course i’m going  to teach you guys everything you need to  know to get started writing php a php is  an awesome programming language and it’s  actually a programming language that you  can use on the web so if you’re looking  to build an awesome scalable and easily  maintainable website or web application  then php is an awesome language to do it  with php is great because it integrates  super well with html so you can actually  write your php code right along with  html and they’ll work together perfectly  there are millions of websites and web  applications that are currently online  which use php and rely on php on the  backend and php is what we would call a  server side language so it’s basically a  language that’s going to sit on the web  server and it’ll be able to interact  with the client and do different things  in order to make your website more  powerful in this course we’re going to  cover the bare basics so i’m just going  to get you guys up and running with php  we’ll talk about installing php getting  everything set up  we’ll look at creating your first php  file and we’ll talk about like how php  can interact with html and then we’re  going to get a little bit more advanced  we’ll talk about how we can use php to  get input from html form we’ll look at  how php can be used in combination with  html to make our websites more powerful  and then we’re going to look at just  some general programming concepts things  like if statements and for loops and  arrays and data structures so we’re  going to get into not only how php can  be used on the web but also how php can  be used just as a general programming  language then finally we’re going to  talk about object oriented programming  so we’re going to look at how we can use  php to create things like classes and  objects so if all that stuff sounds good  then this course has something for you  i’m really excited to be bringing you  guys a php course and hopefully by the  end of this course you have a better  understanding of php and you can get  started writing your very own web  application  in this tutorial i’m going to show you  guys how to install and set up php on  windows so php is actually pretty easy  to set up we basically just have to  download a few files and we’re actually  going to have to modify something  in our windows computer and then we  should be able to have everything set up  and ready to go so i’m going to show you  guys how to do that  and the first thing we want to do is  just head over to our browser we’re  going to go ahead and download something  and i’m actually over here on this  website it’s called  php.net so it’s a pretty simple web  address  and over here there’s going to be an  option for downloads so we’re going to  go ahead and click downloads and you’ll  see over here there’s a bunch of  different options we want to click  windows downloads because we’re on  windows so i’m going to go ahead and  click that and now over here you’ll see  that there’s a bunch of different  options  this is going to be the latest version  of php that’s going to be available so  in our case it’s 7.1  and what i want to do is basically pick  the version that’s appropriate for my  operating system in my case i have a  64-bit operating system and i’m going to  choose this thread safe version so if  you have a 32-bit operating system you  can  pick this guy up here in my case i’m  going to pick this one  and you’ll see here there’s an option  for a zip file so i’m just going to  click on this and that’s going to go  ahead and download this zip file and  this zip file has all the files that we  need to start using php  so when that is finished downloading  let’s just hop over to our downloads  folder and you’ll see here we have this  zip file so what i want to do is i  actually want to extract this so i’m  going to go ahead and extract all the  files that are in here into a another  folder so i’m just going to click  extract all and down here i basically  just want to type in the name of the  folder where i want to store these  in my case and just for simplicity’s  sake i’m going to go ahead and just  store these at the root directory of the  c drive and i’m going to store them  inside of a folder called php so i’m  basically making this folder here php  and i’m going to go ahead and extract  all of these files into that folder so  once we’ve typed that in i’m just going  to click extract and this is going to go  off and extract all those files into a  new folder called php at the root  directory of our c drive here’s the  thing if you want to put them somewhere  else you can put them somewhere else but  to fully follow along with what i’m  doing you can put them at the root  directory of your c drive all right when  that’s finished extracting you’ll see  that it opens up this php folder here so  here i am on the c drive and over here  we have our php folder and in here  there’s just a bunch of files so you  really don’t have to worry about any of  these files obviously don’t touch any of  them or modify any of them but as long  as we have these here in this folder  then we’re ready to go so we have  everything downloaded now we have to do  one more thing which is called  configuring our windows path variable  essentially what this means is we have  to tell windows that php is inside of  this folder so eventually we’re going to  want to run php and use it and use all  of its functionality in order to do that  we have to tell windows about it so we  basically just have to tell windows  where we put php so down here i’m going  to go in my search bar and i’m basically  just going to start typing in  environment so  e-n-v-i-r-o-n and this option over here  should pop up it says edit the system  environment variables this is what we  want so we’re going to click on that and  this should open up a window where we  can configure our path variable so this  window is going to open up here there  should be an option down here that says  environment variables you want to click  that  and over here there’s going to be a  couple different options we want to go  over here to this where it says variable  path so wherever you see this path  variable i’m just going to click edit so  we want to edit this variable and  basically this is just a windows system  variable that kind of tells windows  where a bunch of executable files are so  what we want to do is we want to tell  windows about php and we can do that by  telling windows where it is inside of  this path variable so i’m just going to  click new over here  and then down here in this little  typing box  we can just type in capital c colon  backslash  php so i’m basically typing in the  location of the folder where i extracted  all those php files so once we do that  we can just click ok  and that’s going to go ahead and be  added to the path variable so i’m going  to click out of this click ok click ok  over here  and now we want to make sure that that  worked so once again i’m going to i’m  going to go down to the search bar and  i’m just going to type in cmd this time  and this option over here for command  prompt should show up so you want to  click that  and the command prompt is basically just  a way that we can interact with the  computer using text commands so down  here in the command prompt the first  thing i’m going to do is just type in  echo  percent sign  p-a-t-h and all capitals and then  percent sign and this is going to go  ahead and print out that path variable  so we want to make sure that the entry  php is in here so we want to make sure  that that folder is showing up there as  long as it’s showing up there then  we were we successfully updated the  path variable now the last thing that we  want to do is just uh check to make sure  php is working so i’m just going to type  in php  hyphen v and i’m going to hit enter and  we should get this version number  popping up for php so as long as you get  a version number popping up here and  you’re not getting any errors  then you’ve officially installed php on  your computer and believe it or not  that’s actually all we’re going to need  to get started writing php and building  an awesome website so  stick around for the next tutorial and  i’m going to show you guys how to get  started creating your first php file  in this tutorial i’m going to talk to  you guys about choosing a text editor  for php so whenever we’re writing our  php programs one of the most important  parts of the whole process is going to  be the environment where we’re writing  our php code  and really with php it’s simple any text  editor is going to work any text editor  where you can save a dot php file  is going to be able to support php so  you don’t need some special  configuration you don’t need some  special text editor any old text editor  will do now if you already have  experience working with something like  html or css or javascript then the text  editor that you use for those  programming languages can be used for  php so generally you’re going to be  writing your php alongside something  like css or html or javascript so if you  already have a text editor that you’re  comfortable with in those languages then  you can just use that same text editor  but if you’re new to all this you’re new  to web development and stuff like that  then i’ll kind of walk you guys through  what your options are i mean really your  options range from like the simplest of  text editors to something that is like  super specialized for php really all you  need is like notepad or text edit you  know the simplest text editor that comes  with your operating system but a lot of  people will like to use more of like a  specialized text editor you know  something that is designed to support  the php language something that will  give you things like syntax highlighting  and you know can maybe even like show  you where the errors are in your code  and really just with a simple google  search you could find a bunch of  different text editors that are designed  and support php in this tutorial i’m  going to be using a text editor called  adam and this is a text editor that was  created by github and it’s just a text  editor that i personally like to use  and so i’m actually going to show you  guys how you can download and install  that but i also do just want to say that  just because i’m using this atom text  editor doesn’t mean that you have to  like i said there’s a lot of these  different text editors out there and  really you know the best text editor for  you to use is going to be the one that  you’re most comfortable with so what i  would do is just do a quick google  search you know look up and what text  editors seem to work for people with php  and you can kind of get an idea of what  you can use but like i said any old text  editor is going to work so  well i’m going to go ahead and download  the atom text editor so if you don’t  really care what you’re going to use  then you can just follow along with me  and we’ll be using the same text editor  throughout the course so i’m going to  come down here to my web browser  and i’m going to go ahead and just do a  google search for adam  text editor  and  this should pop up here it’s this  website atom.io so i’m just going to  click this and depending on the  operating system you’re using a option  should come up here for downloading it  so if you’re on mac this would say like  download os x i’m on windows so i’m just  going to click this and it’s going to go  ahead and give me this atom setup.exe  file so i’m going to go ahead and run  that now that i downloaded it and you  can see over here it’s starting to  install on my computer so basically all  you need to do is wait for that to  download wait for it to install and you  can go ahead and start using it for the  rest of this course but like i said you  don’t need to use this text editor  that’s just the one that i’m going to be  using and i wanted to give you guys kind  of an option if you don’t you know  really have a preference but you know  what you should do is find a text editor  that you’re going to be comfortable with  going forward especially if you’re going  to be writing a lot of code so yeah  that’s kind of you know the basics of  choosing a text editor so hopefully that  makes sense and hopefully you guys can  find a text editor that you can use for  the rest of this course  in this tutorial i’m going to show you  guys how to get your first php file up  and running so we’re going to set  everything up that we need to set up  we’ll create a php file and then we’ll  actually print something out onto the  screen so we can make sure everything is  working correctly so  let’s get started the first thing that  we have to do when we’re working with  php is we actually have to start  something called a php server now  basically when we want to use php we  have to run php on a web server so php  is what’s called a server side language  basically means it’s a programming  language that’s going to run on a web  server and we can use php on that web  server in order to interact with our  websites so your website is basically  just a collection of files that gets  given to the user and we can write php  on the web server which is going to  handle like giving out files to users or  getting input from users and doing stuff  like that so  basically all you need to know is that  php is going to run on a web server and  you don’t really need to understand any  more than that just as a beginner to php  and i’m going to show you guys how we  can set up our very own web server and  actually a web server comes with php so  it’s really easy so what i want to do is  i want to open up my terminal or my  command prompt now if you’re on mac it’s  going to be called the terminal if  you’re on windows it’s going to be  called the command prompt and i’m just  going to come down here and just search  for the command prompt i’m going to  search cm because i’m on windows if  you’re on mac just search for terminal  and i’m going to open this up and the  command prompt in the terminal are  basically just two programs which will  allow us to  interact with the computer using text  commands that’s essentially all you need  to know  and over here we’re actually going to  start a web server so you can just type  in php  and i’m going to type hyphen a capital s  and then i’m just going to type  localhost colon 4000  and then i’m going to click enter  essentially what we’re doing here is  we’re creating a web server so we’re  using php  and php is going to create like a little  web server for us and as a beginner to  php this is essentially all you need so  we can use this server that php is going  to create for us in order to run our php  files  you can actually use a different web  server if you want setting up a like  another web server on your computer is  gonna take a a while and you’re gonna  have to do a bunch of configuration  you’re gonna have to download a bunch of  things so as a beginner the easiest way  to get started is to just use this php  hyphen capital s localhost 4000 commands  and you’ll see over here basically just  says php  7.1.11 development server started so  this is like a little web server that we  can use  and you’ll see over here it says  listening on http localhost 4000. so  localhost is it’s essentially just like  the web address  of your local computer and this  basically just means that we have a web  server running on our local machine  and it’s running on port 4000 and then  down here you’ll see it says document  root is  c backslash users backslash mic d now if  you’re on os x this might be different  it’s probably still going to be like  users and then your username so that’s  basically where php is going to start  looking for  files that we’re going to run so now  that we have our server set up you want  to just leave this running so don’t exit  out of this window i’m just going to  minimize it but make sure that you don’t  exit out of it and what i’m going to do  is i’m going to come down here in my  file explorer and i’m going to go to  that document root so if you remember it  was a users  and then my username so i’m just going  to go down here  and we’ll go to users and then mic d so  this is where that document root was in  other words this is where php is gonna  start looking for files that we wanna  run so what i’m gonna do is i’m actually  just gonna create a new folder here so  i’m just gonna click new folder  and i’m just going to call this folder  www so www is just going to stand for  like our website you can name it  whatever you want but i’m just going to  name my www and now what i want to do  once i have this www folder created is i  want to create a php file and to do that  i’m actually going to open up my text  editor so over here i’m going to open up  adam which is the text editor that we  installed in the last tutorial  and if you’re not using atom then you  can just use whatever  text editor that you want it doesn’t  really matter and in here i’m basically  going to  import this folder so i’m just going to  come up and say file and i’m going to  say add project folder and now we’re  just going to go to  users mike d  and then we’re going to add this www  folder and this is basically just going  to let me  see it while we’re developing so i’m  going to click select folder so now we  have www over here in our little  navigation window and i’m going to right  click and i’m going to click new file  and we want to create a php file so i’m  just going to create a php file and i’m  just going to call it  site.php and you can name this whatever  you want it doesn’t matter what you name  it i’m going to i’m gonna name this site  and you need to have this dot php  extension so i’m just gonna click enter  and this file is gonna go ahead and get  created for us so once we have this file  open now we can start writing some code  now here’s the thing about php is php is  very tightly coupled with a language  called html and html stands for hyper  text markup language it’s basically a  language that we can use to  build websites so if you’ve ever built a  website before then you’ve used html now  in order to program in php in order to  really get everything you can out of php  you’re going to want to have a good  understanding of html by no means you  have to be an expert at html but you  should at least understand like what  html is and how it works and sort of  what’s going on i’m just going to kind  of assume that you guys have a basic  knowledge of html and if you don’t know  anything about html we actually have an  entire course on it  in here on draft academy so you can  check that out but for this course i’m  just going to assume that you already  understand the basics of html so i’m not  going to be going over html basics i’m  just going to assume that you know it  so this php file over here site.php this  is actually very similar to an html file  so essentially we can write html inside  of this file and it’s going to work just  like it would work with a normal html  file for all intents and purposes a php  file and an html file are the same  it’s just that in a php file we can  write php  code but everything else is pretty much  the same so it’ll work the same so what  i’m going to do is i’m actually going to  come over here and just set up a basic  html skeleton  so here we just have this very basic  skeleton for an html page  and inside of this html page we’re  actually going to write our php code  so whenever we want to write php code  inside of an html file we could actually  just come down here and i’m just going  to come down here into the body and we  can create special php tags so  everything in html is a tag right so  anytime that you’re like laying out  something or you’re doing something  generally you’re doing it inside of a  tag and we can create a php tag  and inside of that php tag we can write  our php code so a php tag is very simple  i’m just going to make a less than sign  i’m going to make a question mark and  i’m going to type out php  and then i’m just going to make a couple  new lines and you’ll see down here we  need a question mark and a greater than  sign to end it off so anything that i  put inside of these tags is going to be  considered php code all right and i’m  just going to show you guys one simple  php instruction that we can write and  then in the next tutorial we’re going to  look at some more instructions but i  just want to show you guys like how we  can basically set this up and make sure  that everything’s working i’m just going  to type out echo and i’m going to make  an open and close parentheses and i’m  going to type a semicolon after this and  in here i’m going to make an open and  close quotation marks and i’m just going  to type out hello  world so i’m saying echo hello world and  this is actually a php instruction what  this is going to do is it’s going to  print something out onto the screen so  what we can do now is we can save this  file and remember over here i still have  my php server running and it’s saying  document root is user’s mic d  and it has this address up here local  host 4000. i’m going to go ahead and  copy this  and we’re going to put this into our web  browser so i’m gonna open up my web  browser in my case i just have google  chrome  and over here in the address bar i’m  gonna put in localhost  4000. now when we click enter you’ll  notice that it says not found and that’s  because  there’s no php files here at the root  directory but if you remember we stored  our php file in that www folder so i can  just make a forward slash www and then i  can say forward slash and i’m going to  type in the name of the php file so it  was site.php  and so now we’re navigating to the  folder where we’re storing all of our  files and then i’m typing in the file  name and now when i click enter it  should bring us over here to this page  and you’ll see when i go to site.php  it’s printing out onto the screen hello  world so basically we have successfully  run our first php program we have  everything set up and we’re ready to  start working some more  in this tutorial i’m going to show you  guys how you can use the echo command in  php to write html from inside of your  php code so this is going to be kind of  cool now in the last tutorial i showed  you guys this very basic php command  called echo and we basically just typed  out echo and open and close parentheses  and open and close quotation mark and  then we typed out hello world and then  finally we ended this off with a  semicolon and when i was when i went  over here and refreshed my browser we  saw that hello world showed up over here  and that’s kind of just like a super  basic line of code so i’m going to  actually show you guys  sort of what this does and we’re going  to break it down so echo is basically a  command in php which allows us to write  information out onto the html document  so if i was to come over here and i  viewed the page source of this html  document you’ll see over here that we’re  actually printing out hello world  inside of the actual document so when i  came over here and i said echo hello  world this line of text got printed out  inside of our html file and that’s  basically what that echo command does is  it it allows us to write html out to our  html files and that’s kind of cool so  basically the way the echo command works  is i just say echo and i can put these  open and close parentheses and then i  can just basically type out whatever  html i want to put onto the screen and  then after this line of code i’m going  to put this semicolon and that semicolon  is actually really important anytime we  write a line of code in php we want to  make sure that we include that semicolon  and actually with this echo command if i  wanted i could get rid of these  parentheses and you could just leave it  like this so i could either use it like  this or i could use it with parentheses  around it so i’m going to show you guys  some cool stuff we can do with this echo  command in addition to just typing out  regular text i could also type out html  code inside of these quotation marks so  for example let’s say i wanted to print  out a header for my website i could go  in here and just type out h1 and i could  say like  mike’s  site and then i could end off this h1  and now when i run my  website over here when i refresh the  page you’ll see that this is actually  getting rendered as an h1 and if we came  over here and refreshed this source  you’ll see that an h1 is actually  getting put onto the page over here so  not only was i typing out text but i was  also typing out html and that’s one of  the cool things about this echo command  is essentially inside of these quotation  marks i could put any valid html that i  want and it’ll actually get rendered  over there on the browser so this is  really cool and if i wanted i could just  like make another one of these so we  could come down here and we could say  echo and now i can make like a paragraph  for example so i could say like  this is my site and we’ll end this off  and actually if i wanted i could also  put like a horizontal rule here so i  could say like  hr  and then i also want to remember anytime  i write out a line of code in php i  always want to put this semicolon here  so i’m going to put a semicolon there  and a semicolon there and that basically  just tells php like hey we’re done  writing this line of code and now we’re  going to write another line of code so  now when i head over to my web browser  you guys will see that we’re actually  getting this little website so it says  like mike site we have the horizontal  rule and then it says this is my site so  from inside that php code we’re actually  able to write out an entire website and  if i wanted i could you know include as  much text or as much html in here as  possible and this is something that’s  going to come in handy when we’re using  php and as we go forward in this course  anytime i want to like print something  out or show you guys something i’m going  to be using this echo command to do it  and so real quick i also want to just  talk to you guys about how this code  gets executed so whenever we um request  this website over here so for example  whenever i like refresh this webpage  essentially what’s happening is the  little web server that we set up is  going to serve this page so you’ll see  over here when i refresh the page  it comes over here and it’s basically  saying like  www.forwardsite.php so this web server  is actually serving up this php page to  us and whenever we refresh that page  over there this php code is going to get  executed by that web server so when i  click the refresh button over here or  when i navigate to this website this web  server is actually going to come down  here and it’s going to go into these php  blocks and it’s going to execute all of  the php code that’s inside of here so  when i request the page or when i  refresh the page all this code for our  website is actually getting rendered so  the php code is going to get executed  and then this code is basically just  going to get placed into the file and  then we’ll have our finished html file  and that’s kind of how it works and  whenever we specify these php  instructions over here they’re going to  get executed in order so you’ll notice  that on my website it says mike’s site  then the horizontal rule then the  paragraph and that’s because that’s the  order that i specify these instructions  if i was to take this line  and i moved it up here above the header  one now this is going to show up first  and that’s because php is going to  execute these instructions in order so  it’s going to execute this instruction  then this instruction then this  instruction and so over here on our  website  you’ll see now that this paragraph is  actually showing up above this header  one and that’s because we change the  order of the code so this is really just  like the bare basics of php basically  any of the php that we put in here when  the user requests this webpage so when  the user requests site.php  this php code is going to get executed  and this file is going to get put  together so it’s basically going to  render the php file that we see over  here on the browser with all of these  html tags and then we’ll be able to see  like the finished product so that’s sort  of the basics and you’ll see down here  we’re just using these simple  instructions so i’m using this echo  instruction but as we go through this  course we’re going to learn more and  more complex instructions which are  going to allow us to do more and more  complex things so i’m really excited to  show you guys some more stuff we can do  in php and in the next tutorial we’re  going to be looking into another way  that we can leverage the power of php on  our website  in this tutorial i’m going to talk to  you guys about variables in php now a  lot of times in our php programs we’re  going to be dealing with all sorts of  data and information and a lot of times  we’re going to want to be able to  maintain and keep track of that data in  our programs and we can actually use  variables in order to do that so in this  tutorial i’m going to show you guys the  basics of working with variables and  using variables and we’re going to kind  of get up to speed with what variables  are so down here in my php tags i have  this very simple program set up and it’s  basically just  echoing out a bunch of text onto the  screen so it’s saying there once was a  man named george he was 70 years old he  really liked the name george but didn’t  like being 70. so this is basically like  a little story that i wrote and all this  program does is write that story out  onto the screen so you can see over here  on my browser it just is printing out  that story so everything works this is a  valid program right you’ll notice that  it’s it’s very simple but let’s say that  inside of my story i’m looking at it and  i’m thinking to myself hmm  i think i want to change the character’s  name  well in order to do that i can basically  just go into my story and every place  the character’s name is mentioned i can  just change it so let’s say instead of  being named george i want the character  to be named john so i’m going to come  over here and we’ll change this to john  and we can change this down here to john  as well so now i’ve successfully updated  the character’s name right so every  place in the story where the character’s  name was mentioned i updated it to the  new value let’s say that i’m looking at  my story again and i’m thinking hmm i  think i also want to change the  character’s name  well i could basically do the same thing  right i could head down here and  everywhere the character’s age is  mentioned i can change it so why don’t  we change the character’s age to be a  little younger maybe 35 right so i’ll  change it up here and then i’ll also  change it down here  so now  i’ve updated the character’s name and  i’ve updated the character’s age inside  of my program so now if we were to  refresh the page you’ll see that all of  that information got updated in the  story right so so that works perfectly  right it makes sense um but here’s the  problem though in order for me to change  the character’s name and for me to  change the character’s age i had to go  through and manually modify both of  those attributes right i had to manually  go in here and change the name in both  of these spots and i had to do the same  thing for the age and this is fine for a  story with four lines right but imagine  that this story was like hundreds of  lines and we mentioned the character’s  name hundreds of times we mentioned the  characters age hundreds of times well  having to go in and manually change  their name or manually change their age  if i wanted to update it would be  extremely tedious and difficult right  and i would probably make a mistake  somewhere you know where i wouldn’t  catch it  and this is actually a situation where  we can use something called a variable  so a lot of times in our programs we’re  going to have certain pieces of  information or certain data values that  we want to keep track of and we want to  sort of organize and in this case we  have two pieces of information the  character’s name and the character’s age  so what we can do is we can actually  take these values and we can store them  in something called a variable and a  variable is basically just a container  where we can store pieces of information  in our program so i could create a  variable and then i could store the  character’s age and the character’s name  inside of it so let’s go ahead and do  that i’m going to create two variables  one for the name and one for the age and  i’m just going to come over here and  right above this echo instruction i’m  going to make a new instruction  basically we’re going to create a  variable which again is just a container  now anytime you want to create a  variable in php the first thing we have  to do is type in this dollar sign so  whenever you type a dollar sign like  that it’s basically telling php that you  want to create a variable so after the  dollar sign we want to name our  variables so remember a variable is just  a container where we’re storing a piece  of information so what i want to do is i  want to give this variable a descriptive  name which will tell me what piece of  information is stored inside of it so in  our case i’m going to call this  character name because it’s going to  store the character’s name so i’m just  going to say  character  name  and now that i’ve given this a name i  want to give this a value so i can just  say character name and i can just say  equals and i’m going to make these  quotation marks and i’m just going to  type out the character’s name john and  then like always i’m going to put a  semicolon there to end off the  instruction  and so down below here we also want to  make another variable so we also in  addition to storing the character’s name  john we also want to store their age so  once again i’m just going to type a  dollar sign and now i’m going to call  this variable  character age  and i’m going to set this equal to the  character’s age which is going to be 35.  so you’ll notice over here when i wanted  to store text inside of a variable i had  to use these quotation marks and when i  wanted to store a number inside of a  variable i could just type out the  number like that and that’s kind of  different ways that we can store  information so now that we’ve stored the  character’s name and the character’s age  inside of variables what i can do is i  can actually replace every instance of  the character’s name and age inside of  the story with that variable and you’ll  see in a second why that can be useful  so what i’m going to do is i’m going to  come down here to my story and  over here for example we’re typing out  the character’s name so instead of  having to type out john what i could  actually do is i could get rid of this  and inside of this little string of text  here  i could just type a dollar sign  and now i can type in the name of the  variable that i want to place inside of  here so i could just type out  character name and you’ll see that this  is actually getting highlighted a  different color and basically when we  put this dollar sign and we type out the  variable name here in this text so in  between these quotation marks this is  telling php that we want to insert the  value that’s stored  inside of this variable into our little  print statement here so now if i was to  come over here and i was to refresh the  web page you’ll see that we’re still  printing out there once was a man named  john but we didn’t actually type out  john remember all we did was we just  included this variable name over here in  the story and it was actually able to  show up over here  on the website and so what php did was  it saw that we wanted to include the  value of the character name variable  inside of here and it went up grabbed  the value and basically just inserted it  here into our story so we can actually  do that now for every occurrence of the  character’s name so i could come down  here and i could do the same thing so  i’m going to say character name  and we can do the same for the  character’s age so down here we have  characters age so i’m just going to type  in character age  and then over here we can do the same  thing so i’m just going to do  character age  and so now even though i’m not  physically typing out 35 and john these  are still gonna show up in my story  so you’ll see over here we get the same  exact thing it’s still using the name  john and it’s still using the age 35 so  what’s useful about these variables is  that they allow us to store and manage  these different values in our programs  these different pieces of information  that we’re using  in our programs so let’s say that now i  wanted to update the character’s name  instead of having to go through and  manually change it in all the places in  my story i can actually just come up  here and i can modify it so let’s say i  wanted to change the character’s name to  tom well i could just type tom over here  in one spot so i changed the variable in  one place and now it’s going to  automatically be updated throughout my  story so now when i refresh my program  you’ll see instead of using the name  john it’s using the name tom i could do  the same thing for the age so why don’t  we make him  80 years old and now tom is going to be  80 years old over here in my program so  that is a really awesome way that we can  maintain and keep track of the different  pieces of information in our programs  and really the point is that i can use  this character name variable and then if  i want to change the value i just change  it up here when i assign it a value  and another cool thing that we can do  with these variables is we can actually  modify them throughout our program so  let’s say that halfway through this  story i wanted to change the character’s  name what i could do is i could actually  come down here and i’m going to make a  new line  and let’s say halfway through the story  we’ll change the character’s name i  could just say dollar sign  character name and i could just give  this a new value  so i could just give this the value of  like mike for example and now halfway  through our story the character’s name  is going to change from tom to mike so  you’ll see over here the first part of  the story it’s using the name tom and  the second part of the story it’s using  the name mike and basically you can just  update these variables as you go through  your program you can like change their  values and do different things with them  so that’s kind of the basics of  variables variables are containers  where we can store different pieces of  information that we want to keep track  of in our program now you’ll notice that  not all pieces of information are going  to get stored in variables so for  example i didn’t store like the word  once or the word man inside of a  variable right i stored the pieces of  information  that i was using multiple times  throughout my program like the  character’s name and the character’s age  these are both values that i could store  about the character and therefore i put  them in variables in order to make them  easier to use and then whenever i wanted  to access those values i just referred  to the variable’s name instead of having  to like physically type out the value so  that’s really the basics of variables  and you’ll see over here we’re able to  store like text inside of variables  we’re also able to store numbers inside  of variables  and there’s also some other different  types of data that we can store and  represent and work with in php and in  the next tutorial i’m going to talk to  you guys all about the different data  types that we can represent in php  in this tutorial i’m going to talk to  you guys about data types in php now in  php we’re going to be working with all  different types of information and as we  you know go forward in the course and we  start writing more and more complex  programs we’re going to be dealing with  all different types of information and  data so in this tutorial i just kind of  want to talk to you guys about the  different types of data that we can work  with and represent inside of our php  programs and it’s actually pretty simple  so down here in these little php tags  i’m actually just going to be creating a  couple different variables and we’ll be  storing different types of data inside  of those variables so the first type of  data that we can work with and represent  in php is what’s called a string and a  string is basically just plain text so  any time in php where we want to you  know work with plain text whether it’s  like a name or you know it could be like  a location or a line in a book or you  know anything that is plain text we can  use a string so i’m going to create a  variable over here we’ll just call it  like phrase and  whenever i want to create a string i’m  always going to put an open close  quotation mark so any time that we’re  writing out a string or we’re working  with a string it’s going to basically  just be text inside of these open and  close quotation marks  and any time you have the quotation  marks there php is going to know like  hey this is going to be a string so i  could just put a phrase like to be or  not to be right so this is just any text  that i want to store  or work with or represent in my program  can be a string like this  in addition to plain text we can also  represent numbers and there’s two basic  types of numbers in php there’s uh whole  numbers and then there’s decimal numbers  and php has special names for these the  first is going to be an integer so an  integer is basically going to be a whole  number and so those are just like  counting numbers like 1 2 3 4 5. it’s  essentially just a number without a  decimal point so if i was to store  someone’s age inside of a variable i can  make this a number and with numbers all  you have to do is just type out the  number so let’s say someone was like 30  years old i can just type out 30 here i  don’t need quotation marks i can just  put the number here and with numbers and  php you can also make them negative so i  can make this a negative number um but  you’ll notice here this number doesn’t  have a decimal point so there’s no  decimal point you know with like a bunch  of numbers after it um it’s just a whole  number and this is what we would refer  to as an integer and it’s important to  know the difference between  decimal numbers and integers because php  is actually going to distinguish between  them so just know that anytime you have  a number without a decimal point it’s an  integer we can also use a decimal number  um sometimes people will call these  floating point numbers or they’ll just  call them like floats and these are  pretty straightforward it’s any number  with a decimal point so  maybe we could store someone’s gpa so  let’s say their gpa was like a 3.7 right  or it could even be like a 3.0 or  2.98743 you know basically any decimal  number that you want to work with and  represent in php and this is like i said  it’s a lot of people will call these  like floating point numbers or decimal  numbers but it is important to  distinguish between these two so there’s  a big difference in php between 30 and  30.0 there is going to be a difference  between the two of those this one  obviously being a decimal all right so  there’s one more um data type one more  main data type that we can work with and  sort of represent in php which is called  a boolean and a boolean is basically a  true or a false value so this is  probably a little bit less intuitive  than these guys over here like text and  numbers that’s pretty obvious  but a lot of times when we’re  programming we’re going to want to  represent and work with true or false  data so we’re going to want to be able  to store like true or false information  and we can store that on something  called a boolean so i’m going to create  a boolean variable  and why don’t we call this like is male  so this variable could tell us whether  or not someone’s a male and i could  either set this equal to true if they’re  male or i could set this equal to false  so any time we have a boolean variable  or any time you’re representing a  boolean value it can only be one of two  things either true or false it’s like a  binary data type and this is going to  come in handy a lot in php and we’ll  kind of get into you know how booleans  are useful more later in the course but  for now just know that you can store a  true or a false value inside of a  boolean so there’s one more data type i  guess it’s kind of a data type it’s  basically a value that you’ll see  sometimes in php and it’s the null value  so anytime you see null like this in php  this basically stands for no value  and a lot of times in php we’re gonna  kind of go out of our way to say  something has no value so sometimes  you’ll see like maybe an error message  or you’ll try to print something out and  it’ll basically just say no  and that’s because it has no value so  anytime something has no value php will  denote that using this null keyword so  these are really the basic data types  and as we go through this course and  really as you go through your php  journey  i’d say 99 of the time you’re just going  to be using these data types you’re just  going to be representing information in  the form of text numbers or booleans and  really with just this information you  can you know essentially build any  program that you want i also do just  want to point out that you know we don’t  always have to store this information  inside of variables so if i was to come  down here  i could just echo out like a string  so i over here i’m printing out a string  onto the browser so over here it says  like hello right i could do the same  thing for a number so i could just put a  number down here like 4.57  and it’s gonna be the same thing so you  know you don’t always have to store  these things in variables although if  you wanted we could like you know i  could print out like this phrase  variable and this is going to get  printed out onto the screen as well so  that’s kind of the basics of you know  different data types in php and really  you know these are data types that  you’re going to be seeing as we go  forward in the course so i just wanted  to kind of give you guys an introduction  into what they were and how we can use  them  in this tutorial i’m going to show you  guys the ins and outs of working with  strings in php now a lot of times when  you’re writing your php code one of the  most common data types one of the most  common types of information that we’re  going to be working with is going to be  strings and strings are basically just  plain text so anytime in your php  program where you want to represent or  use plain text you’re going to be  working with strings so in this tutorial  i’m going to just show you guys a broad  overview of how to use strings and all  the different things that we can do with  them so let’s get started down here in  my php tags  i want to show you guys basically how we  could print out a string so i could just  say like echo and then whenever i create  a string i always want to make these  open and close quotation marks and then  in here i could type anything so i could  just type like  draft academy and now when i refresh my  browser this is going to get printed out  over here so this is a string this is  plain text and really you know this can  just be anything you want so any plain  text that you want to represent or use  can be placed inside of a string and in  addition to just printing out the string  over here directly we could also store a  string inside of a variable a variable  is basically just a container that will  allow us to manage that string a lot  easier so i could come over here we  could call this like phrase and i could  set this equal to this string over here  and now if i wanted to print this out  instead of printing out the actual  string i could just  print out the variable and this will  print out the value that’s stored inside  of that variable so you can see over  here we’re still getting draft academy  now strings can be very cool and when  we’re writing our php code there’s  actually these little things called  functions which we can use to find out  information about and modify our strings  so i could use one of these functions in  order to do special things with the  strings in my program and i’m going to  show you guys a couple of those string  functions which we can use so down here  in this little echo command i’m going to  go ahead and use one of these string  functions and like i said these are  basically just like little snippets of  code that we can call and they’ll do  something to the string so they’ll give  us some information about the string or  they’ll like modify the string in some  way so i’m going to show you guys a  couple of those and you can just kind of  like see how they work one is going to  allow us to convert this to either all  uppercase or all lowercase so in certain  circumstances in your programs you’re  going to want to make the string upper  or lower case and we can do that so i  could just say str to  lower and then i’m going to make an open  and close parentheses and i’m actually  going to surround this string with these  open and close parentheses so i’m saying  str2 lower  open and close parentheses and then  inside of these parentheses we’re  putting this variable and now what  you’ll see is when i refresh my browser  this is actually all lowercase now so  you can see it converted entirely into  lowercase we could also do the same  thing for upper so i could say string to  upper and now this will convert it  entirely into uppercase so you can see  now we just get draft academy all uppers  and you don’t just have to pass a  variable in here i could pass anything i  wanted in here so if i passed in like  dog  now this will print out dog in all  uppercase like that and so that’s kind  of like a useful way to you know take a  string that you have in your program and  convert it to upper or lower case  there’s another one of these little  functions which will tell us how many  characters are inside of the string so i  could say str len so this stands for  string length and this will tell us how  many characters are in that string so  over here we get 15  because there’s 15 characters in draft  academy and a lot of times in our  programs that will come in handy we’ll  be able to figure out like how many  characters are inside of a string  another thing we can do is we can  actually find out  what the different characters of the of  the string are so over here i have this  string draft academy right it’s storing  the text draft academy inside of it  let’s say that i wanted to figure out  what the first character in this string  was so i wanted to figure out like  what’s the first character in the string  well i can actually come over here and  i’m going to get rid of this string  length so you’ll notice i just erased  all of that and over here after i type  out the variable name i’m going to type  an open and closed square bracket just  like that so right after the string i’m  going to type this open and close square  bracket and in here i can put what’s  called an index and the index is  basically going to refer to a specific  character in the string so let’s say i  wanted to figure out what the first  character in the string was i could  actually put a 0 in here and this is now  going to print out the first character  in this string you’ll see when i refresh  my page over here we get this g because  that’s the first character in the string  if i wanted to print out the second  character in the string i could come  down here and say  phrase square brackets one and now i’m  going to get that i because i is the  second character in the string so  whenever we’re using these square  brackets like i said we can put an index  in here and it’ll tell us what character  is it that index position in the string  and whenever we have a string like this  in php we’re gonna start the indexing  off at zero so i would say that this  first character g is at index position  zero in the string the second character  i is at index position 1 r is at 2 a 3 f  4 etc so when we start indexing these  strings in other words when i assign  index positions to the characters in the  string we always start at zero so that’s  why down here when i said phrase zero  this printed out that g  and also i can do this with things other  than just a variable so again if i came  down here and i just like printed out my  name this will tell me what the first  character in that string was so we’re  going to get this capital m another  thing we can do is we can actually  modify individual indexes in the string  so i could actually come down here and i  could say like  phrase and i could say like phrase zero  is equal to and i could change this to a  b and now if i was to just print out the  phrase it’s going to actually replace  phrase zero with b  so now it’s gonna say braf academy  instead of draft academy and that can be  really useful so you can actually modify  like individual characters inside of  your string all right so i want to show  you guys a couple more of these little  string functions these little things  that we can do to modify these strings  and another one is actually going to  allow us to replace  certain sub strings within our string so  over here i have this phrase variable  and it has this string and i can  actually replace like the word draft  with another word if i wanted to so  let’s say instead of it calling this  draft academy we wanted to call this  like panda academy well there’s another  one of these little functions which will  allow us to do that so down here i’m  just going to type str underscore  replace and this stands for string  replace  and over here the first thing i’m going  to put inside of these open and close  parentheses is going to be the substring  that i want to replace so i’m going to  put draft inside of here  so i can just say draft and then over  here i want to put what i want to  replace it with so we could put panda  and then finally we’re going to put one  more thing in here which is going to be  the actual string where we want to do  this so i can just say  phrase and now this is going to replace  the occurrence of draft  with panda in the phrase string so over  here i can just run this program and you  can see now it says panda academy  instead of draft academy and you could  really do this with anything so i could  say like you know just ffv for example  and then we could replace that with  panda and now it’s gonna be like this  you know weird word over here that we  get so that string replace can be really  useful and there’ll be a lot of  circumstances where you might want to  like replace a certain character or a  certain sequence of characters with  another sequence of characters finally  there’s one more of these that i want to  show you guys and it’s basically um  going to allow us to get like a sub  string  so over here i could just type out suv  str and basically this is going to allow  us to just grab like a section  of this overall string over here so i  could just grab like a single word from  it or i could grab just like a couple  different characters and this will allow  us to do that so i’m going to make an  open and closed parenthesis after i say  substring and now inside of here i want  to pass a couple different  pieces of information so i can come over  here the first thing i’m going to do is  type out the string that i want to get  the substring from then i’m going to  type a comma and over here we can type a  starting index so this will basically be  the index where we want to start  grabbing uh the sub string from so  remember i have all of these different  characters in this string have indexes  so like this is index position 0 1 2 3  etc let’s say that we wanted to use this  substring function to grab this academy  word over here well the first thing i  need to do is figure out at what index  academy starts so we could actually just  start counting it’s 0 1 2 3 4 5 6 7 8.  so if i put an 8 right here now this is  going to grab a sub string from this  string starting at index position 8 and  going to the end so we should just get  academy and you can see over here we  just get academy i could also put  another number in here which is going to  be a length and this will basically say  how many characters we want to grab so  if i said like three in here for example  this is going to grab one two three  characters so it’s gonna grab aca and  then it’s gonna stop so over here we can  get this little substring just aca and  this little function can come in handy  quite a bit so that’s sort of just some  basic um string functions that you guys  can use in php and to be honest with you  there’s a lot more of these you know i  could spend like a couple hours just  going through each of the functions and  what they do but really what i wanted to  do in this tutorial was just kind of  expose you guys to what this stuff is  and expose you to you know some  different ways that we can work with and  modify strings if you’d like to find out  more about these string functions you  can basically just type into google like  php string functions and they’ll be like  a huge list  of all of these awesome string functions  that you guys can use but for this  tutorial i think that’s good and it  gives you guys a little introduction  into how to use it  in this tutorial i’m going to show you  guys the ins and outs of working with  numbers in php now one of the most  important data types in php is numbers  and a lot of times in our programs we’re  going to be working with and doing  different things with numbers so i  really just want to give you guys a full  introduction into  what we can do with numbers how we can  use them in php and i’m also going to  show you guys some little cool math  operations that php is going to allow us  to do by default so this is going to be  pretty cool and down here in my php tags  i’ll just sort of show you guys the bare  basics so i could basically just like if  i wanted to i could print out a number  and whenever we’re typing on a number in  php you can just type it out you don’t  need to use any quotation marks you  don’t need to use any like special  characters you just type out the number  and like this will show up over here on  the screen so we get 40. php can also  handle negative numbers so i could make  a negative number just like that i could  also make a decimal number so i could  say like um you know eight four seven or  something and now we have like a  negative decimal number and it’s  important that you understand the two  distinct types of numbers so and php is  going to differentiate between the two  a little bit so this for example like 40  would be a whole number it doesn’t have  a decimal point so this is like what we  would call an integer number  but then over here if i put a decimal  point on here  now all of a sudden this is what we  would call a floating point number or a  decimal number so this just has decimal  points after it so you want to just make  sure that you’re aware of the difference  between those two types of numbers  honestly it’s not going to like affect  anything that much but just so you’re  aware that there are those two distinct  types of numbers all right so in  addition to just like printing out a  number over here i could also do  arithmetic so i could perform like math  operations so i could say like echo five  plus nine and now instead of echoing  five plus nine this is actually going to  echo out the answer of five plus nine so  it’s going to go ahead and add both of  these together and over here we should  get 14. so you can see php actually  solved this little math equation that we  gave it so we can use addition we could  also use subtraction which is just the  hyphen we could use division which is  going to be this forward slash and we  could use multiplication which is going  to be the asterisk so i could go ahead  and multiply like 5.7 times 9 and over  here we should get 51.3 so php is  basically able to do you know any math  that we throw at it there’s also one  other operator that i want to show you  guys it’s called the modulus operator so  i could say like ten  percent sign three  and we would read this as ten  mod three and basically what this means  is um modulus operator is gonna take ten  it’s gonna divide it by three and it’s  gonna give us the remainder so 10  divided by 3 is going to be 3 with a  remainder of 1. so over here on the  browser we should get 1 which you can  see we do so this is a pretty useful  little operation and there will be  certain circumstances where you want to  find out the remainder of a division  another thing i want to show you guys is  order of operations so php is going to  allow us to specify order of operations  so for example if i said like four plus  5  times 10.  what this is going to do is it’s going  to multiply the numbers first so it’s  going to do 5 times 10 first and then  it’s going to add 4 onto it so we should  get 54  which you can see we do um but let’s say  i wanted to do the addition first i  could just put parentheses around it so  i could say like four plus five in  parentheses and now php is going to  honor the order of operations so we’ll  end up getting 90. because we’re adding  four and five which is nine and then  we’re multiplying that by 10. so this  just follows normal order of operations  rules if you’re familiar with just like  normal you know math order of operations  then it’s the same thing  right so in addition to just storing a  number or like printing out a number  right here we could also store a number  inside of a variable so over here i  could actually create a variable why  don’t we call this num and i could just  set this equal to any number so i can  set it equal to like 10. so i could  actually just print this num out and now  we’re going to be printing out 10 onto  the screen because it’s going to print  out the value that was stored inside of  this number variable  there’s also some things we can do so a  lot of times in php you’re going to want  to increment a variable that has a  number in it so i could say like num and  then i could say plus plus and plus plus  is basically just going to add 1 onto  num so now down here instead of printing  out 10 we’re actually going to print out  11. and that’s because we use this plus  plus which basically just adds one to  the number so over here instead of  getting 10 we’re getting 11. you could  do the same thing with minus minus so  this will subtract one from the number  so now because we have minus minus we’re  going to end up getting a 9 instead of a  10. so that can be pretty useful another  thing we can do is we can add a number  onto  this number so i could say like num  and let’s say i want to do something  like num is equal to num plus  25 right so basically over here i’m  saying that i want num to be equal to  num plus 25. well this is actually going  to give us 35 so it’ll do exactly what  we think it’s going to do but there’s a  shorthand for this so i could actually  just come over here and i could say num  plus equals 25 and this is going to do  exactly the same thing so now we should  get 35 all the same which you can see we  do you could also use minus equals  divide equals and multiplication equals  and basically like multiplication equals  would just be the same as saying num  times 25 right it’s basically doing the  same thing it’s just a shorthand all  right so all of that stuff can be pretty  useful and it can be pretty fun to just  kind of play around with that  so another thing i want to show you guys  is how we can use  math operations so there’s a lot of like  more complex math operations that we  might want to do in our php programs for  example instead of just doing like  addition and subtraction i might want to  like find a square root of a number or  you know use exponents or you know do  something more advanced some more  advanced math calculation and there’s  actually these things in php called  functions and functions are basically  just like little  snippets of code that we can call which  will perform a specific operation for us  and we’re going to talk more about  functions later in the course but for  now just know that a function can  basically like do something for us in  our program and there’s a lot of math  functions that we can use in php which  will perform math operations for us so  for example let’s say i wanted to find  the absolute value of a number absolute  value is just like if the number is  negative or positive it’s always just  going to give you like the value so i  could just say abs i can make an open  and close parentheses and inside of  these parentheses i could put a number  so i could put like negative 100 and  this is actually going to give me back  the absolute value of a hundred so if i  was to print this out over here you can  see we just get a hundred instead of  negative a hundred you can also do a  bunch of other math operations so for  example let’s say we wanted to um take a  number to a power like i wanted to do  like two raised to the fourth power or  something like that we could say pow and  in here i could just pass in a two and a  four  and now this is going to give us two  raised to the fourth power so over here  we get 16. we could also do like kind of  the opposite so i could instead of  taking a number to a power i could say  square root so sqrt and then in here i  could pass a number like if i passed 144  now we’re going to get 12 back and you  can see over here we do so that can be  you know that’s an easy way to get a  square root of a number another thing we  could do is compare numbers so i could  actually come over here and i could say  max and when i say max i can pass two  numbers  into this parentheses so i could say  like 2 i’ll separate it with a comma and  then i could say 10. and basically what  this function is going to do is it’s  going to tell us which of these two  numbers is bigger so what it should do  is it should just print out 10 because  that’s the bigger number and you’ll see  over here we get 10. there’s another one  i can use which is called min so i could  say instead of max we could say min and  now this will do the opposite so it’ll  tell me which number is smaller so over  here you can see now instead of 10 we’re  getting 2 because 2 was the smaller of  the numbers so in addition to doing  something like that we could also round  our numbers so let’s say i have like a  decimal number and i want to round it up  or down  i could just say round and in here i  could just pass a number so i could say  like two 3.2 and this will round it down  to three so it’s going to round  according to just like standard rounding  rules um if i had like 3.7 now this will  round it up to four  there’s also two other functions so  one’s called seal and this is called the  ceiling function and basically no matter  what decimal point is over here it’s  always gonna round it up so no matter  what it’s always just going to round  this number up so you’ll see here we get  4 even though this is a 3.3  and i can do the same thing um  in the other direction so i could say  floor and now no matter what this will  round it down so even if i have like a  3.9 over here it’s always gonna round  this number down so we’re always gonna  get three so that’s just kind of like a  couple of these different math functions  that you can use and to be honest with  you there are dozens and dozens of these  math functions that are available in php  i mean there’s all sorts of things to do  things with like logarithms and you can  do stuff with like sine cosine tangent  all that stuff um i’m not going to spend  time going through every single one if  you’d like to find more of these math  functions you can basically just go  online and search php math functions  there’s a bunch of pages with you know  full explanations on how to use all of  these guys but i really just wanted to  give you guys an introduction into kind  of like all these different operations  that you can use and sort of like how  you can go about using them so hopefully  you guys learned something you know  obviously working with numbers is  extremely important and numbers is  probably the most common type of data  that we’re going to be working with in  our programs so you want to make sure  that you have a sort of solid  understanding of how numbers work and  how you can work with them  in this tutorial i want to show you guys  how to get input from users in php so a  lot of times in our php programs you  know we’re going to be dealing with all  sorts of information and data but a lot  of times we’re going to want to be able  to get that information and that data  from a user right any good website is  going to allow the user to be  interacting with it you know filling out  forms doing all sorts of stuff  on the website so in php we can actually  get input from users  that type information into things like  text boxes or buttons or you know really  anything like that so i’m going to show  you guys how we can do that in php and  in order to do this in php we’re  actually going to need to do a couple  things the first thing we’re going to  have to do is we’re going to have to set  up something called a form  now if you’re familiar with html then  hopefully you have like a basic  understanding of what a form is but if  not i’m going to go ahead and explain it  to you guys a form is basically a  special html element that’s going to  allow the user to  input information and then it’ll be able  to pass the information that the user  enters over to our php programs so the  form is kind of like the middle man  between html and php right so the form  is where html and php meet and it’s  essentially just a way that php can get  information from a user so we can set up  a form and then inside of that form we  can put like text boxes or radio buttons  or you know submit buttons really  anything that we want and then the user  is going to be able to interact with us  and php will be able to get what the  user enters so this is going to be kind  of cool and down here i’m going to show  you guys how we can set up a basic html  form so i’m just going to come over here  and we’re just going to type out form  and we need to give this some html  attributes so the first thing that i  want to put in here is just the keyword  action  and i’m just going to say action is  equal to and open and close quotation  marks and inside here we’re basically  going to put the name of the php page  that we want to be able to handle this  form so in our case i’m over here on my  site.php file and this is just the file  that we’ve been using throughout this  course and this is a file where i want  to handle um what happens with the form  so  down here i’m just gonna put the name of  this page so it’s just site.php  so we’ll basically just put the name of  the php page that we want to work with  and then over here we’re going to make  another one which is going to be called  method and this is essentially going to  tell this form uh what we’re trying to  do with it with this form we’re trying  to get information from the user like  the whole purpose of having this form  here is that we want to be able to get  information from the user and use it in  our php code so i’m just going to put  get right here and get is basically just  going to mean that we’re trying to get  information so now i have the first part  of this form and we want to come down  here and we’re just going to make a  closing tag so i’m just going to close  this form off  now inside of this form i can actually  put some like text boxes basically i can  put html elements that will allow the  user to interact with the page so you  know something where they can type in or  like a button that they can press so  over here i’m going to make an input so  i’m just going to say input and input is  actually a special tag that can be used  with these forms and the input tag will  allow the user to input information and  since it’s like special it’ll work with  the form in order to pass that  information back to php so over here i  can just say input and i want to give  this a type and i’m just going to say  input type is equal to text and this is  just going to give us a basic text box  and then over here i want to give this  one more thing we’re going to give this  a name and you want to make sure that  this is a  one it’s going to be a name that’s going  to describe what type of content you’re  getting and also this name needs to be  unique so let’s say for the purposes of  this tutorial and for the purposes of  this program why don’t we ask the user  to enter in their name so i’ll basically  just prompt the user tell them that they  should enter in their name so i’m  actually just going to call this name  and then finally we can just end off  this input tag and then over here i’m  just going to type in a prompt so i’m  basically just going to type in name  and this will kind of like tell us what  this um text box is for so now if i was  to come over here and refresh my page  you guys will see that we have this text  box and it’s basically asking us for our  name so i could actually type into here  and um you know it’ll allow me to input  information  but we’re not done there’s one more  thing that we have to do and we have to  put a submit button so you know once the  user types in the information that they  want i want them to be able to click a  button and then that information will  basically get submitted so what i’m  going to do down here is i’m going to  create a button so i’m just going to say  input and type is going to be equal to  submit so this submit button is special  it’s basically just going to submit all  the information in the text boxes up  here so when i click this submit button  it’s basically going to submit the  information to php and we’ll be able to  you know access all the information that  got submitted in our php program so over  here like i said just type submit  and then you’ll see that we’re getting  this submit button down here so now we  have our html form set up so we actually  have everything set up on the html side  now we need to go over into php  and i’ll show you guys how we can  actually get access to all of the  information that was entered in this  form so over here i have my little php  tags and i’m actually just going to take  these and i’m going to move them down  here below the form  so i’m going to make like a break tag  and i’m going to put the php down here  and so what we can actually do now is  inside of these php tags we can access  the information that got submitted when  we click that submit button so when i go  over and i click that submit button  essentially what happens is that form  gets submitted and we can access the  information that got submitted inside of  our php program so what i can actually  do is i can just say echo and remember  the echo command is just going to sort  of echo something out into the html  document and what i want to do is i want  to echo out the name that the user input  right so remember we gave this a name it  was just called name  and down here i want to echo out that  information that got submitted so what i  can actually do is i can say echo and  make a dollar sign underscore and i’m  going to type out g-e-t  and this stands for get so this is  basically going to get the information  that got submitted and then i’m going to  make an open and closed square bracket  and inside of here i’m going to make  quotation marks and i’m going to type  out the name  of the input that i want to grab so  you’ll notice over here this input tag  for the name i gave it a name called  name right so this was its name  down here i can just type that in and  basically what this is going to do now  is it’s going to print out the value  that got submitted inside of that text  box so if i was to come over here to my  program and i could come over here and  just type in my name so i’m just going  to type in mike now when i click submit  what’s going to happen is  the name that i submitted is going to  get echoed out onto the page so let’s  click submit and you’ll see over here it  echoes out that value so it echoes out  mike  and it was actually pretty easy right  all we had to do was set up this form  and then down here we said get and we  passed in the name of the input tag so i  could make this whatever i wanted i  could change this to like username if i  wanted and then down here i could change  this to username  and it’s going to do the same thing so  that name is pretty arbitrary like it  can be whatever you want it to be it  just has to match so the name over here  has to match the name up there  so that’s basically how we can get input  from the user we set up our form we  allow the user to enter in in that text  box and then when they click that submit  button essentially what happens is this  field over here is going to get  populated with the user’s name and we’ll  be able to print it out so that’s  basically how this works and if i wanted  i could actually come over here and i  could print out like  your  name is and then it’ll print out their  name so now it’ll be like a little bit  more of a um explanation so you’ll see  down here now it says like your name is  adsf um but if i actually typed in my  name like mike  now it’ll say your name is mike so  essentially we’re taking the information  that we got from the form and we’re sort  of like interweaving it into our html  document just like that and you can  really do this for as much information  as you want so obviously over here we’re  just getting their name but if i wanted  i could get another piece of information  so i could say like  let’s ask them to enter in their age and  actually over here we’re gonna put a  break tag  so i could say like enter your age and  then again we can just make another  input and we can make this a number  input because it’s an age and i could  call this  age  and then we can sort of like close this  off  and then down here i can print out their  name so i’m actually just going to copy  this line up here and we can paste this  down here  and  i’m also going to put a break tag here  and now instead of saying your name is i  could say like  your  age is and then over here we can just  print out their age so now i can  actually enter in two pieces of  information the name  and the age so over here you know  we could say like someone’s name is john  and they’re 30  and now it’ll populate both of these  fields with  that information so you can see we’re  getting john and 30. so that’s one of  the basics of getting input from users  and you know really this is just  scratching the surface obviously you can  get more complex with the types of  information that you’re getting and the  amount of information that you’re  getting but  this basic concept is going to apply in  every aspect of php right we set up our  form  we use this action which is just going  to point to the current page  and then we’re using this get method and  then down here when we want to access  that information that gets submitted we  can just say like dollar sign underscore  get and then the name of the input form  that  was submitted  in this tutorial i’m going to show you  guys how to build a basic calculator in  php so we’re basically going to design a  little program where the user can enter  in two numbers and then our program will  add those two numbers together and print  out the results so it’s going to be a  pretty simple calculator but this should  show you guys um how we can get numbers  from the user in php so down here i have  a basic little program set up and  essentially in my html i just have a  form and the action is set to site.php  and that’s the name of the file that i’m  currently on and then over here we have  the method as get and in the last  tutorial i kind of explained what all  this was doing then finally down here we  have this submit button which is  basically going to submit the form so  that we can access whatever the user  entered in our php which i’ve placed  down here  so the first thing we need to do if we  want to build this calculator is we’re  going to need to get information from  the user so essentially we’re going to  need to get two numbers from the user  right we’re going to need to get those  two numbers and then we can add them  together  and print out the results so what we  should do is we should actually create  two inputs so we’re basically going to  create like two little  input boxes where the user can enter in  those numbers and then we can grab that  information so i’m gonna create two of  these little inputs i’m just gonna say  input and i’m gonna say type is equal to  and in html there’s actually a special  type we can use for numbers so you can  just say number and so type equal the  number is basically going to make it so  the user can only enter in a number so  they’re not going to be able to enter in  text and then over here we can just give  this a name so i’m going to say name is  equal to and we’ll just call this num1  so this is going to be the first number  that they’re going to enter then we can  just make like a break tag  and then i’m going to do this same exact  thing so i’m going to copy this  and we’re going to go ahead and paste  this down here  and again the input type is going to be  a number but i’m going to call this one  num 2. so now we have input boxes for  the first number and for the second  number  and we have our submit button so  basically what’s going to happen is when  i click that submit button this  information the information inside of  both of these uh boxes is going to get  submitted and will be able to access it  from within our php so down here in the  php what i want to actually do is i want  to get access to those pieces of  information to num1 and num2 and  essentially all i want to do is add them  together so what i could do is i could  actually just say echo and i’m basically  just going to echo out the result of  adding those two numbers together so  what we want to do is we want to get the  first number and i can just say dollar  sign underscore get in all caps and then  i’m going to make an open and close  square bracket and inside of here i’m  just going to say num1 and essentially  what this is going to do is it’s going  to get whatever the user typed into that  first number box and it’s going to put  it over here and then what i want to do  is i want to add num2 onto it so i’m  going to put a plus sign here and i’m  going to essentially do the same exact  thing i’m going to say dollar sign  underscore get open and close square  brackets and we’re going to say num2  essentially what we’re saying here is i  want to echo out into the html num1 plus  num2 and because both of these were  entered in as numbers in other words  because i said the type of input was  going to be a number php will actually  add these numbers together as if they  were actual numbers so if i put like two  and three in there we should get five so  this will go ahead and print out the  answer and then i’m just gonna come over  here and i’ll say  answer and then we’ll be printing out  the answer so let’s go over to our html  or to my browser i’ll refresh the page  and you’ll see over here we have our two  text boxes we have our submit button and  then over here it says the answer zero  and that’s basically just because we  haven’t entered in any numbers yet so if  i came over here and i said like 10 and  then down here we said i don’t know 21.  now this will give us the result of  adding those so i’m gonna click submit  and down here it says 31. so that is  essentially how we can go about adding  two numbers together now i want to show  you guys um one cool thing so if i was  to make this browser window a little bit  bigger you’ll notice up here inside of  the url we have these little line over  here it says num1 is equal to 10  ampersand num2 is equal to 21. and  essentially what this is doing is it’s  telling us what the values of those  variables were so with php this can  actually get added onto the url so if i  was to change this up here in the url  like if i change num2 to like 50 and  then i click enter  this is actually going to change the  information that gets entered in so  without having to type numbers inside of  here like without having to do anything  i was actually able to change what the  answer was down here and this is sort of  a key concept in php and i wanted to  introduce it in this tutorial anytime  that we’re entering in information with  that form  when the form gets submitted the  information that got submitted is going  to appear up here in the url and so  essentially what’s happening is when i  load up this page i can give these  different pieces of information so i say  like num1 is equal to a hundred and now  since i said num1 is equal to 100 up  here in the url it’s going to take the  value of num1 as 100. so when i click  enter now num1 becomes 100 and it adds  those two numbers together so like i  said whenever we submit that form that  information shows up up here so if i was  to get rid of this and i just hit enter  you’ll notice that this whole form gets  reset but but now if i put in like 40  and i put in 30 and click submit  it’s essentially just adding these  things on to the end of the url and this  information is basically telling us what  this answer is going to be so that’s  sort of like how that works um up there  in the url and that’s not like you know  too important later in the course we can  actually leverage those urls to do  different things but like i said i’m  going to talk more about that later but  i just wanted to kind of  mention it so you guys aren’t confused  if you see that stuff up there in the  url  so  this program works pretty well and we’re  able to you know add different numbers  together and this kind of shows you um  instead of getting text how we can get  numbers and we can actually do math on  those numbers in our php program  in this tutorial i’m going to show you  guys how to build a mad libs game in php  now if you’re not familiar with mad libs  it’s basically a game where you can  enter in like a bunch of random words  and then you’ll take all those random  words and kind of like sprinkle them in  through a story and usually because you  entered in a bunch of random stuff the  story ends up being like pretty funny i  actually have a picture over here in my  browser see this is kind of just like a  basic mad lib essentially you’re  entering in different like parts of  speech or different things like nouns or  like a person or  a place and then all those things that  you enter in will get like i said  sprinkled in throughout a story so we’re  going to build something like this just  like a mad lib essentially we’re going  to allow the user to  enter in a bunch of different words and  then we’ll take those words and put them  into our story down here i have a little  basic story set up and it’s basically  just saying roses are red violets are  blue i love you so this is kind of like  a classic poem  but i think it would be a lot better if  we mad lived it up and we allowed the  user to enter in some you know random  stuff so how about instead of saying  roses are red we let the user enter in a  custom color so this will just be like a  color instead of saying violets are blue  why don’t we let them enter in their own  plural noun so  and then finally instead of saying i  love you why don’t we let them say i  love and then some  celebrity so i love celebrity so now um  instead of saying roses are red violets  are blue i love you it’s gonna say roses  are and then the color that they enter  the plural noun that they enter are blue  and then i love the celebrity that they  enter so this should be kind of cool and  you can see just over here in my program  i’m printing it out and in order to do  this we’re actually going to have to get  information from the user so we’re going  to have to let the user  enter in some words that they want to  use in the story so up here i have this  form already set up and it’s just action  site.php this is the name of the php  file that we’re currently on and it says  method get and then down here i have a  submit button so this is a very basic  form outline and inside of this form we  want to basically prompt the user to  enter in some information so we’re going  to have them enter in a color a plural  noun and a celebrity so i’m just going  to say color  and then over here we’ll make an input  tag  and type is going to be equal to text  and we’re going to give this a name so  why don’t we just call this color  and then i’m just going to say i’m going  to make a break over here and i’m  basically just going to copy this and  we’ll  use this same thing for the plural noun  and for the celebrity so  over here we’ll make this plural noun  and we’re just going to call this  plural noun and then finally over here  we’ll do the same thing for the  celebrity  and again we’ll just call this  celebrity instead of color so  essentially i have three input boxes  three text boxes one’s asking for a  color the other is asking for a plural  noun and the other one is asking for a  celebrity and you can see i gave them  all names to match so this one’s name is  color plural noun and celebrity so we  have our form set up right in other  words we have the prompts set up for the  user to enter in that information the  last thing that we need to do now is we  need to be able to get that information  when they submit the form and put it  into our story so let’s go down here to  our story and i’m going to show you guys  how we can do that so what i want to do  is i want to create three variables and  each one of these variables is actually  going to store the color the plural noun  and the celebrity and inside of these  variables we’re going to store the  result of getting that information from  the user so i’m just going to say color  and i’m going to set this equal to  dollar sign underscore get  open and close square brackets and then  the name of that text box so it was just  color and basically what’s going to  happen is when the user submits the form  this variable is going to get populated  with whatever they entered in for the  color and we can do the same thing for  the plural noun so i’m just going to  call this plural noun and this is going  to be equal to get  and here it’s going to be  plural noun and then finally we’re going  to do the same thing for the celebrity  so we’re going to say celebrity is equal  to  get  and we want to get the  celebrity  then now that we have these variables we  can just print them out inside of our  story so down here instead of saying  color i can just say  the color variable so this will print  out the value that’s stored inside of  the color variable  same thing is going to be for the plural  noun and then down here we’ll just say  plural noun and then finally we’ll do  the same for the  celebrity so essentially i stored all of  the things that the user input inside of  these variables and then down here i’m  actually going to print them out inside  of the story all right so let’s go ahead  and test this out i’m going to go over  to my webpage so you can see over here  we have all of this information set up  now you’ll see down here this story is  basically just printing out roses are  blank  blank or blue i love blank and then over  here we can actually start submitting  some information so over here in color  i’m just going to type in  magenta plural noun why don’t we do like  microwaves and then celebrity let’s do  tom hanks so now when i click submit all  of this information should get submitted  and it’s going to get stored inside of  each one of these variables then those  variables are going to get printed out  inside of our story so let’s do that i’m  going to click submit and you’ll see  down here that our story updates so it  says roses are magenta microwaves are  blue i love tom hanks so we were  actually able to make this mad libs now  now obviously the one problem with this  mad libs is that um you know if i was to  like reset this form  this is like showing up here before we  actually submitted the form so ideally  we would want this text to show up after  we submit the form like after the users  entered in all the stuff and not before  um and actually later in the course  we’re gonna learn a technique that we  can use to do something like that but  for now this kind of works and you can  see how you know we could essentially  enter in whatever color whatever plural  noun or whatever celebrity we wanted and  it would show up inside of our story  just like that so hopefully that makes  sense and what you should do is just  build your own mad libs game and you  know you can model your own little mad  lib story and sort of play around with  it  in this tutorial i want to talk to you  guys about url parameters in php a url  parameter is basically just a  value that we can tack on to the end of  one of our urls which will essentially  pass a value into our php program and  then we can access it so i want to show  you guys basically how this works and  what it’s doing so over here i have a  very simple program set up i have this  form over here the action is site.php  that’s the page that i’m currently  working on and then the method is get  and whenever we’re using these url  parameters you always want to make sure  that this says get right there and  actually in the next video i’m going to  talk to you guys some more about what  get actually is doing  and there’s actually another method we  can use called post so if you’re  interested in like exactly what’s going  on here check out the next tutorial but  for now i just want to talk to you guys  about these url parameters  so make sure that get is put over here  and then i basically am just asking the  user to enter in their name so i’m using  this text box over here telling them to  enter in their name and then we have a  submit button then down here in my php  i’m basically just printing out the name  onto the browser so if i come over here  i can use this program i could just like  type in my name mike and when i click  submit my name prints out down there on  the page very simple but one thing i  want to show you guys is when i expand  my browser window over here you’ll  notice that up here it says site.php  question mark name is equal to mike so  essentially what happened was when i  submitted that form the value for name  actually got placed inside of our url  and this is what we would call a url  parameter or a url variable or a url  value and basically what this means is  this is just the piece of information  that we’re giving to php so i could come  over here and i could change this to  like dave and now you’ll see that the  value updates down here so without  having to type in anything like if i  typed in a name up here steve and i  click submit you’ll notice that it  updates down here and it also updates up  here but if i wanted i could just bypass  this text box altogether and i could  pass a value in here like john  and now that’s going to be the value  that this page is getting so up until  this point in the course we’ve always  been getting our information through  these text boxes and that’s a very  common way to do it a lot of times  you’re going to want your user to  interact with the website using things  like text boxes or buttons but other  times in our php programs you might want  to give information to your php page  without having to like make the user do  it so in other words in certain  circumstances like there might just be  certain values that i want to give in a  specific url and i don’t necessarily  want the user to have to enter them and  we can use these url parameters in order  to do that so if i want to add another  url parameter over here you’ll notice  that we say essentially the name of the  site then we use this question mark and  that sort of delineates these two things  and then over here i’m basically saying  the name of the parameter or the name of  the variable  and then the value that we’re giving to  it if i want to add another one i can  just say ampersand and now i can do the  same thing so let’s say we wanted to  pass in like an age i could say like age  is equal to  70. so now in addition to giving this  page this name value i’m also giving it  this age value over here with a value of  70. so if i was to come over here and i  just like entered this in you’ll notice  that like nothing’s changing right even  though i added that new you know  parameter up here in the url it doesn’t  really change anything on the page but  inside of our php it’s going to change a  lot so i could actually access this  value that got passed in in the url so i  could come down here and  instead of echoing  the name i could actually echo the age  now so because we passed in this  variable age i’m able to print out what  it was so instead of printing out the  name now when i enter this  it’s going to print out the age so you  can see we’re printing out 70. and if i  was to get rid of this up here in the  url  now it’s just not going to print out  anything because it didn’t receive that  value and this is a really awesome way  for us to build these urls one of the  reasons that this is so useful is  because you could have a  web page that’s you know has a bunch of  values associated with it and then you  could store all of those values in the  url so a user could actually like  bookmark that page and they could go  back to that page with all of that same  information set  for the page and you know this doesn’t  have to be like someone’s name or  someone’s age i mean this could be any  information that you want to store on a  particular web page and like i said  because all of it’s stored in the url uh  users can like bookmark that page and  they can have all of that information  stored so a lot of websites will do  something like this for example if i  came over here and i like did a google  search so if i just searched for like  dogs for example when i hit enter you’ll  notice that google has something similar  so you know i don’t know exactly what  technology google’s using but you’ll see  over here google has something similar  to what we did over there so they have  like this ampersand they have this  little value here aqs is equal to chrome  dot dot whatever  so google is doing similar things inside  the google url there’s also information  stored just like we had on our url so  this can be kind of useful  and obviously like you know google’s  using this for some complex use case  obviously we’re just passing a name  but the concept is the same we can store  information  inside of these urls now here’s one of  the problems with something like this is  that it’s not very secure so all of the  information that we pass into this  website is basically visible so if i was  to type in like my name over here and i  click submit  the name that got passed in is a you  know it’s basically visible and  available up here in the url and a lot  of circumstances you’re going to want it  to be visible like i said for  bookmarking or something else but in  other circumstances you’re not going to  want the user to be able to you know  see this information or even be able to  modify it like i could just modify this  piece of information and it’s going to  change you know what happens on my  website for a situation like that we can  actually use another um  form method so you’ll notice up here  inside of my form i have this little  method attribute says get  there’s another method called post which  we can use which will basically do the  same thing but in a more secure fashion  and in the next tutorial i’m going to  talk to you guys about what that post  method can do and we’ll just talk about  the differences between get and post but  for now that’s kind of been an  explanation of those url parameters  and those are extremely useful like i  said for any information that you just  kind of want to be publicly available  in this tutorial i’m going to talk to  you guys about two different ways that  we can get information from users um and  basically these are two form methods  called get  and post and i’m just going to talk  about the difference between the two of  them and we’ll kind of look at what  they’re doing and when you should use  get and when you should use post so  let’s go ahead and get started  down here i have a basic program set up  basically i have this form  and all it’s doing is asking the user  for a password  so here i have this input and it’s a  type password  and the name is called password right  and whenever we put type password over  here this is actually going to give us a  text box which like is a password so  you’ll see over here in this text box  like when i type in it sort of like you  know blocks it out and that’s basically  just like you know you can see that a  lot on different websites just so nobody  can see what you’re typing in when  you’re typing in your password and then  obviously we have the submit button and  then down here in the php i’m just  echoing out the password so that way we  can just like see what i enter in so if  i came over here i could like enter in a  password  um and then when i click submit the  password will show up down here so i  just my password was wordpass and i want  to talk to you guys now about the  different ways that we could get  something like a password from the user  so over here and up to this point in  this course whenever we’ve been building  these forms we’ve always set this method  equal to get and basically when we set  this method equal to get what’s going to  happen is the information that the user  enters is obviously it’s going to get  you know submitted into php and we’re  going to be able to use it but that  information is also going to get put up  in the url as a url parameter and a url  parameter is basically just you know a  way that we can  pass information into php so when i use  this get method you’ll see over here  when i entered in my password the  password actually showed up  up here inside of the url so if i was to  enter in another password like i’ll  enter another password click submit i  entered in banana and now because i use  that get method over here the password  is actually showing up inside of the url  now i’m sure you can imagine like this  is not a good scenario for a password  certain information that we’re passing  into our  php pages is going to be fine to show up  there in the url right it’s not going to  be a problem like it doesn’t really  matter with certain pieces of  information but with a password if your  password is actually showing up inside  the url that is extremely insecure and  also i could come up here and i could  change it so i could change the password  to like  orange and now the password is like  updated throughout the entire page right  so a piece of information like a  password is not something that should be  stored up there inside the url and it’s  not something that the user should just  be able to change willy-nilly whenever  they want in the url in a situation like  that where we have information that we  want to you know pass between the client  and the server more securely we want to  use the post method so down here instead  of saying get i’m actually just going to  say post and just with that one change  this is actually going to update so down  here if we’re using post if i want to  actually be able to print this out  instead of saying get i’m just going to  say post all in caps so basically this  over here is going to match this down  here and post is basically just going to  do exactly what get did except it’s  going to do it without placing it upside  up in the url parameter and there’s also  like a couple other small differences  like when you use post  you can actually get potentially more  information from the user than when you  use get but the main difference is that  when we use post it’s going to be more  secure so that information isn’t going  to show up  inside of the url so over here if i was  just to like refresh my page  you’ll see when i refresh my page even  though i have password equals to orange  up here in the url it’s not showing up  down here anymore right so that  information like when i put the  information up here it wasn’t able to  show up but if i was to enter in a  password into this text field because  it’s using this post method so if i  entered in  my password and i click submit now we’re  going to be able to grab that  information securely and you’ll notice  up here in the url there’s no  information right that information did  not show up in the url it got passed  between the client and the server in a  more secure  fashion that’s basically the difference  between get and post get is just kind of  like anything goes anyone can see the  information it’s up there in the url and  in a lot of cases that’s going to be  useful but in a lot of other cases like  in the case of a password or even like a  username or you know a credit card  number i mean any type of like secure  information um you don’t want it to show  up inside that url you want it to be  past  more securely back to php and we can use  post in order to do that so like i said  in certain circumstances get’s going to  be appropriate in certain circumstances  post is going to be appropriate but now  that you kind of know the difference you  can kind of make that decision for  yourself and this is just a quick note  about php in general most of the time  developers are going to prefer to use  post as opposed to get whenever they’re  you know getting information from a form  just like this so a lot of times you’ll  see people using post more so than using  get  get is going to be used more with like  url parameters but here’s the thing it’s  really up to you it’s up to you the  developer to make the decision as far as  like what you want to be able to happen  when that form gets submitted but like i  said i think for the most part people  prefer to use post over get when they’re  getting information from a form  in this tutorial i’m going to talk to  you guys about arrays in php now an  array is basically a container or it’s a  structure where we can store multiple  pieces of information so a lot of times  in php we’re going to be dealing with  all types of data and one way that we  can manage and maintain and keep track  of that data is by using something  called a variable and throughout this  course we’ve been using variables i’ve  kind of you know showed you guys how  they work and a variable is great  because it can store one single value so  it’s a container where i can store a  single data value but a lot of times in  php we’re not just going to want to be  able to store one value we’re going to  want to be able to store large groups of  values so a lot of times an array is all  that we need right a lot of times we  only need to be able to store one value  but if you have like a large list of  information or you know you need a  container where you can store large  amounts of information that’s where an  array comes in an array is actually very  similar to a variable but unlike a  variable an array can store more than  one piece of information inside of it so  an array could have 10 or 20 or 100 or a  thousand or even a million values a  million pieces of information  inside of it so that’s why arrays are  really useful and there’s a lot of  situations where you’re going to want to  be able to store and keep track of large  amounts of information and you can use  arrays to do that so i’m going to show  you guys how we can create an array how  we can use it and we’ll sort of look at  the basics so down here in my php tags i  want to create an array we actually  create an array very similar to the way  that we create a normal variable i’m  just going to make a dollar sign and  we’re going to give this a name so we  want to give it a descriptive name and  the name will basically tell us like  what type of information is stored  inside of this array so i’m going to  make a an array that’s going to store a  bunch of like names so we’ll just call  it friends so maybe this could store  like a list of a bunch of my friends so  i can say dollar sign friends is equal  to and now i want to type in  array and i’m going to make an open and  close parentheses and then as always  we’ll end it off with a semicolon now  inside of these open and close  parentheses i can actually store  multiple pieces of information inside of  this array so in our case we’re going to  store like i said a list of friends  names so i could put like kevin  karen  oscar and then we do one more gym  so inside of this array i’ve basically  typed out all of these different values  and you’ll notice that i separated each  one of these values with a comma so over  here i basically am just listing out all  these different names and these are all  now elements inside of the friends array  so i would say that this over here is an  element inside the friends array this is  an element in the friends array this is  an element these are all elements  inside of this one structure inside of  this one container so unlike a variable  where i can only store one string  in this array i can store multiple  strings side by side just like that and  that’s why these arrays are useful so in  addition to just storing a string inside  of here i mean i could store any type of  data that i want so maybe in here i also  wanted to throw in like a number  i could throw in like a boolean value  really you can put any type of  information that you want inside of  these arrays it’s not going to matter so  now that we’ve actually created this  array and we’re storing all of this  information the question becomes how do  we access that information right the  information is no good if we can’t  access it right so this container is  actually storing all these pieces of  information and i’m going to show you  guys how we can access them so over here  i’m actually just going to echo out  something so i’m just going to say echo  and i’m going to show you guys how we  can access individual elements inside of  this array now one thing i could do is i  could just echo out friends just like  this and you’ll see over here on my  browser when i refresh the page it’s  just printing out array so it’s  basically just telling us like hey this  is an array there’s a bunch of stuff in  here but if i wanted to access an  individual element for example let’s say  i wanted to access the first element  inside of the array this kevin value  after i type out the name of the  variable i can make an open and closed  square bracket and inside of this open  and close square bracket i want to put  in the index  of the element that i want to access so  all of the elements inside of this array  are assigned index positions and so all  i have to do in order to access the  specific element is put its index inside  of these square brackets so if i want to  access this first element here i’m going  to put a 0 inside of here because the  first element in the array is at index  position zero so now when i run my  program over here you’ll see that we’re  printing out kevin so we’re printing out  that first name inside of the array if i  wanted to print out the second element i  could just put a one here and this will  print out the second one so we should  print out karen and if i wanted to print  out oscar over here i could put a two in  here and this will print out oscar so if  you haven’t caught on when we’re giving  index positions to these array elements  we start at zero so we would say that  the first element in the array this  kevin is actually at index position zero  and then over here we would say that the  second element in the array karen is at  index position one and so forth so we’re  going to start zero one this is going to  be two this is going to be  three and this is very important so this  is how we’re going to index these arrays  and if you’re familiar with strings in  php this is actually the same way that  we index strings so we start with zero  so that’s why i have to use these  numbers down here and that’s really how  we can access and work with a specific  element in the array another thing i  could do is i could modify one of the  elements in the array so for example  over here let’s say we wanted to modify  this element right here where it says  karen all i have to do is just say  friends and i can make an open and close  square bracket and i want to put the  index of the element that i want to  modify so karen is at nx position 0 1 so  i’m going to put a 1 over here and i  could just give this a new value so i  could call this  dwight for example  and now when i go down here and i print  out friends one the value will have  updated so now this should give us  dwight instead of giving us karen and  it’s also important to note that we can  store different data types in these  arrays alongside each other so i could  put like a 400 here and this is a number  it’s not a string and you’ll see that  this is still going to work so i’m able  to print out 400.  all right so this is really the basics  of using arrays in php it’s essentially  just a structure where we can store  multiple pieces of information now  another thing that i can do is i could  also add information onto this array so  you’ll notice over here we have four  elements in this array and it’s at index  position zero one two three so there’s  no array element at index position four  but i could actually add an array  element in if i wanted to so i could  come over here and i could say like  friends  four and i could add in another friend  so now i could name this friend like  angela and down here i could print out  friends four and this is going to give  us angela so essentially what i did was  i added an extra friend onto the end of  that list and you can do stuff like that  i mean i could also make this like a 10  and it would be the same thing so i  could add an element at index position  10 in this array and it’s going to be no  problem it’s going to be able to handle  that just fine now i want to show you  guys one more thing we can do which is  actually pretty useful with these arrays  is you can figure out how many elements  are inside of the array so i’m actually  just going to get rid of this and i’m  going to come down here and i’m just  going to print out count like this and  then i’m going to surround the array  with open and close parentheses so i’m  saying count and then inside of these  parentheses friends and this is going to  tell me how many elements are inside of  this array so you’ll see over here we’re  getting four and if i wanted like i said  i could add another element so i could  say like  friends four is equal to  mike  and now we should get five here instead  of four because i added another element  and that’s kind of the basics of working  with arrays arrays are extremely useful  and there’s gonna be tons of situations  where you wanna use them so so you wanna  make sure that you have a pretty solid  understanding of these going forward  in this tutorial i’m going to show you  guys how we can get input from check  boxes in php this is going to be a  pretty cool tutorial because not only  are we going to learn how to get input  from text boxes but we’re also going to  see how we can use arrays out in the  real world so this is going to be an  example where we actually get  information from the user we’ll store  that information inside of an array and  then we can kind of work with it so this  is going to be pretty cool now over here  in my program i have just sort of a  basic program set up already  down here i have a form and you can see  the action is site.php that’s this file  that i’m currently working on and then  over here the method is post and then  down here we have our submit button so  this is a pretty standard form and then  down here in our php i haven’t actually  written anything yet so what i want to  do is i want to show you guys how we can  work with check boxes and a check box is  basically just you know like a little  box where you can check and what we’ll  actually do is we’ll have like a list of  check boxes so we’ll have like four or  five and what we’re going to do is we’re  going to allow the user to select their  favorite fruits so i’m gonna have like a  little list of a bunch of fruits and the  user will be able to check which fruits  they like and they’ll be able to submit  that information to us so it’s gonna be  pretty cool and it’ll just kind of  demonstrate how we can use checkboxes  and how we can also use arrays so over  here  right on top of this submit i’m going to  make another input i’m just going to say  input the type is going to be equal to  checkbox so this is basically going to  tell html that we want to create a  checkbox and then over here i want to  give this a name so i’m going to say  name is equal to i’m just going to call  this  fruits and i’m going to make an open and  closed square bracket now whenever we’re  trying to get input from like multiple  check boxes we always want to put these  square brackets here and basically  that’s going to signify that we’re going  to store all of these fruits inside of  an array and once they’re in the array  it’ll be a lot easier for us to work  with them and do different things with  them and finally i want to put one more  attribute down here which is going to be  value and value is essentially going to  be the value that this checkbox is going  to have associated to it so we’re going  to have this check box be a checkbox for  apples so that’s going to be our first  fruit so if the user checks this  checkbox that means that they like  apples because that’s the value over  here and then i’m just going to put a  break over here and actually i’m going  to do one more thing so i’m going to  come over here to the left of this input  and i’m just going to type out what it  is for so i’ll just say apples  and  now what i’m going to do is i’m  basically going to do this for a bunch  of different fruits so i’m going to go  ahead and copy this line of code  and i’m basically just going to paste it  down here a few times so why don’t we do  like three different fruits so we’ll do  apples let’s do oranges so i’m just  going to change this to oranges over  here and then over here on the value i’m  also going to change this to oranges and  then down here why don’t we do pairs so  this one’s going to be pairs and then  once again over here i’m going to change  this to  pairs so we have a check box for three  different fruits and you know making the  check box is pretty simple again we just  have to specify the type we have to give  it a name and remember if we want all of  these check boxes to sort of be stored  inside of the same array in other words  if we want the values that the user  checks to be stored in the same  container in the same array we have to  name it just like this and then finally  we give each of these a value so if i  was to refresh my page you’ll see over  here that we get  all these different checkboxes and  what’s cool about checkbox is i can  check multiple boxes so you know i can  basically just check and uncheck as many  as i want here’s the question though  what i want to do is i want to be able  to  get the values that the user checks so  if the user checks apples and oranges  when they click submit i want to be able  to get that information right if they  select all three i want to be able to  get that information and because the  user is able to select multiple pieces  of information here we’re storing it  inside of an array and remember an array  is just a container that can hold  multiple pieces of information and  that’s basically what i was saying down  here in this name i’m saying i want to  store all of these values in other words  i want to store the values that the user  checks inside of this fruits array  so inside of our php now we can actually  get that information so what i can do is  i can come down here and i’m going to  create a variable called fruits and i’m  going to set this equal to dollar sign  underscore  post in all capitals and remember  i use post up here so i want to use post  down here and i’m going to make an open  and close square bracket and inside of  here we’re just going to type out  fruits so i’m basically typing out the  name  that i specified up here although we’re  going to leave off the square brackets  we don’t need that so down here i now am  storing all the fruits that the user  checked and submitted  inside of this variable and actually  this is an array so this is an array  that’s holding all the fruits that the  user checked from the checkbox so i  could come down here and why don’t we  just like echo this out just so we can  kind of see what’s going on so i’ll echo  out  fruits zero so this is basically going  to tell us what the first fruit that was  checked is so over here back on our web  browser i’m just going to refresh the  page and now i’m going to go ahead and  do this so i’m going to check apples and  oranges so i’m checking two of these  different boxes when i click submit we  should be printing out apples because  that was the first check box that i  checked so now when i click submit you  see we’re getting apples down here if i  was to come over here and print out  fruits one so this is going to be the  second fruit in the list now if i do the  same thing so if i do apples and oranges  we should be printing out oranges  because that was the second element that  was stored inside of that array if i  only checked one of these though since  i’m trying to print out the second  element in the array it should be blank  so now you can see it’s blank and if i  did like oranges and pairs now we’ll be  printing out pairs so that’s basically  how these check boxes work right i can  set up all these different check boxes  and because i gave them all the same  name over here they’re all going to be  stored inside of the same array so when  i click that submit button all that  information is getting passed back to  php on the server and it’s basically  storing that information inside of an  array and then i can work with that  information and do different things with  it so this is a pretty useful thing to  do on your websites and as you can see  it’s very simple right and we can use  that array structure in order to store  all of that information  in this tutorial i’m going to show you  guys how to use associative arrays in  php an associative array is a special  type of array where not only we can  store data values but we can actually  store what are called key  value pairs  so unlike a normal array where i could  just store like numbers or text or a  combination of both in an associative  array i could store a series of key  value pairs which would allow me to  access that information differently so  i’m going to go ahead and show you guys  how these work the easiest way to wrap  your mind around it is just to see an  example so let’s create an example and  then we’re actually going to create a  little  web application where  we can use these associative arrays so  down here i’m going to show you guys how  we can use these let’s say that i was  writing a website for a school  and for this website i wanted to be able  to keep track of the different students  in my class and the grades that they got  on a particular test well this is  actually a scenario where we could use  something called an associative array so  in other words inside of this array i’m  going to be storing two pieces of  information i’m going to be storing the  student’s name and i’m going to be  storing the grade that they got on the  test and those two data values are sort  of like linked together and in an  associative array we could actually  represent data like that so i’m going to  create this just like i’d create a  normal array i’m going to make a dollar  sign and why don’t we just call this  like  grades and i’m going to set this equal  to  array with an open and close parenthesis  so so far this is exactly like a normal  array the difference is though now when  i put elements inside of this array  instead of just storing single pieces of  information i’m going to store key  value pairs so the first thing i want to  do is store the student’s name so let’s  say the student’s name is jim and then i  want to say equal sign greater than sign  and i’m going to store the grade that  jim got on the test so let’s say that  jim got an a plus on the test let’s say  jim’s really smart  so unlike a normal element in an array  i’m storing a key which is the student’s  name and then i’m storing a value in  other words i’m mapping a value to a  specific student’s name  over here i can do the same thing for  another student so let’s say we have  another student pam  and let’s say that pam got a b minus on  the test  all right so now i’m storing the student  and i’m also storing the grade that they  got on the test why don’t we do one more  let’s make another student  and you’ll notice i’m separating these  different students with this comma just  like i would normal array elements so  over here we’re just going to say  oscar and let’s say that oscar got a  c plus on the test right so we have our  three students jim who got an a plus pam  who got a b minus an oscar who got a c  plus so you’ll notice i’m storing a key  and then i’m mapping it to a particular  value i’m storing the student’s name and  i’m mapping that name to a particular  grade what’s cool about these  associative arrays is when i want to  access one of these elements i could  just come down here and i could say  grades and i’m going to make an open and  close square bracket and in here i can  just type in the name of the student so  i could say grades gym  and this is actually going to tell me  what grade jim got on the test so when i  refresh the page over here and actually  whoops i need to echo this out so i’m  going to say echo  this is going to tell me what grade jim  got so we can see over here jim got an a  plus i could do the same thing for oscar  so  i’m just gonna see what oscar got looks  like oscar got a c plus  so unlike a normal array where we access  elements using their index position in  an associative array we access elements  using what’s called a key and the key is  basically this value over here so we  would say that jim is a key pam is a key  and oscar is a key and then over here we  have the values so we have a key and  it’s mapped to a particular value and i  want to when i want to access that value  inside the associative array i just pass  in the key  and one thing you do want to keep in  mind is that you want all of the keys  inside of your associative array to be  unique so if i came over here and i made  this student also named jim well then  when i tried to access gym  like it’s unclear which one we’re  referring to so you always want to make  sure that these are unique right so i  have different names for all these keys  the values can be the same however so i  could  come over here and have pam also get an  a plus just like jim and that’s going to  be no problem but you always want to  make sure that those keys are unique and  just like with a normal array i could  also come over here and modify this so i  could say like grades  gym  and i could give this a value so i can  say grades gym is equal to f so let’s  say now jim fails the test  and over here when we print this out  jim’s is going to have a new value of f  so you can essentially do everything you  do with a normal array you could also  get how many elements are inside of it  so i could say count  and over here this will tell us how many  key mappings we have so we have three  and like i said it’s just like a you  know any normal array that we would have  in our program i want to show you guys  how we could use this to build um a  little website so what i’m going to do  is i actually have a form set up over  here and i’m just going to uncomment  this  and you’ll see it’s a simple form it’s  just action is site.php that’s this file  we’re using the post method and then  down here we have an input button what i  want to do is i want to write a website  where the user can enter in a name and  then we will basically print out what  grade that user got on the test so i can  come over here and i’m just going to  make another  input and i’m just going to say type is  equal to  text this is just going to be a text box  and i’m going to give this a name so why  don’t we call this student  and essentially inside of this text box  we’re going to be printing we’re going  to be typing in the student’s name whose  grade we want to figure out so what i  can do is when the user submits when  they click this submit button i could  actually come down here and access that  information so what i could do is i  could say echo and i’m going to echo out  grades and then i want to get the value  that they passed in so i’m going to make  an open and close square bracket and  inside of here i’m going to put  dollar sign underscore post  open and close square bracket and in  here we’re just going to put student so  essentially what i’m doing here is i’m  grabbing the value that the user entered  inside of that text box  and i’m accessing that element inside of  the associative array so let’s go over  here and refresh our page  and you’ll see we have our text box so  if i typed in like jim for example when  i click submit this should tell me what  grade jim got on the test so you’ll see  we get back a plus i could do the same  for pam  and looks like pam also got an a plus  and then we could do it for oscar and  oscar got that c plus so this is  basically a way that we could wire up  like getting user input with an  associative array and this is actually  really useful so you can see how storing  the information like this in an  associative array where we have key  value pairs makes it really really easy  for us to access that information in the  future  in this tutorial i’m going to show you  guys how to use functions in php a  function is basically just a special  container where we can put a bunch of  code that’s designed to perform a  specific task so a lot of times when  you’re writing your php code you’re  going to have certain code which is  going to be naturally grouped together  so you’re going to have certain code  which is naturally just like performing  you know some common task and a lot of  times in php what we can do is we can  take code like that and we can put it  inside of its own special container  called a function and the cool thing  about functions is it allows you to  organize all the code on your website  but a function is also going to be able  to be reusable so i can basically take  some code that performs a specific task  put it inside of a function and then i  can use it in multiple places throughout  my program and that is extremely  powerful so in this tutorial i’m just  going to give you guys a basic  introduction into functions we’re going  to create a function and we’re going to  kind of talk about how they work so down  here in my php tags i’m going to go  ahead and create a function and like i  said a function is basically just a  container where we can put a bunch of  code that’s designed to perform a  specific task so in this tutorial i’m  actually going to create a function  which is going to say hi to the user so  the whole purpose of this function is  going to be to say hi to the user and  i’ll show you guys how we can use this  and how we can leverage its power so  when we create a function the first  thing we want to do is just type out the  word function  and this is going to tell php that we  want to create a function the next thing  we want to do is give this function a  name so you basically want to give it a  name which is going to describe what  it’s doing so in our case we’re building  a function which is going to say hi to  the user so i’m just going to call this  say hi just like that and then i’m going  to make an open and close parentheses  and i’m going to make an open and close  curly bracket and what i want to do is i  want to go inside of this open and close  curly brackets you’ll see in here i’m  typing inside of these curly brackets  and any code that you put in between  those curly brackets is going to be  considered part of the function so  whatever code i want to put in there  that’s technically technically going to  be part of this function so i’m going to  make a very simple function i’m just  going to type out one line of code and  it’s just going to say  hello user  now i have one line of code here in my  function but you can have as many lines  of code as you want functions can hold  you know dozens or hundreds of lines of  code doesn’t really matter and so now  that i created this function i’m going  to go ahead and run my little program  here so i’m going to go ahead and  refresh my browser but you’ll see when i  refresh my browser nothing happens so  nothing is actually getting printed out  and here’s the problem anytime we put  code inside of a function like this  that code is only going to execute when  we do something called calling the  function so for this code to execute i  have to call  this function so i’m going to come down  here below the function and i’m going to  call it and the way that we can call a  function is just by typing out its name  so i can just type out say hi and then  i’m going to type in open and close  parentheses and basically what i’m doing  here is i’m telling php that i want to  execute all of the code inside of this  function so when php is looking through  this file and it comes down here and it  sees say hi  it’s going to know that it has to jump  up over here and execute this code so  now when i refresh my browser you’ll see  that we’re printing out hello user so  the code  inside of that function is actually  getting executed and this is obviously a  very simple function and like i said the  whole point is that whenever you have  code that’s you know performing a  specific task you can put it inside of a  function just like that but we can take  this a step further so another cool  thing we can do with these functions is  we can actually give them information so  i can give this function information  which are called parameters and then the  function can use those parameters or the  information that gets passed in in order  to do different things so what i could  do is i could come over here and let’s  say that instead of just saying hi to  the user we wanted this to say hi to  someone specific right so what i can do  is i can essentially create a variable  up here in these parentheses i’m just  going to say dollar sign and i’m just  going to call it name  and now i can come down here and instead  of printing out user i’m just going to  print out name and so essentially what i  did up here is i specified that this  function this say hi function  is going to take in a parameter so it’s  going to take one value in  that means whenever i call this function  for example if i call it down here i  have to pass it a name i have to pass it  a value so i could go ahead and i could  pass it like a name like my name so i  can pass it the value mic and now this  value is going to get stored inside of  this name variable and it’s going to  print out hello mic down here so now  when i run my program you’ll see it’s  printing out hello mic instead of  printing out hello user and i could  change that depending on what i put in  here so if i put like tom in here now  it’s going to print out hello tom so  this function is using the piece of  information that i gave it in order to  perform its task a little bit  differently and that’s sort of like  what functions are  another cool thing we can do with  functions is we can actually reuse this  code so i can write this code up here  one time and i can execute it as many  times as i want inside of my program so  for example i could come down here and i  could just copy this and i could paste  it a few times  and i can say hi to tom i can say hi to  dave we could also say hi to  oscar  and so i’m basically saying that i want  to call this function three times i’m  passing it three different pieces of  information and actually over here i’m  just going to put a break tag so we can  kind of see this a little bit easier so  now it’s going to print out hello tom  hello dave and hello oscar so when i  refresh this you’ll see it’s printing  out all of that so i wrote this code one  time i wrote the code to say hi to the  user one single time and i was able to  use it three times throughout my program  and that’s kind of one of the core  concepts with functions is you can write  them once and you can use them a bunch  of different times so we can actually  reuse this code throughout our program  and in addition to just passing in one  parameter i can pass in as many as i  want so i can put a comma over here and  let’s say i want the user to pass in an  age as well  and now i could say hello name  you are  age right  so down here i can pass in two  parameters now so i could say tom is 40  let’s say  dave is 13 and oscar is 80.  and now our program is going to be able  to respond to that so you’ll see it’s  saying hello tom you’re 40 dave’s 13  oscar’s 80. so we can pass in two or  three i mean you can pass in as many  parameters basically as you want  and then whenever you call the function  you need to pass them in  just like that so that’s why functions  are useful functions are actually  extremely useful and there’s a lot of  situations in php where we’re going to  want to use them  in this tutorial i’m going to talk to  you guys about return statements in php  functions so one of the cool things  about functions in php is that they  allow us to group similar code together  that performs specific tasks so a  function is essentially just a container  where we can put different lines of code  and then we can reuse that code  throughout our programs and if you’ve  been following along with the course we  kind of looked at the basics of  functions in the last tutorial and one  of the things we talked about was how we  can give functions information in the  form of parameters so i could actually  like give a function a couple of  different pieces of information and then  the function can use that information to  perform its task  in this tutorial i want to talk to you  guys about another cool thing with  functions which is in addition to us  giving functions information in the form  of parameters a function can also give  us information back so if i call a  function in my program not only can i  give that function information but that  function can also give information back  to me so it could tell me like you know  how the function went it could give me a  particular piece of information back it  could pass me back like an array or a  variable it could do all sorts of stuff  so i’m going to show you guys how we can  use  returns in php functions so what i want  to do in this tutorial is i’m actually  going to create a function which is  going to cube a number  and whenever we cube a number you’re  basically taking it to the power of 3.  so if i was to say like 2 raised to the  power of 3 that’s the same as 2 times 2  times 2 right this is me q being a  number and what we want to do is we’re  going to write a function which will  cube a number so i could pass a number  into it and it’ll go ahead and cube that  number for me it’s going to be a pretty  simple function but this will kind of  illustrate the point that i’m trying to  get across in this tutorial so  whenever we create a function we always  just want to say function like that  and then we’re going to give this a name  so i’m actually going to go ahead and  name this function cube because that’s  what it’s doing it’s going to cube a  number i’m going to make an open close  parenthesis and an open and close curly  bracket  inside of these open and close  parentheses what i want to do is i want  to specify that this function is going  to take one parameter so we’re going to  allow the user to pass one value into  this function and i’m just going to call  it num so this is going to be the number  that we want to cube and the goal of  this function is going to be to take  this number cube it and then return that  value back to the user so not only are  we going to cube the number but we’re  also going to return the result of  cubing that number back to the user so  this should be pretty cool all right so  now that we have our function set up i’m  going to go ahead and write the code so  this is actually going to be a pretty  simple function all we really have to do  is just cube num so i can basically just  say  num times  num  times num right i mean essentially by  doing this i’m cubing the number but  here’s the problem is i don’t just want  to cube the number i want to return the  result of cubing the number back to the  caller so whoever calls this function i  want to give this value back to them and  in order to do that we can use a special  word in php which is called return so i  can come over here and just type return  and basically what this is going to do  is it’s going to return the value that’s  over here  back to the caller in other words back  to whoever called this function  so if i was to come down here i could  actually call this function and it’s  going to give me a value back so i’m  going to go ahead and demonstrate that  i’m actually going to create a variable  and i’m just going to call it  cube  result and i’m going to set it equal to  cube  so i’m calling this cube function and  i’m going to pass it a number so why  don’t we pass it in a four so we’re  going to cube four and i’m storing the  value that gets returned from this cube  function inside of this variable so  essentially what happens is when i call  this cube function it goes off it  executes all this code and when it sees  this return keyword it’s basically going  to pass this value back down here so  when i say cube result is equal to cube  4  the value of cubing 4 is going to get  stored inside of this variable so let me  show you guys that i’m just going to  echo this out so echo out cube result  and now when i run my program you guys  will see i should get 64 back because 64  is 4 cubed right 4 times 4 is 16 16  times 4 is 64. so essentially what  happened was by using this return  keyword i was actually able to get this  value if i was if i got rid of this  return keyword so if i just um you know  put this line over here  you’ll see that we don’t get anything  and that’s because nothing was returned  from this function but when i put the  return keyword here  then everything works and we’re golden  so actually what i could do is i could  just cut off the middle man and i could  just print this out so i’m just going to  go ahead and put this in here  and now we’re just printing out cube 4  so it’s going to do the same thing  because this is getting a value back  when we call that  function so essentially what happens is  whenever we put this return keyword in  there it’s always going to be the last  line of the function so whenever php is  going through and executing this  function whenever it sees this return  keyword it’s going to break out of the  function so let me show you guys if i  was to come down here and say like echo  hello  you’d think when i call this cube  function then it’ll go over here it’ll  return this value and then it’ll print  out hello right but when i run this  program you’ll see that we’re not  printing out hello this line of code  never gets executed php never sees it  never touches it that’s because whenever  i put this return keyword in here this  is going to break us out of the function  and we’re going to go back down here so  whenever i say return that’s basically  me saying hey i’m done with this  function  if i was to take this and put this up  here above the return keyword now we’re  going to be fine right because over here  it’s still printing out hello that’s  because i printed this out before that  return keyword so really you can return  any type of value that you want you  could return numbers like i did over  here you could return a string you could  return re you could return an  associative array basically you can  return any you know type of information  that you want to return  using that return statement you could  also just return nothing so i could just  come over here and just say return  and this will still break me out of the  function but it won’t return a value and  sometimes people will do that if they  just want to kind of like break out of a  function but i’d say for the most part  you’re going to be returning information  back to  the caller  in this tutorial i’m going to show you  guys how to use if statements in php now  an if statement is basically a special  programming structure which allows our  programs to make decisions so by using  if statements i can actually allow my  program to respond to the different  pieces of information in the program and  do different things in different  situations so if statements are  extremely useful and basically they just  make our programs a lot smarter so i’m  going to give you guys a total  introduction into if statements we’ll  look at a basic example and then later  on the course we’re going to use if  statements to do a bunch of different  stuff so over here i have this little  text file open and one of the cool  things about if statements is that  we actually encounter tons of if  statements um in our everyday lives and  i’ve kind of like highlighted a couple  of that you might see every day over  here it says i wake up  if i’m hungry i eat breakfast and this  is actually an if statement believe it  or not this is the type of thing that  we’re going to be able to code into our  programs so it says i wake up if i’m  hungry i eat breakfast now basically  this is saying that if a certain  condition is true then i’m gonna do  something so it says if i’m hungry  and if i’m hungry is either true or  false right you’re either hungry or  you’re not so if i’m hungry i’m gonna  eat breakfast right so if this condition  up here is true  then i’m gonna perform this action down  here i’m gonna eat breakfast but if this  condition up here is false in other  words if i’m not hungry then i’m just  gonna move on right so i’m not gonna eat  breakfast i’m just gonna kind of move on  with my life here’s another one down  here it says i look at my phone  if it’s about to die i charge it and  this is another if statement so it says  if my phone’s about to die and this is a  condition so this is either true  or it’s false if it’s true in other  words if my phone is about to die then i  charge it if it’s false then i just move  on and then finally there’s one more  down here it says i leave my house  if it’s cloudy so here’s a condition the  condition is saying if it’s cloudy if  that’s true if the condition is true  then i’m going to bring an umbrella  otherwise though i’m gonna bring  sunglasses and you can see  this if statement is a little bit  different than the ones that i showed  you up there because we have this little  otherwise right so if it’s not cloudy in  other words if this condition up here is  false  then we’re going to bring sunglasses so  this is essentially as complicated as if  statements are it’s just we’re checking  a condition we’re checking to see if  something is true like whether or not  it’s cloudy or whether or not your  phone’s about to die or whether or not  you’re hungry we’re checking a condition  if that condition’s true we’re going to  do something and in some cases if that  condition is false like down here we  could do  something else so i want to show you  guys how we can sort of use this type of  if logic in our programs to help our  programs to make decisions all right so  i’m going to go back over here to my  site.php file and this is the file that  i’m you know sort of writing all my php  in and then down here in the php blocks  i’m actually going to create an if  statement so i’m going to create a very  simple if statement just to illustrate  how we can do this in php all right so  down here the first thing i’m going to  do before i create my if statement is  i’m actually going to create a variable  so i’m just going to say  dollar sign and i’m going to call this  variable is male  and this variable is going to keep track  of whether or not someone is a male so  i’m going to say is male and why don’t  we just set this equal to true so this  is a boolean variable it’s storing a  boolean value which means it’s storing a  true or a false value now that i have  this boolean variable what i want to do  is i want to write a program that will  respond to this so let’s say that inside  of my program if the person’s male in  other words if this variable is true i  want to do something  in order to do that i can use an if  statement so i could come down here and  i could just say if and then i’m going  to make an open and close parenthesis  and open and close curly bracket and  this is sort of like the general  template the general  skeleton of an if statement so over here  in these parentheses i want to write a  condition  right so if you remember when we were  looking at those if statements like  those real life if statements i had a  bunch of different conditions like if my  phone is charged or if i’m hungry or if  it’s cloudy outside right i had all  those conditions that were either true  or false and that’s exactly what i want  to put inside of these parentheses  essentially i want to put a condition  that’s going to be either true or false  in our case we have this variable up  here is male and this is a boolean  variable so it’s either true or false so  we can actually use this variable as the  condition for our if statement so i  could say if  and then down here i’m just going to say  is male  basically what i’m doing here is i’m  checking to see if  the person is male and if the person’s  male  then inside of these curly brackets i  can specify what we want to do so i  could just basically echo out like  you are male  and so now this if statement is actually  going to be able to respond to this  variable so if this variable is male is  true  then we’re going to print out you are  male but if it’s not true then we’re  just gonna move on and we’re not gonna  print out you are mail so let’s go ahead  and run this program  and you’ll see over here it says you are  male  the value of ismail was true but if i  change this to false  now when i run my program we’re not  going to be printing out you are mail  we’re just going to print out nothing  and that’s because over here this line  of code in other words the line of code  that’s in between these curly brackets  is only going to get executed when this  condition up here inside the parentheses  is false and so down here i can specify  whatever code i want i mean i could have  you know 20 or 30 lines of code if i  wanted to i just have one here for  simplicity’s sake but let’s say that we  want to make this a little bit more  complex right so over here when the  person’s male we’re able to tell them  that right so if this variable over here  is true then we’ll execute this line of  code but what about when the variable is  false what about when the condition  up here is false let’s say that instead  of just moving on and printing out  nothing we wanted to tell them that they  weren’t male so we wanted to print out  like hey you’re not male well what i  could do is i could come down here and i  can use another keyword in php which is  called else so i can say else just like  that i can make an open and close curly  bracket and now down here inside of this  open and close curly bracket i can  specify code that’s going to get  executed when this condition up here is  false so i could actually come down here  and i could print out echo and i could  say you are  not male  and so now  if this variable up here is set to false  in other words if the condition inside  of these quotation marks is false  then we’re going to execute this line of  code so let me show you guys that since  ismail is false now when i refresh my  page  our program will respond to that so my  program is saying you are not male in  other words my program is now smart  enough to be able to respond to this  variable so if that variable is true my  program can respond to it if it’s false  my program can also respond to it that’s  because i put this variable here in this  if condition so that’s really the basics  of what if statement is it allows us to  respond to different situations in the  situation that the person’s male  we’re able to respond to that and tell  them that they’re male in the situation  that they’re not male we can also  respond to that and really at a core  level that’s what an if statement is  it’s just a way for us to respond to the  different information to the different  data  that is inside of our programs but  really this is just scratching the  surface so i want to show you guys how  we can make this even more powerful and  what i’m going to do is i’m actually  going to create another variable up here  so in addition to a variable called  ismail i’m also going to create a  variable called is tall and i’m going to  go ahead and set this equal to true so  this is also going to be a boolean  variable and why don’t i also set this  guy equal to true up here  so now not only am i dealing with one  piece of information but i’m dealing  with two pieces of information so  let’s see if we can make our if  statement powerful enough to be able to  handle both of these pieces of data well  let’s say that when the person was male  and when the person was tall we wanted  to do something so if the person was  like a tall male then we wanted to be  able to tell them that i can actually  come down here and i could modify this  condition so instead of just checking to  see if they’re male i’m also going to  check to see if they’re tall so over  here inside of this condition i can say  if is male and then i can use what’s  called the and operator and it’s just  two ampersands just like this and  basically what this is going to allow me  to do is check another condition so in  addition to just checking the ismail  condition i could also come over here  and i could check to see if they’re also  tall  and basically what this is saying is if  the person is male  and  they’re tall  then we’re going to execute this code  down here  but if they’re not male or they’re not  tall then we’re going to go ahead and  execute this down here so in order for  this condition to be true they have to  be both male  and they also have to be tall so down  here i could say like you are a tall  male because  if this code gets executed that means  they’re both male and tall so now when i  run my program you guys will see because  both of these are true now it’s going to  tell me that so it’s going to say you  are a tall male but here’s the problem  if i came over here and set one of these  guys equal to false so if i set is tall  equal to false  now because both of these guys aren’t  true in other words because one of them  is false now this whole thing is going  to be false and we’re going to come down  here and print this out and so it’s  going to say you are not male so  obviously we could change the text for  that but you guys will see what happens  so i’m going to refresh the page and now  it says you are not male so basically it  executed the code that was down here in  this else block because one of these  guys was false and that would do the  same thing if both of them were false so  when i use that and operator both of  these guys have to be true and i also  want to show you guys another operator  which is called the or operator and the  or operator is just two vertical bars  just like that and it’s actually very  similar to the and operator it allows us  to check two conditions but the  difference is with the or operator only  one of these conditions needs to be true  in order for the whole thing to be true  so unlike and where is male had to be  true and is tall had to be true now only  one of these guys has to be true and  will still execute this code so you’ll  see here because ismail is true  we’re still going to be able to say that  you’re a tall male and you can see it  works out just like that so that’s sort  of the difference between and and or  essentially it just allows us to check  multiple conditions  i’m actually going to turn this back to  and  and let’s sort of talk about this if  statement some more so you’ll notice  here i’m checking to see if they’re male  and i’m also checking to see if they’re  tall right so in a situation like that  we can tell them that so we can tell  them hey you’re a tall male but what  about the situation where they’re male  but they’re not tall  let’s say that in that situation in the  situation where um this condition is  true and this condition is false i  wanted to do something else so maybe i  wanted to print out like hey you are a  short male because they’re not tall well  i can actually account for that and i  can use something called an else if and  an else if is basically a way for me to  check another condition  if this condition up here is false so  what i could do is i can come down here  and i’m just going to type out else if  just like that and then i’m going to  make an open and close parentheses and  i’m going to make an open and close  curly bracket and then i’m just going to  type enter so you can see how i  basically just embedded this little  elsif right in here and it goes before  this else so here’s this if statement  and we have this opening curly bracket  the closing curly bracket we have the  else f with the opening curly bracket in  the closing curly bracket and then  here’s that else finally so basically  what this means is if this condition up  here is false  then instead of just jumping down here  to the else we’re going to come back  down here to the else if and we’re going  to check another condition so over here  in these parentheses i can actually  specify another condition and let’s say  that we want to check to see if the  person is male  and they’re not tall so instead of  checking to see if they’re tall i want  to check to see if they’re not tall so i  can actually come down here and say  dollar sign is male  so i want to check to see if they’re  male  and i want to check to see now if  they’re not tall and in order to check  to see if they’re not tall i can use  something called the negation operator  and the negation operator will basically  take the opposite of the condition that  we specify so i could say exclamation  point just like that and then i can type  out is tall and because i included this  exclamation point here it’s basically  going to negate whatever this value is  so if that value is true  then this exclamation point is going to  make it false and if that value is false  the exclamation point is going to make  it true and so essentially we can use  this exclamation point to check to see  if they’re not tall and so i would read  this else if  is male and  not is tall so is not tall and so down  here what we could do is we could  actually print out you are a short male  so i could say you are a  short male and so now if i set is tall  equal to false like i did up here this  if statement is actually going to be  able to catch that so now when i refresh  my page it’s telling me you are a short  male  so it checked this condition up here and  this condition was false because one of  these guys was false  then since this was false it came down  here and checked this else if and it  checked to see if they were male and if  they were not tall  and that ended up being true because  ismail is true and is tall is false  and so we printed out you are a short  male so i could do the same exact thing  for the other scenario where they’re  tall  but they’re not male so i could actually  come down here and i’m going to make  another elsif so i’m just going to say  elsif open and close parentheses open  and close curly bracket  and i’m going to click enter  so now i have another elsif here and i’m  going to check another condition  now i want to check to see if they’re  not male and they’re tall so i can say  exclamation point dollar sign is male  and dollar sign is tall  so this is going to be true when they’re  not male in other words when ismail is  equal to false  and when is tall is equal to true so i  can again just kind of copy this guy  and i’m going to say you are  not male  but are tall  so i’m basically saying that they’re not  a male but  nonetheless they are tall  so basically now we’re covering every  possible situation we’re covering the  situation where they’re male and tall  we’re covering the situation where  they’re male and short we’re covering  the situation where they’re tall but not  male and then finally down here this  else is going to cover the condition  where they’re not male and not tall so i  could say you are not male  and not tall  and basically this else is going to get  executed when none of these conditions  up here is true so when none of that is  true we’ll come down here and we’ll  execute this else so we actually have an  if statement that will cover every  possible scenario for these two  variables so if they’re both equal to  true  it’s going to tell us you’re a tall male  if is male is equal to false our program  is going to be able to respond to that  so our program will tell us you’re not  male but are tall  if i set is tall equal to false and i  said is male equal to true our program  once again is going to be smart enough  to respond to that so it’s gonna say you  are a short male and finally if i set  both of these guys equal to false  then our program once again will respond  to that and it’s gonna say you are not  male and not tall so using these if  statements my program was able to  respond to the different pieces of  information that it was given and in  this tutorial i covered a lot of the  basics i covered how to use if how to  use lsip how to use else we talked about  this and operator we talked about the or  operator we talked about the negation  operator but there’s actually a lot more  to talk about and in the next tutorial  i’m going to talk about another subject  with if statements  which is called comparisons so inside of  these if statements instead of just  using like boolean variables like i did  over here i could actually compare  different pieces of information so i  could compare like numbers or strings  or you know different different types of  information and i could use those  comparisons as the basis for my if  statement conditions so stick around for  that and we’re going to talk about how  we can use comparisons with if  statements  in this tutorial i’m going to show you  guys how we can use comparisons in if  statements in php so a lot of times when  we use if statements one of the things  we can do is we can actually compare  different information  so for example if i had two pieces of  information in my program i could  compare those pieces of information and  depending on the result of that  comparison i could do different things  and one of the coolest things about if  statements is that it allows us to  compare and you know sort of like work  with all the different pieces of  information in our programs so down here  in my php tags i’m actually going to  create a function  and what i want to do is i want to  create a function which i can pass two  numbers and it’ll tell me the maximum of  those two numbers so there’s actually a  function like this already in php it’s  called the max function so i could say  echo max and then over here i could pass  it like a three and a six  and basically what this is gonna do is  it’s gonna tell me which number is  bigger so when i refresh my browser over  here it gives me a six right so no  matter what numbers i pass in here this  function will always be able to tell me  which one is bigger and i want to show  you guys how we could actually make a  function just like this  on our own in php so instead of using  this i could actually write my own so  what i’m going to do is i’m going to  create a function so i’m just going to  say function and i’m just going to call  this get max so i’m not going to use the  name max just because that other  function is already using it so i don’t  want to like confuse myself  so i’m just going to say function get  max and then over here i’m going to make  an open and close parentheses and open  and close curly bracket  so that max function that we looked at  before took two parameters so two  numbers so i’m just going to say num1  and num2 so we’re going to take in two  parameters what we want to do is we want  to figure out which of these numbers is  bigger so i want to figure out if num1  is bigger or if num2 is bigger then i  want to return that back to the caller  so i want to actually return that piece  of information  back so here’s the question how can we  figure out which of these two numbers is  bigger in other words how can we figure  out which one we should return back to  the caller well we could actually use an  if statement so i’m going to go ahead  and set up an if statement i’m just  going to say if i’m making open and  close parentheses open and close curly  bracket  and inside of this parentheses we need  to specify a condition now if you’ve  been following along with the course  you’ll know that in the last tutorial i  gave like a full overview of if  statements and we were using boolean  variables inside of these parentheses  and so essentially what we need to put  inside of here is a true or a false  value we need to put a condition but one  of the things i can do with these if  statements is i can actually compare  different values so i could say like if  num1 is greater than  num2  and you might not think that this is a  true or false value but it actually is  i’m comparing these two values together  and num1 is either greater than num2 or  it’s not right it’s either true or it’s  false like this comparison num1 greater  than num2 is either true or it’s false  num1 is either bigger or it’s not and so  this actually gets resolved down to a  true or a false value and therefore we  can put it inside of these parentheses  and use it as a condition so i can say  if num1 is greater than num2 then down  here i can do something so i’m actually  just going to return  num1 and remember whenever i use this  return keyword it breaks me out of the  function so whenever i use this return  keyword then we’re basically done with  the function we kind of leave the  function we go back to  wherever the function was called from  and so i’m basically just going to  return num1 back and that’ll be the end  of this function  otherwise though i’m going to say else  so if this condition isn’t true in other  words if num1 isn’t greater than num2  then i’m just going to return  num2 so believe it or not this is  actually all the code that we need for  this get max function right we’re  getting these two numbers i’m saying if  num1 is greater than num2 then i’ll  return num1 right because if this  condition’s true i know for a fact num1  is the biggest  otherwise though we’re going to return  num 2. all right so down here below this  function i’m just going to call it and  i’m actually just going to go ahead and  echo out the answer  so i’m just going to say  get max and i’m going to pass in two  numbers so let’s pass in a 3 and a 90.  so this one’s pretty obvious we should  get 90 back assuming we wrote that if  statement correctly and i’m going to  refresh the page and actually looks like  i forgot to put a semicolon over here  that’s my bad  um all right so now when i refresh the  page we shouldn’t get an error and  you’ll see we’re getting 90. so looks  like this function worked now i’m going  to say let’s try num1 as the biggest so  let’s say the first number we pass in is  going to be the biggest we’ll see if we  can handle it and we can so we get 300.  so looks like our get max function is  working and that’s all thanks to this  comparison right i compared these two  different numbers and i was able to  return the correct answer  but let’s say now that instead of just  passing in two numbers we wanted to make  this function a little bit more complex  so let’s say we wanted to be able to  figure out the maximum of three numbers  so in addition to passing in num1 and  num2 i also wanted to pass in  a num 3 well this actually makes our  function a little bit more complex but  nevertheless we should still be able to  figure it out so why don’t we start over  and we’ll start fresh with a new if  statement so i’m going to create a new  if statement and now we need to figure  out what types of comparisons we need to  make in order to figure out which of  these numbers is the biggest what we can  do is we can basically check to see if  num1 is the biggest first so i could  check to see if  num1 is greater than or equal to  num2  and so i’m going to use this and  operator num1 is greater than or equal  to num3 so unlike before when i was just  checking to see if it was greater than  now i’m checking to see if it’s greater  than or equal to and honestly we could  use either of them here but i’m going to  use greater than or equal to  so if this is true in other words if  this whole condition up here is true  that means that num1 is the biggest so i  can just return  num1 just like that so basically i’m  checking to see if num1 is greater than  or equal to num2 and if it’s greater  than or equal to num3 but let’s say that  this isn’t true right so let’s say that  num1 isn’t the biggest well i can  actually come down here and i can say  else if  so i basically want to check another  condition so if num1 is not the biggest  let’s check to see if num2 is the  biggest so i can just say num2 greater  than or equal to num1  and  num2 greater than or equal to num3  and basically if this condition is true  that means that num2 is the biggest  right because it’s bigger than num1 and  it’s bigger than num2 or greater than or  equal to and over here i can just return  num2 because that means num2 is the  biggest then finally we can just make an  else and i can say else let’s return  num3  and this one’s a lot easier because if  num1 is not the biggest and num2 is not  the biggest then we’re only left with  one option which is num 3 being the  biggest so by process of elimination we  figured out that num3 is the biggest all  right so now we have our updated get max  function let’s come down here and we’ll  test it out so  390 and let’s make one more in here why  don’t we do like 400. so now this should  give us back a 400 hopefully so over  here i’m going to refresh the page and  you’ll see we’re getting back 400 so  even with three inputs our function was  still able to run why don’t we try to  make this one the biggest the middle one  so  we should get 900 and let’s make the  first one the biggest now and we’ll get  3 000.  cool and then we can check to see if  it’s still gonna work if two of them are  equal so  we’ll make this one 3000 as well and  we’re still getting 3 000. so  looks like our function is working we  tested out all the different  possibilities and um we were able to be  successful so down here you’ll notice  that i used these comparison operators  and that’s what they’re called they’re  called comparison operators so things  like greater than or equal to  right so we have greater than  less than  greater than or equal to  and then we have less than or equal to  and in addition to those we also have a  double equals which is going to mean  they’re equal so this is basically  saying if num1 is equal to num2 that’s  going to be a double equals we can also  use an exclamation point equals which is  going to check not equals so this is  going to this whole thing over here is  going to be true if num1 is not equal to  num2 so those are all the different  comparison operators that we have and in  addition to doing this with like numbers  we could also check to see if like two  strings are equal so i could like check  to see if one string is equal to another  string  you can use basically all the data types  inside of these comparisons but  hopefully that gives you a sort of an  idea of how we can use comparisons  inside of our programs honestly you’re  going to be using comparisons all the  time with if statements so you want to  make sure that you have a firm grasp on  how to use them  in this tutorial i’m going to show you  guys how to build a four function  calculator in php now if you’ve been  following along with this course you’ll  know that in the beginning of the course  we actually created a very basic  calculator the calculator basically just  allowed the user to enter in two numbers  we took those numbers and then we added  them together and printed the answer out  onto the screen in this tutorial though  i’m going to show you guys how we can  build a fully functional calculator  which can do not only addition but  subtraction multiplication and division  and we’re going to allow the user to  decide which operation they want to  perform so this is going to be pretty  cool and we’ll get to use  if statements in order to do this so  down here inside of this program i have  this form that i set up in my html  and the action is just site.php that’s  this file and then the method is post  and then i have this submit button so  the first thing that i’m going to want  to do if i’m going to build this  calculator is i’m going to want to be  able to get information from the user  and actually for this particular  calculator we’re looking for three  pieces of information we’re looking for  the first number we’re looking for the  second number and we’re also looking for  the operation that they want to perform  so not only do i want to know what two  numbers they want to use but i also want  to know if they want to add subtract  multiply or divide so we’re going to  create input boxes for all of those  different things so i’m just going to  say input  type is going to be equal to  number so we’re going to use this to get  the first number and then i’m going to  give this a name and i’m just going to  set it equal to num1 and then i’m just  going to put a  break over here  so this is going to allow us to get num1  and i’ll basically just say first num  and then what i’m basically just going  to do is copy this and we can use this  same template to get the second number  so over here i’ll just make a new line  and we’ll paste this down here  for the second number so now we’re able  to get the first number and we’re able  to get the second number i’m going to  change this to num2 and the last thing  we want to do is we want to get the  operator so i’m just going to say op and  that’s going to stand for operator  and i’m actually going to create another  input and i’m just going to say type and  we’re going to set this one equal to  text box so we’re going to allow the  user to type in like a plus sign a minus  sign a multiplication sign or a division  sign  and then over here i’m just going to say  name and we’re just going to say this is  gonna be op  once again for operator and then we’ll  put a break over here so i have three  input boxes the first num  second num and the operator these two  are getting a number  and this one over here is just getting  text and actually this should just be  text not text box my bad so  now we’re able to get information from  the user and if you see over here on my  web page  that should all work out so we have you  know boxes for each of these different  inputs and then we have our submit  button so our job now is to get that  information  i’m actually going to store it inside of  different variables and then we need to  figure out what operation they wanted us  to perform so down here let’s just  create a few different variables so i’m  going to create a variable  we’ll just call it num1 and i’m going to  set this equal to  whatever the user entered so it’s going  to be post and we’re going to get the  number from the num1  number box and then i’m going to do the  same thing for num2 so i’ll say num2 is  going to be equal to  post and  num2  and then finally i’m going to do the  same thing for the operator so i’m just  going to say op which is going to stand  for operator is going to be equal to  post  and here we’re just going to get op  so  these two are going to be numbers and  this one is going to be a string of text  so what we want to do now is we want to  figure out what operation the user  wanted to perform in other words we have  these two numbers right and it doesn’t  really matter what those are but we also  have this operator and in order to  figure out if we need to add the numbers  subtract them multiply them etc we need  to figure out what’s inside of there in  other words we need to figure out if  it’s a plus sign or if it’s a minus sign  and in order to do that we can use an if  statement so  by using an if statement we’ll allow our  program to respond to this value so down  here i can just say if i’ll make an open  and close parentheses open close curly  bracket  and the first thing i want to do is  check to see if this is a plus sign so i  can basically just say if  op  is equal to a plus sign and if the  operator is equal to a plus sign then we  can basically just echo out num1 plus  num2  so if it’s a plus sign then we’ll just  write out the answer so it’s going to be  num1 plus num2 and then we can keep  checking different things so if that’s  not the case then i can say elsif and  i’ll come down here and i’ll check to  see if  the operator is equal to  a minus sign if the operator is equal to  a minus sign then we can just echo out  num1 minus  num2 because if this condition over here  is true that means we want to subtract  the numbers and i can actually do the  same thing for division and  multiplication i’m going to copy this  and i’m going to paste it down here  because we’re basically doing the same  thing and i’m just going to say division  sign so i’m going to do that forward  slash and i’m going to make a forward  slash here and then i’ll paste this one  more time for multiplication so here  it’s going to be an asterisk and then  down here we will multiply them so this  if statement is basically checking if  it’s a plus sign if it’s not it’s  checking if it’s a minus sign then it’s  checking if it’s a division sign or a  multiplication sign but there’s also one  more situation that could occur and  that’s when the user entered in an  invalid operator so they didn’t enter in  one of these four up here so we can just  say else  and then down here i can just echo out  an error message so i could just echo  out like  invalid operator right so that’ll cover  the case where the user entered in an  invalid operator so basically this if  statement is going to allow me to figure  out what’s inside of the operator  variable in other words it’ll allow me  to figure out what the user entered as  an operator and depending on what they  entered i can perform that operation  down here so this should be fully  functional why don’t we refresh the page  and we’ll go ahead and run this program  so you can see down here it’s saying  invalid operator that’s basically just  because i haven’t entered anything yet  so if i come up here i can say first num  why don’t we say this is going to be 10  and why don’t we do addition and then  the second number is going to be like 35  so now when i click submit it should add  those numbers together so it’s going to  look through that if statement figure  out which operator we submitted and it’s  going to do  the operation for us when i click submit  you see we get 35. so why don’t we do  30 and we’ll multiply 30 by 2. so now we  should get 60 and you can see down here  that we do so that’s actually working  pretty well let’s do one more so i’m  going to say like 45 and let’s make the  operator like some nonsense like draft  and then we’ll just say 35 35. so this  is actually going to be an invalid  operator so our program will recognize  that and it will show us so down here we  were able to use this if statement in  order to figure out what the operator  was that the user entered and that is  actually pretty awesome so hopefully you  can see how that works and you can kind  of see like you know how something like  this could be useful and i also want to  show you guys um one more thing and this  isn’t like directly related to this  calculator but it’s a little thing that  i think some people might be confused  about so actually if i came over here  into my calculator program and i entered  in like a decimal number so if i entered  in something like 4.6 and i click submit  you’ll see that we’re actually getting  this error here and it says please enter  a valid value  um  and actually let me do it again so we  get that um the nearest values are four  and five so it didn’t actually let me um  put in here a decimal number and that’s  actually has to do with um how html  works and it basically just has to do  with how this input tag works over here  so by default when we say number this is  only going to take like whole numbers  but we could actually modify that so we  could make it so we could use decimal  numbers so i could just say over here  step and i could set this equal to like  0.1 for example and now this is gonna  specify that we can take numbers um to  this decimal point so  we can basically say numbers to the tens  place so over here now i should be able  to enter in a 4.6  [Music]  with no problem so i could say like 4.6  plus  5.0 and it’ll be able to do that math  for us but you’ll notice if i tried to  do like 4.567  this is gonna throw an error again  because the step is not that significant  so if i said  step is 0.001  now i’m going to be able to enter in a  number just like this so i could say  like  4.567  plus  and i could do like nine and now it’ll  be able to add these numbers together so  that’s not necessarily like a php  limitation that’s more of an html  limitation  but if you were a little bit confused  about that hopefully um that clears it  up a little bit but you know the main  point of this tutorial was to kind of  show you guys how we could use an if  statement to figure out  what a user inputted into our program  in this tutorial i’m going to talk to  you guys about switch statements in php  a switch statement is basically just a  special type of if statement which we  can use to compare one value to a bunch  of different values so there’s certain  circumstances when we want to use the  functionality of an if statement but we  want to check a bunch of different cases  and in a situation like that we can use  a switch statement in order to make it a  lot easier so i’m going to show you guys  how to use switch statements and we’ll  kind of i’ll show you guys an example in  this tutorial that will hopefully  illustrate it so over here in my program  i have a basic little program set up  basically i have this form over here and  i’m asking the user what their grade was  so this is a program where the user can  enter in their grade and it’ll basically  like tell them what they got so the user  could enter in a grade that they got on  a test for example and then down here  i’m storing the grade that they enter  inside of this grade variable and i’m  just printing it out onto the screen so  it’s very simple and over here i’ll show  you guys how it works so let’s say i got  an a plus on my test i could put a plus  in here and then i could click submit  and it’ll just print it out down there  so this is a pretty simple program but  we’re actually going to make it a little  bit more interesting with switch  statements so let’s say that instead of  just printing out the grade that they  got instead we wanted to tell them how  they did so if the user got like an a  then we could basically be like hey you  did really well on this test or if the  user got a b we could be like hey you  did all right or if they got a c we  could be like you could do better next  time and if they got an f we could tell  them that they failed so in other words  what if we can make our program be able  to respond to the grade that the user  got well i’m going to show you guys how  we can do that really easily with  something called a switch statement and  like i said a switch statement is very  similar to an if statement it’s  basically another way that we can allow  our program to respond to different  information  a switch statement though is used in a  situation like this where we want to  compare the grade to a bunch of  different possible values so i’m going  to show you guys how to do this i’m just  going to come down here and we’ll set up  the switch statement so you’re just  going to type out switch and open and  close parentheses and an open and close  curly bracket now inside of these  parentheses i want to put a value  so what we could actually do is we could  put this grade right here so i’m just  going to pass in  dollar sign grade  and basically what this means is that  we’re going to compare  the grade to a bunch of different things  and depending on whether or not it’s  equal to those things then we’ll do you  know something here or there so inside  of this switch statement i can create  something called a case so i just want  to type out case and then i’m going to  type out a capital a just like this and  then i’ll type out a colon  basically what this means is that in the  case that the grade is equal to a  i can come down here and i can actually  type out some code so i could just type  out like echo and i’ll just say  you did amazing and then i can just put  a semicolon here and then i’m gonna say  break right here and i’ll explain to you  guys what break does in a second  basically what this is saying though is  it’s saying that in the case that grade  is equal to an uppercase a then we’re  going to type out you did amazing so  actually let’s go ahead and try this out  so over here i’m going to go ahead and  type in an a so i’m actually going to  refresh the page and i’ll put a capital  a in here now when i click submit  instead of just printing out capital a  it’s going to print out you did amazing  right so it’s actually able to respond  to the value that i put in there if i  put like a b in here for example though  and i click submit it’s not printing out  anything right that’s because i didn’t  tell it to do anything  but what i could do is i could create  another case for b so i could come down  here and say case  and then capital b  and i’ll type a colon and now i can type  out echo  you did  pretty good  and then once again i’m going to say  break down here so now if i go over to  my program and i refresh the page  and i was typing in a b  now it’s going to tell me  you did pretty good so if i type in  capital a it says you did amazing if i  type in a capital b  it says you did pretty good so it’s  actually able to respond to the  different grade that i got so what i  could do is i could basically create one  of these cases for all of the possible  grades now i want to explain one more  thing which is this break statement and  this break statement is basically a  statement that will break us out of a  programming structure so switch over  here is a programming structure right  we’re in here in between these open and  close curly brackets and when we put  break here it basically will break us  out of the switch statement so for  example let’s say that the grade was  equal to a right so this case was true  and we came down here and we printed out  you are amazing if i didn’t put a break  statement here  then this switch statement would keep  executing so even though i figured out  that the grade was an a i would keep  looking through all of the other cases  so the reason we put a break here is  because once we figured out that the  grade was equal to a i don’t want to  check any more cases right so i’m just  going to put a break there and it’ll  break us out now you don’t need to put  the break there but a lot of times  people will just because it’s more  useful so i can create a case for all  the possible grades so i could create  one for c d and f and i’m actually going  to go off i’m going to do that and then  we’ll come back and we’ll see what i did  all right so i went ahead and i created  cases for most of the common grades so i  have one for a  b c  d  and f so for each one of these grades  it’s basically giving you a different  message if you get a d it says you did  very bad if you get a c it says you did  poorly if you get an f it says you fail  and we have cases for all of these  possible grades so now if i actually  came over here into my program this  thing will be able to respond to a lot  of different grades so i’m going to  refresh the page and we’ll just click  continue and now if i typed in like an f  for example and i click submit it’s  going to tell me that you fail if i type  in a c  it’ll say you did poorly if i type in an  a it’ll say you did amazing if i typed  in a b  it’s going to say you did pretty good  there’s one problem with this program  though and if i type in a grade like an  invalid grade for example if i typed in  a g right here and i click submit you’ll  notice that nothing’s getting printed  out right so no errors getting thrown  like the program is still running but  nothing is actually getting printed out  and the problem is that we’re not  handling that situation so over here in  this case statement we don’t have a case  for like every possible input but if the  input isn’t one of these valid grades  maybe we could like tell the user hey  you entered in an invalid grade and to  do that i could actually come down here  and use what’s called a default  statement so i could just type out  default and i’m going to type a colon  and then down here i can basically just  type out something i want to do  so i can just say like invalid  grade and essentially what this is gonna  do is um  when none of these cases up here are  true in other words when it’s not a b c  d or f  then we’re going to go ahead and execute  this code and it’s just going to say  invalid grade so now when i come over  here to my program and i type in like if  i typed in an a it’s still going to tell  me that i did amazing but if i typed in  a g now it’s going to tell me that i had  an invalid grade so that default  case is actually going to get executed  for us so that’s kind of how we can use  these switch statements and like i said  a switch statement is very similar to an  if statement in fact everything that you  can do with a switch statement you could  do with an if statement it’s just that  switch statements make it a lot easier  for us to do that and really switch  statements are used in a situation where  you have one value like the grade and  you want to compare it to a bunch of  different values so switch statements  are very useful and there’s tons of  situations where these will come in  handy  in this tutorial i’m going to show you  guys how to use while loops in php a  while loop is basically just a  programming structure which allows us to  loop over a specified block of code  while a certain condition is true  basically loops allow us to just keep  repeating something as long as a certain  condition is true so i’m going to show  you guys in this tutorial the basics of  working with while loops we’ll kind of  look at what they’re doing how they work  the ins and outs of what they are and  then we’ll also look at a couple  variations on the while loop  specifically something called the do  while loop so this is going to be a  pretty fun tutorial down here in my  little php tags i’m going to actually  show you guys a while loop so before i  do that though i’m going to create a  variable and i’m just going to call this  index  and i’m going to set it equal to 1. so  this is a very simple variable i’m just  storing a number inside of it and then  down here i’m actually going to create a  while loop and you guys will see how  that index variable comes into play in a  second if i want to create a while loop  i can just type out while  open close parentheses and an open and  close curly bracket now like i said a  while loop is basically a structure  which we can use to loop over a certain  block of code while a certain condition  is true so there’s going to be certain  things in php that we want to just like  continuously do and a while loop can  allow us to do that there’s two parts to  the while loop the first is called the  loop condition and the second part is  called the loop body and the loop  condition is a lot like a condition in  an if statement so if you’re familiar  with an if statement  basically the first thing we need to put  in an if statement is a condition and  that condition will tell the if  statement whether or not we should  execute the code in between the curly  brackets it’s the same for a while loop  in a while loop we’re going to specify a  condition inside of these parentheses  and that condition is going to determine  whether or not we should keep executing  the code in between the curly brackets  so i’m going to create a very simple  condition i’m just going to say wow  index  is less than or equal to 5. so this is  basically saying that i’m going to keep  looping through this while loop as long  as the value stored inside of the index  variable is less than or equal to five  and then down here inside of these curly  brackets i can specify some code that i  want to be continually executed over as  long as this condition is true so i’m  basically just going to print out the  value of index so i’m just going to  print out  index and then i’m also going to print a  break statement just so this is a little  bit easier to see all right and then one  more thing underneath this echo  statement i also want to increment the  index variable so i can just say index  plus plus and if you remember when we  say index plus plus this is the same as  saying index is equal to  index plus one so i’m essentially just  incrementing this variable by one so  every time we go through this while loop  this index variable is going to get  incremented so the first time we go  through the loop it’ll be equal to one  and the second time it’ll be equal to  two etc so i’m going to show you guys  exactly how this works i’m going to come  over here and refresh my page and you’ll  see we’re actually printing out one two  three four five so maybe this is what  you expected maybe it’s not what you  expected but let me go ahead and explain  why this is happening  so over here i create this variable  called index right and it’s basically  just a simple variable storing the value  one and now i create my while loop so  remember there’s two parts to the while  loop we have the loop condition up here  and this basically determines whether or  not we should keep a looping through the  loop and then down here we have the loop  body whenever we’re using a while loop  the first thing that php is going to do  is it’s going to check this condition so  before php does anything else before it  executes any of this code it’s going to  check this condition so it’s going to  check to see if index is less than or  equal to 5. if index is less than or  equal to 5 then we’re going to go  through and we’re going to execute all  of the code in this loop body so over  here we start with index equal to one so  the first time we go through the loop  index is equal to one so we’re going to  be able to pass this condition this  condition is going to be true and then  we’re going to print out one so you can  see over here we’re printing out one  then we’re going to increment the index  variable so now the index variable is  going to be equal to 2.  once we’ve executed all the code inside  of this loop body we’re going to jump  all the way back up and we’re going to  check this condition again so before we  can execute the code inside of the loop  body we have to check the condition so  on every single iteration of the loop  we’re going to check that condition  first if the condition is true we’re  going to execute the loop body otherwise  we’re going to break out of the loop and  we’ll be done so the second time we’re  going to print out two the third time  through the loop we’re going to print  out three  four and then five because every time i  go through the loop i’m continuously  incrementing the index variable  eventually we’re going to get to a point  where index is equal to 5. we’re going  to come down here we’re going to print 5  out and then 5 is going to get  incremented up to 6 and then we’re going  to come up here and check the condition  and 6 is not less than or equal to 5 so  then we’re going to break out of the  while loop and we’ll be done and that’s  the basics of how this while loop works  we define a looping condition as long as  that condition’s true we’re going to go  through and execute the code inside of  here so it’s actually very simple when  you think about it and while loops are  extremely powerful and there’s a lot of  stuff that we can do with them i do want  to point one thing out and this is kind  of like a warning  sometimes you’ll run into a situation  which is referred to as an infinite loop  and an infinite loop is a situation  where the condition up here inside of  these parentheses is never going to be  false so sometimes like you’ll run into  a situation where you forget to  increment something or you forget to  change a specific value  or a value just never changes and this  condition up here just stays true  forever basically what that means is  your loop is just going to keep looping  infinitely and we call that an infinite  loop so let me demonstrate if i was to  get rid of this um  line of code right here where it says  index plus plus right  i’m actually incrementing the index  variable there if i got rid of that then  index is always going to be one right  it’s never getting modified so this  condition up here is always going to be  true because index is always going to be  equal to 1. so now if i came over here  in my browser and i click the refresh  button you’ll notice that it’s just a  bunch of ones down here and you can see  over here like i could scroll down  infinitely and it’s just going to keep  being ones basically what happened is  my program is running and it’s just  continually printing out ones onto the  html document now i’m just going to go  ahead and terminate this so i clicked  that little x up here you don’t want to  let a loop like this run because it  could slow down your computer  significantly but i just kind of wanted  to demonstrate how something like that  could mess up your program so i’m going  to add this back in and now we’ll go  back to the normal program  infinite loops happen to everybody and  as you you know start learning more  about while loops i’m sure they’ll  happen to you but it’s just something to  be aware of where if something’s not  working correctly it might be because an  infinite loop is occurring  so a while loop is very simple and we  kind of looked at how they work i want  to talk to you guys about one other type  of loop though and it’s actually similar  to a while loop and it’s called a do  while loop so in order to illustrate  what the do while loop does i’m actually  going to show you guys a little  example we’re going to do a little  experiment i’m going to set index up  here equal to 6. so index initially has  a value of 6. now remember whenever  we’re using this while loop the first  thing that we do is check the loop  condition so before i go through and i  execute all of this code i always check  this condition  first and that is extremely important  that we do that if i set index equal to  6 well we’re never going to be able to  execute the code down here because it’s  not going to pass this condition so the  first thing it’s going to do is check to  see if index is less than or equal to 5  which it’s not so we’re not going to end  up executing any of this code so if i  was to run my program now  you’ll see that we’re not printing  anything out right that’s because we  never ran this echo command so we never  printed out the value of index there’s  actually another type of loop though  which i want to show you called a do  while loop so i’m actually just going to  take this line right here and i’m going  to paste it down here below so i’m  pasting it right after this  closing curly bracket and then i always  want to make sure that i put a semicolon  there on the end and then up here in  front of this opening curly bracket i’m  going to say do  and essentially what we have here is a  do while loop and a do while loop is  exactly the same as a while loop except  the order is reversed so instead of  checking the condition first and then  executing the loop body we’re going to  execute the loop body first and then  check the condition so there’s certain  circumstances where you’re not going to  want to check the condition first you’re  going to want to do something first and  then check the condition and that’s  where do while loops can come in handy  so even though i have index equal to 6  up here and it’s technically not going  to pass this condition what you’ll see  is we’re still going to be able to print  it out down here and that’s because in a  do while loop we’re executing the code  inside of the loop body before we check  the condition so now when i run my  program you’ll see i’m able to print out  six so all this code gets executed then  we check the condition to see if we can  execute it again and since six is  greater than five we just break out of  the loop so that’s sort of the  difference between while loops and do  while loops with a while loop we always  check that condition first and then  execute the loop body in a do while loop  we execute the loop body first and then  check the condition  i think probably for the most part  you’re going to be encountering while  loops a lot more than you will do while  loops  do while loops are more in a specific  circumstance but you will find dual  loops out there in the world so if you  see them now you’ll know the difference  in this tutorial i’m going to talk to  you guys about for loops in php now for  loop is a special type of loop which is  used in conjunction with an indexing  variable and basically what’s special  about for loops is that they allow you  to keep track of a specific variable as  you go through your loop so if you’ve  been following along with this course in  the last tutorial i showed you guys how  you could use while loops and do while  loops and while loops and do while loops  are awesome but a while loop is just a  very general type of looping structure  so a while loop can basically handle any  situation where you want to loop over a  specific block of code  a number of times and while loops are  really powerful in that sense but  they’re also really general and there’s  actually another type of loop in php  which is called a for loop and a for  loop serves more of a specific purpose  in a for loop not only can we loop while  a certain condition is true but as we go  through our loop we can keep track of  something called an iterating variable  so i’m going to kind of show you guys  how this works we’ll talk about um the  difference between a for loop and a  while loop and we can kind of look at  how we can use for loops in php all  right so down here in my program i  basically just have this while loop that  i was using in the last tutorial and  essentially what this is doing is it’s  just looping five times so i have this  variable index i set it equal to one and  i’m saying wow  index is less than or equal to five  we’re going to basically print out the  value of index and then increment index  so  over here you’ll see that when i print  this out we get like one two three four  five so i’m basically just printing out  um numbers one through five and actually  this is a very common situation so i  kind of want to point out what’s  happening here this variable index is  actually keeping track how many times  we’ve gone through the loop so on the  first iteration of this while loop  index has a value of one on the second  iteration of this while loop index has a  value of two on the third iteration of  the loop  index has a value of three index is  actually keeping track of how many times  we’ve gone through the loop and index is  actually what we would consider a  indexing variable or an iterating  variable basically this is a variable  that’s changing every time we go through  this loop so every time i execute the  code inside this while loop you’ll see  that this index variable is actually  changing so it’s incrementing we could  also decrement it if we want we could  add 5 to it it’s basically just a  variable that’s changing every time we  go through the loop and these indexing  variables can be extremely useful when  we’re working with loops and because  this is such a common and sought after  situation there’s actually a special  type of loop called a for loop which is  designed specifically for using an  iterating variable like index so i’m  going to show you guys  how we can use a for loop and we can  essentially use a for loop to do exactly  what this while loop is doing so down  here below this while loop i’m actually  going to create a for loop so i’m just  going to come down here and say four  and let me bring this over here four i  want to make an open and close  parenthesis and an open and closed curly  bracket so so far the while loop and the  for loop look pretty similar right the  four loop has a similar structure we  have this open and close parenthesis and  these open and close curly brackets but  there’s actually some key differences  and the biggest difference is that  inside of this while loops parentheses  we’re specifying one thing so we’re  specifying the looping condition but  inside of this four loops parentheses  we’re actually going to specify three  separate things so the four loops  parentheses is going to be a little bit  more complex than the while loops  parentheses so the first thing that we  want to put inside of this four loops  parentheses is going to be a variable  initialization now one thing i want you  guys to notice up here with this while  loop is i actually had to create this  indexing variable up here so outside of  the while loop i had to create a  variable give it a value and then i was  able to use it inside the loop well in a  for loop instead of having to place this  outside of the loop we can actually do  it right here in the parentheses so the  first thing that we’re going to do for  this for loop is i’m basically going to  create a variable so i can just say  dollar sign and i’m going to call this  variable i so this is going to do the  same thing as this index variable up  here i’m just going to call it i and i’m  going to give it a value so i’m going to  set it equal to 1. and then i’m going to  put a semicolon right there so the first  thing i’m doing inside these parentheses  is i’m creating a variable called i and  i’m giving it a value of one just like i  did up here for my while loop the second  thing i want to put inside of this  parentheses is going to be the looping  condition so over here in the while loop  my loop condition is actually right here  in these parentheses so that’s the  second thing that i want to put in the  for loops parentheses and you’ll notice  that i’m putting this semicolon here to  separate and now i’m going to specify  the looping condition so i could just  say i want to loop while the variable i  is less than or equal to five so this is  the same exact condition as i had up  here it’s just now we’re using this i  variable instead of the index variable  and then once again just like i did over  here i also want to put a semicolon here  and now i want to do one more thing i’m  going to put one more thing over here in  this parentheses and this is going to be  essentially just a line of code that i  want to execute after every iteration of  the loop now you’ll notice over here in  this while loop every time i go through  the loop i’m incrementing the index  variable so every single time we go  through the loop we add one to that  index variable and that’s essentially  what i want to put over here so this is  going to be a line of code that will get  executed after every iteration of the  loop and generally what we’re going to  be doing is we’re going to be modifying  this indexing variable in some way so  over here i’m just going to say i plus  plus and basically this is going to do  the same thing as it does over here this  is going to tell a php that every time i  go through the for loop i want to  increment it i want to add one to it so  that’s going to be extremely useful and  now this for loop is actually set up  identically to this while loop and all i  have to do is i could actually just  do this same line of code i could say  echo and i could just print out i and  then a break  so these loops right now for all intents  and purposes are exactly the same  they’re doing exactly the same thing so  they’re equivalent the while loop up  here and the for loop the difference is  though this while loop takes up one two  three four lines of code this for loop  really only takes up two lines of code  right and so this for loop is  essentially doing the same thing as this  while loop but it’s just way more  compact it’s way more streamlined and  it’s a lot easier for us to do this so  once again over here we have this  variable so we’re creating our indexing  variable just like we did up there  we’re specifying our looping condition  just like we did up here  and then we’re specifying a line of code  that we want to execute after every  iteration of the loop just like we did  over here so now i could actually get  rid of this while loop altogether and  i’m actually just going to run my  program and you’ll see we’re going to  get the same exact output because  it’s doing the same exact thing it’s  just that this for loop is way cleaner  way more streamlined and way more  optimized and really the benefit and the  advantage of using the for loop is that  we can keep track of this variable so i  have now this indexing or this iterating  variable that i can you know basically  modify and do whatever i want with all  right so using this for loop i want to  actually show you guys how we can loop  through the contents of an array and  this is actually a very very common use  case for a for loop it’ll kind of give  you guys an idea of how these for loops  can be used so i’m actually going to  create an array and i’m just going to  call it  lucky numbers and i’m just going to set  it equal to array open and close  parentheses and then in here we can just  put a bunch of numbers so i’m going to  say like 4 8 15 16 23 42. so i have a  bunch of numbers in here just six  numbers and i want to show you guys how  we can use this for loop in order to  print out all the numbers in the lucky  numbers array now just to kind of  refresh your memory whenever we have an  array if i wanted to access like a  specific element i could just say lucky  numbers and then i could put an index in  here so if i wanted to access this first  element i could just put a 0 in here and  that would give me access to this first  element so i’m going to show you guys  how we can use this for loop to print  out all the elements in here  the first thing i want to do is instead  of starting i off at 1 i actually want  to start i off at zero  and that’s because array indexes start  at zero so the first element in the  array is actually at index position zero  the next thing i want to do is modify my  looping condition right now it says i  want to keep looping as long as i is  less than or equal to 5 but really if  i’m going to loop through all the  elements in this array and print them  out i want to keep looping as long as i  is less than  count  lucky numbers and basically this is  going to tell me how many elements are  inside this lucky numbers array so this  should actually give us a six because  there’s six elements inside of here and  then just like before we’re going to  increment by one now down here instead  of just printing out i  i actually want to print out  lucky numbers square brackets  and now we’re going to print out i so  i’m printing out lucky numbers at index  position i  and i’m going to go ahead and run this  and you guys will see essentially what’s  happening so i’m going to refresh the  program and you can see we’re printing  out all of the numbers that were inside  of that array so essentially what’s  happening is the first time that we go  through this array i is equal to zero so  the first time we’re actually printing  out lucky numbers zero the second time  we go through the array i gets  incremented so i is now equal to one so  we’re printing out lucky numbers one and  we’re going to keep doing that until we  get to the end and remember even though  there’s technically six elements inside  of this array the index position of the  last element is actually five so lucky  numbers five is actually this forty-two  so actually what we can do is we can get  rid of this less than or equals and we  could actually just say less than  because we don’t need to go all the way  up to six we only need to stop at five  so now if i was to say like dollar sign  i this is gonna do exactly the same  thing as you can see over there so one  more time i’m just gonna walk you guys  through what we have here we started i  off at zero because array indexes start  at zero we said percent i is less than  count lucky numbers because we wanted to  loop through all the numbers in the  lucky numbers array and then we said i  plus plus  in this tutorial i’m going to talk to  you guys about using comments in php a  comment is basically just a line of code  inside of our php file which isn’t going  to get rendered by php so generally when  we’re writing  lines of code inside of our php files  like there are instructions that are  meant for the computer to carry out so i  can have an instruction down here like  echo and that’s meant for the computer  right i’m telling the computer i’m  telling php to do something but a lot of  times in our programs there’s gonna be  situations where we want to write out  little notes or little reminders for  ourselves or for other developers and to  do that we can use something called a  comment so a comment is basically any  text  inside of our php file that’s not meant  for the computer it’s meant for us  humans and we can create a comment  pretty simply all you have to do is just  type two forward slashes just like this  and i can come over here and i’m typing  a comment and you’ll notice just in my  text editor that this comment is  actually colored a little bit different  than  some of the code that’s down here and  that’s really because this is now a  comment so the text that i put after  these two forward slashes is no longer  an instruction that’s meant for the  computer now it’s basically just plain  text that i as the developer or the  programmer can use and sort of look at  so you can use comments to leave little  notes you could leave like a little to  do stub here  you could also do other stuff so a lot  of times people will use comments to  like describe a line of code so i could  describe this line of code down here i  could say like this line prints out a  string right so i’m basically like  describing what it’s doing you can also  use these comments after a line of code  so after here i could say like two  forward slashes and now i’m typing a  comment once again so anything that  comes after these two forward slashes is  going to be considered a comment  the thing is though these are only going  to work on a single line so if i was to  come down here and start typing you’ll  see now this isn’t no longer considered  a comment so only the stuff that’s on  the same line as these two forward  slashes is going to be a comment in a  lot of situations you’re going to want  to have comments that span multiple  lines so one thing you can do is just  have multiple lines with forward slashes  on them so i could do something like  that and you can see i’m printing on  multiple lines another thing you can do  though is you use starting and ending  comment tags and these are what we would  call comment blocks it’s basically just  going to be a block where you can put  you know as many comments and as many  lines of comments as you want to make a  comment block you can just type a  forward slash in asterix and now you’ll  see after i typed this in everything  down here  changed color everything basically  became a comment that is until i make  another asterisk and another forward  slash and so now only things that are in  between this starting and this ending  tag are going to be considered comments  but you can see like i can write on as  many multiple lines as i want like this  now this whole thing in between these  comment blocks is going to be considered  a comment so that’s kind of the basics  of working with comments and really a  comment is extremely open-ended i mean  it’s just anything that’s not going to  get rendered by the computer so any text  that you want to put in there any notes  any you know you can write logs in there  i mean you can do whatever you want with  a comment it is completely open-ended  but i do want to show you guys one thing  that a lot of developers will use  comments for and it’s actually to do  what’s called commenting out a line of  code so a lot of times when you’re  writing your php programs you might have  a line of code which you think is kind  of causing trouble so maybe you have a  line and you think that line is breaking  your program or something so a lot of  times you’re going to want to try to  test your programs without those  specific lines of code and let’s say  that like this line of code down here  like i think maybe it’s causing problems  in my program well one thing i could do  is i could just delete it and then i  could come over here and run my program  and  that line is no longer getting executed  right  but here’s the problem with that is i  have to actually like physically delete  the line of code another thing you can  do instead of having to delete the line  of code is just comment this out so i  could actually just put a comment in  front of this and now this whole thing  is a comment and so instead of having to  delete the line of code we’re going to  get the same result where this line of  code doesn’t get executed but without  having to delete it so this line of code  is no longer going to get rendered by  php because it’s technically a comment  but i don’t have to actually physically  delete it from the file so that’s what  we would call commenting out a line of  code and that can be really useful but  like i said i mean comments are very  open-ended i mean you can do whatever  you want with a comment and you can  really just use them to help yourself  out  in this tutorial i want to talk to you  guys about using the include statement  in php the include statement basically  allows us to include another file inside  of our php file so i could set up like  another php file or an html file and  then i could use this include keyword in  order to essentially just use all the  code from that other file in my current  file and this is a huge topic and it’s  extremely useful in php and in this  tutorial i’m going to show you guys just  a very basic use case essentially we’re  going to define a header and a footer  for our website and then we’re actually  going to be able to include that header  and footer  inside of our php file so i’m going to  show you guys how we can do this now  down here i just have a very simple file  set up it’s just my html file and let’s  say that when i’m creating my website i  want all the pages on my website to have  the same header and have the same footer  so imagine that i had like 100 pages on  my website and i wanted all of them to  have like i said the same header and the  same footer and if i wanted to be able  to change that header and footer like i  wouldn’t want to have to go and change  it on a hundred different places what we  can actually do in php is we could write  a html file for the header of our  website we could write an html file for  the footer of our website and then using  php we could include the contents of  those files into each one of our web  pages so i’m going to show you guys how  we can do this and it’s actually pretty  useful  over here in my little file explorer i  actually created two files i created  this footer.html file in this  header.html file now these are like the  world’s simplest html files um the  header is basically just a header one  and then we have a horizontal rule it  says mike’s website and the footer is  again just a horizontal rule and then it  says thanks for visiting so you know  obviously in your own website you can  make the header and the footer as  complex as your heart desires for the  purposes of this tutorial though i just  created some simple headers and footers  so let’s say that those were going to be  the headers and the footers for every  page in my website right so every single  content page that i created on my  website i wanted that to be the header  and the footer well what i could do is i  could actually come down here in my php  and i could do something called  including those files and essentially  what this will do is it’ll go out grab  all the code from those files and place  it here into this php file and inside  these php tags i can just say include  and then inside of quotation marks i  basically just want to type in the name  of the file so i’m just going to go  ahead and type in  header.html so all i had to do was just  say include header.html and now all the  sudden when i refresh my browser over  here  you guys will see that that header is  actually going to show up in my website  so now without having to type out any of  the code for the header without having  to do anything all i had to do was just  say include header.html  and now i have all of the code for the  header of my website right here  another thing i could do is include the  footer so again i can just kind of copy  this guy and we’ll come down here  and now i’m going to include footer.html  and you’ll see over here we should get  that footer on the website yeah so now  we have the header the horizontal rule  and then the horizontal rule for the  footer and it says thanks for visiting  so what i could do now is i could come  in here and i could you know basically  create my html file so i could you know  write out some text whatever maybe i’m  writing like an article or something and  that’s going to go ahead and show up in  between the header and the footer  and this is really useful because what  you could do is you could basically just  include the header and include the  footer on every single php file that you  make so all the different pages on your  website you can include the header and  you can include the footer and then your  header and your footer will  automatically show up on all of those  files but what’s cool about this is if i  wanted to modify the header or the  footer all i have to do is come over  here into this file and modify it so  instead of saying mike’s website we  could say like mike’s cool website right  so i updated the header i made a simple  change  and i actually don’t have to change any  of the code inside of this site.php file  i don’t have to touch it and the header  is automatically going to update when i  refresh my page so you see over here now  it says mike’s cool website but i didn’t  have to modify any of the code in here  and the point is that if you are  including the header on like 100 or 200  pages in your website and you wanted to  change it you only have to change it in  one spot and it will automatically  update on all of those other pages in  your website and that’s why this is so  powerful so what a lot of people will do  is they’ll break up their website into  little reusable components so they’ll  you know place the header of their  website in its own file the footer of  their website in its own file maybe you  have like a navigation list or like  breadcrumbs or something you know  essentially you can place any of those  things inside of their own files and  then you’ll be able to use all of those  different html components inside of your  websites so these includes are extremely  useful and there’s a lot of  circumstances where you guys are going  to want to do this and really what this  does is it makes your website more  modular so you can break your website up  into these little you know components  and then you can just insert them into  your different pages using those include  statements but really this is just  scratching the surface of what these  includes can do and so in the following  tutorial i’m going to talk to you guys  some more about what these includes can  do and more specifically we’re going to  talk about not just including html files  but also including php files  in this tutorial i’m going to show you  guys some more things we can do using  that include statement in php so the  include statement is really awesome  because it basically allows us to go out  to another file and grab all the  information in that file and include it  in our own file so in the last tutorial  i showed you guys how we could use  include in order to go out and grab  html from all these separate files and  we can bring it all together and sort of  like scaffold out  our website and in this tutorial i’m  going to show you guys how we can take  that a step further and actually include  other php files inside of our php file  and what you’ll see is when we include  other php files things start to get  really awesome the first thing i’m going  to do is show you guys how we can create  a php file and then i’m going to show  you how we can actually include it here  in our php and i’ll show you some cool  stuff that we can do with it so over  here i’m actually going to create a new  php file so i just have this site.php  file and i’m going to come up here and  we’ll just make a new file and i’m going  to go ahead and call this  articleheader.p  so basically this is going to be a file  which is going to act as like the header  for an article so let’s say that we were  writing a blog or something and every  blog post was going to have like a  specific header and let’s say that we  want all of the headers on our website  to look the same so i want all the blog  headers on the website to sort of have  the same look and feel this is the file  where we can kind of define that look  and feel so what i want to do is i’m  basically going to design this article  header so let’s say that every article  on our blog website has like a title an  author and a word count and i’m actually  going to show you guys how we can use  variables inside of this php file and  then we can actually give those  variables values in another php file so  just stick with me for a second and this  is gonna make sense um so this is gonna  be our article header and i’m actually  just gonna make a header two  and what i’m gonna do in here is i’m  gonna make some php tags so i’m just  gonna say less than sign question mark  php and then question mark greater than  sign  and in here i’m just gonna print out the  title  inside of this header two i’m printing  out the value of this title variable but  you’ll notice that i didn’t actually  give this a value and that’s actually  important and you’ll see later where we  can actually give this a value so in  addition to the header two i’m also  going to create a header for for the  author so in here again we’re gonna put  some php tags  and i’m gonna throw these here in the  header four and this time instead of  printing out the title we’re gonna print  out the author and then finally down  here we’re gonna print out the word  count so i’m just gonna say word count  colon and again i’m gonna put these php  tags in here and we’re gonna echo out  the word count  so you’ll notice that this is all this  file is right i have a header two i have  a header four and then i just have this  like word count thing down here so i i’m  not actually putting any information in  here i’m just printing out the values of  variables but i didn’t give any of these  variables values yet and i’ll show you  what we can do is we can actually  include this article header file into  another php file and inside of that  other php file we can give these  variables values so down in this  site.php file i’m going to come down  here into my php tags i’m just going to  say  include and i want to include that file  so it’s article  header.php so when i include this file  you’ll see over here when i refresh my  page  that we get this little skeleton here so  if i actually um viewed the page source  you’ll see over here in the page source  we have  this header two we have this header for  and we have word count so we actually  got all of that information from that  other file but these things are all  empty so they don’t have any values so  what we can do is inside of this  site.php file where we included this  article header we can actually give  these variables some values so up here  above i’m going to give the title  variable a value i’m going to give the  article variable a value and i’m going  to give the word count variable a value  so i could say title and we could just  say this is  my first post so this is like my blog  post title and then down here  the author is going to be me it’s going  to be mike  and then we can also do word count so  over here we’ll say word count is equal  to let’s say 400.  so  i’m actually assigning these variables  values in this php file then i’m  including this article header now when i  refresh my page you’ll see that all of  that information gets populated so i  basically created like a little template  over here in this articleheader.php file  and i was basically like okay we’re  going to put the title in here we’re  going to put the author in here and the  word count in here but i didn’t give  those values i’m actually letting the  pages that include the article header  assign those values so on this site.php  file i could give this you know one  title one author and another word count  but if i was to create a separate php  file so if i created like two or three  or four more php files when i created  different blog posts i could give those  different titles different authors and  different word counts so even though i’m  including the same file article header  depending on the file that i’m including  it from i can give it different  information and that is extremely  powerful because we can basically define  these little templates over here and  then these templates will get populated  with the data  that for example like this site.php file  assigns so we can assign a value for  title a value for author and a value for  word count and one of the other cool  things is if i ever wanted to change the  way that article header looked and felt  like all i have to do is change it over  here so i could change this to like an  h1 instead of an h2  and i don’t have to change anything over  here in site.php  everything is still going to work  correctly but the styling will be  updated so this is essentially how you  can take these includes to the next  level like not only can you include  static html but you can also include  dynamic php and you can populate the  values in that php  inside of the php file that’s including  it so that’s one really useful way that  we can use these now i want to show you  guys one more thing we can do we can  actually include php files that have  like variables and functions inside of  them so i’m actually just going to get  rid of this stuff  and you guys will see over here i have  this file it’s called usefultools.php  if i open this up it’s a very simple  file  i have a variable over here it’s called  feet and miles and this is basically  like how many feet there are in a mile  so this might be like some value that i  want to keep track of and then i also  have this function over here called say  hi it takes a name parameter and it just  prints out hello name so this is a very  simple file but let’s say that i wanted  to use all the functionality that was in  this file so i wanted to use that say hi  function and i wanted to use that feed  in mile attribute but i wanted to use it  inside of my site.php file i could  actually come down here and i could just  say include  useful  tools.php  and now that i included this file i can  use all of that functionality and i can  use that variable so i could come over  here and i could say like say hi and i  could pass in a name  and now i’m actually going to be able to  use that say hi function so when i  refresh the page it says hello mic i  could also use that variable so i could  print out  feet in mile  and this is going to go ahead and print  that out for me so over here we’re  printing out 5280.  so even though i didn’t write this  function and i didn’t create this  variable inside of my site.php file  inside of this file over here i was  still able to use that function and that  variable because i included this php  file so a lot of times what people will  do is they’ll create a file just like  this useful tools and they’ll have a  bunch of functions in it or they’ll have  variables in there they’ll have you know  a bunch of php code  in there and this is sort of like its  own you know php file and then when they  want to use all that functionality  they’ll just include the file um inside  of their php file and then they can use  those functions or those variables to do  whatever they want so those are two  really powerful ways that you can use  this include command so over here with  the article header we basically created  this like little template and then we  let  whoever was including it decide what the  title author and the word count would be  and then over here we defined a bunch of  like we defined a function and a  variable and we were able to use that  function and that variable um just by  including this file  in this tutorial i’m going to talk to  you guys about classes and objects in  php now a lot of times in php we’re  going to be dealing with different types  of data and in php we have a certain set  of data types that we can work with so  we can represent things like strings  which are just plain text we can  represent whole numbers like integers  and decimal numbers we can also  represent true false values like  booleans but here’s the problem is a lot  of times when we’re writing our php  programs we’re not going to be able to  represent everything just using like a  single string or a single boolean or a  single number like there’s a lot of like  real world entities like in the real  world that can’t just be broken down  into a single string or a single number  like  for example like we could represent  something simple like someone’s age for  example like i could create an age  variable over here and i could set it  equal to like 90 right maybe someone’s  90 years old for something simple like  that like someone’s age we can easily  represent it with you know something  like a a number but there’s a lot of  things in the real world that can’t be  represented with just a number or a  string or a boolean and essentially the  problem is is that we only have these  you know three or four different data  types and so the types of information  that we can represent and model in our  programs is very limited so php  recognizes this problem right the  problem that we can’t represent  everything in the real world just with a  single string or number so in php we can  actually create our own custom data  types and we can do that by creating  things called classes a class is  essentially just a specification for a  custom data type so just like we have  the string data type and like an integer  data type a boolean data type i could  create my own custom data type that  would allow me to model something in the  real world right so a string for example  represents like plain text a number  represents a number but i could create a  custom data type to represent something  like a phone or i could represent like a  keyboard or i could represent a water  bottle like i could represent anything i  could think of in the real world using a  class because i’m able to create my own  custom data type so in this tutorial we  are going to create our own class which  like i said is a custom data type and  then we’re gonna use that inside of our  program so let’s say for the purposes of  this tutorial that we’re writing a piece  of software for a library right so let’s  say that we want to you know write some  software that will help a library manage  all of its books  so what we’re actually going to do is  we’re going to create a book class  and this book class will basically allow  us to represent and model books inside  of our php program so i’m going to show  you guys how we can create a class i’m  just going to come down here in my php  tags and i’m just going to type class  and then a space and now we want to type  in the name of the class that we want to  create in my case i’m going to create a  book class because i want to be able to  model a book inside of my program and so  i’m just going to type book like that  and a lot of times when we’re creating  classes people will use a capital letter  it’s not necessary but that’s just kind  of like a pretty common convention then  i’m going to type in open and close  curly bracket and i’m just going to  enter a few times so inside of this open  and close curly bracket we can start  writing our custom data type we can  start creating our class now whenever we  create classes in php what we’re  actually going to do is we’re going to  take this book and we’re going to break  it up into a series of attributes so  remember by default in php we have like  strings we have numbers and we have  booleans so we can represent all of that  information by default but when we  create a new data type generally what  we’re going to do is create the new data  type based off of those other data types  so for example with this book class what  i want to do is define a series of  attributes that represent a book so what  i can do is i can actually start  thinking of what different attributes  are going to make up a book let’s try to  think of some i think a good set of  attributes would be a title an author  and a number of pages so every book has  a title every book has an author and  every book has a number of pages so  inside of this book class i can  basically say that every book should  have a title an author and a number of  pages and i can do that by defining  attributes so the way i can create  attributes is i can just come in here  and i can say var there  and then i can make a dollar sign and  i’m going to type out the name of the  attribute so like i said a book is gonna  have a title and i’m gonna type a  semicolon  a book is also going to have an author  and  a book is also gonna have a number of  pages so i’m just gonna type pages so  essentially what i’m doing here is i’m  telling php that i want to create a new  book data type and this book data type  is going to be composed of a title an  author and pages so every book inside of  our program will have a title and author  and pages so essentially this class is a  specification so it’s like a blueprint  for creating a book inside of my program  what i can do now is i can come down  here below this class declaration and i  can actually create a book inside of my  program so i could model a real book in  my php program and we can create a book  in our program much like we would create  a variable in our program so i can make  this dollar sign and i could just call  this like book one for example and i’m  going to set this equal to new  book just like that  and essentially what this is doing is  it’s creating a new book data type and  it’s storing it inside of this book  variable so up here we created this book  class and this is basically just a  blueprint it’s a template for what a  book is inside of our program and down  here i created an actual book and this  is what we call an object so an object  is an instance of a class like i said a  class is a blueprint for our book right  the book class is basically just  defining what a book is and down here  i’m creating an actual book and so  because this is an actual book i can  give it its own title its own author and  its own number of pages i could come  down here and i could say book one and i  can make a dash and a greater than sign  and i can say title and i’m just going  to set this equal to something so i  could say the title of this book is  going to be  harry potter so let’s say that this is a  harry potter book  and i could do this for each of the  attributes so i could also say  book one  author and the author of the harry  potter books is  jk rowling  and then finally we can say book one  and  pages and we can give this a number of  pages so let’s say it has 400 pages so  essentially what i did is i created a  book in my program and i gave that book  a title i gave that book an author and i  gave that book a number of pages so what  i could do is i could actually come down  here and i could print some of this  information out so i could echo out like  book one  title and now this is gonna print out  the title of book one so we should get  harry potter and you’ll see over here  we’re printing out harry potter i could  do the same thing for the other  attributes so like the author now we’re  going to be printing out the author of  book one which is jk rowling so before  in my program before i created that book  class i had no way of representing a  book right i had no way of like storing  or representing or modeling a book  inside of my program but now since i  created this book class i basically  created a template for what a book is i  created a book data type and now i can  use that book data type to create  variables so now this book one variable  is actually storing a book  object and remember an object is just an  instance of a book  so we created this harry potter book and  actually why don’t we come down here and  we’ll create another book so i’m  actually just going to copy this whole  thing and we’ll paste this down here so  in addition to creating a harry potter  book let’s say we wanted to create a  lord of the rings book i’m actually  gonna change this to book two so instead  of being called book one i’m changing  this to book two and i can do the same  thing i can say new book and now over  here we’re gonna change this so this is  gonna be  a lord of the rings book and the author  is  tolkien  and let’s say the lord of the rings has  like 700 pages  so essentially what i’m doing now is i’m  creating a new book and this time it’s a  lord of the rings book the author is  tolkien and it has 700 pages so here i  have book one this is a harry potter  book the author’s jk rowling down here i  have book two and this is the lord of  the rings book so i could do the same  thing for book two i could print out  like  book two and the author of book two is  gonna be  tolkien so now we’re printing out  tolkien  so just like i can create two strings  like i could come down here in my  program and i could create like string  one and this would be like whatever and  i could create another string like  string two  and this is going to be whatever  just like i can create two strings in my  program i can also create two books so  we basically created a new  data type and it’s important to know the  difference so up here we have a class a  class is basically just a blueprint it’s  a specification for what a book is in  our program we’re basically defining the  new data type down here we’re creating  what are called objects an object is an  instance of a class so an object is an  actual book so we have a book object and  it has the title harry potter the author  jk rowling and 400 pages down here we  have another book object with the title  lord of the rings tolkien and 700. so  these are both books but they have  different titles they have different  authors and they have different pages  and that is the beauty of classes and  objects is we can take something complex  like a book and we can represent it  inside of our programs  in this tutorial i’m going to talk to  you guys about constructors in php a  constructor is basically just a special  function that we can put inside of a  class which is going to get called when  we create an object of that class and we  can actually use these constructors to  do a bunch of cool stuff and in this  tutorial i’m going to just give you an  introduction into constructors and then  we’re actually going to be able to use  constructors to give our objects some  default information so it’ll make it a  lot easier for us to create objects in  php  now if you’re following along with this  course in the last tutorial i talked to  you guys about classes and objects and  we created this book class over here and  we basically said that the book is gonna  have a title an author and a number of  pages and then down here we created two  book objects so we have book one and the  title was harry potter and this was like  jk rowling 400 pages and then we created  another book object down here and it was  the lord of the rings book and the  author was tolkien and it had 700 pages  so we created our two book objects and  we created our book class and everything  went pretty well so i want to show you  guys another thing we can do inside of  this class which is use something called  a constructor and a constructor like i  said it’s basically a function that’s  going to get called whenever we create  an object of the class so down here i’m  going to create a constructor and we’ll  kind of see what they do so basically  all we want to do is just say function  and i’m going to type two underscores  just like that so  one two underscores and then  construct just like that and it needs to  be named construct if you don’t name it  exactly like that then this isn’t going  to work so then over here we’re going to  make an open and close uh curly bracket  so so far this is just a normal function  right it looks exactly like any other  functions that we’ve used throughout  this course um and what i want to show  you guys is that whenever we create a  new book object like we did down here  this function gets executed so i’m going  to go ahead and just type out echo and  i’ll just say new book  created  so now actually i’m going to type a  break here too just so we can see this a  little bit better  so now when i go over here and i run  this program on the browser you’ll see  we’re printing out new book created two  times and actually what’s happening is  we create these two books so down here i  say new book  and i also say new book down here and  whenever we say this like i said this  constructor function gets executed so  then over here the first time we create  the book it says this and then we create  another book and it prints this out  i want to illustrate this a little bit  further just to really kind of like  instill what’s going on so this works  just like any normal function so just  like any other function i could pass it  some information so i could say like  this function is going to take a  parameter called name and then let’s say  down here i just want to print out the  name that gets passed in so what i could  do now down here when i create these new  books i can pass in a name so i can pass  in like mike and i could pass in tom and  i’ll show you guys when we run this  program now it’s going to print out mic  and then it’s going to print out tom  because those are the two values that  are getting passed into the constructor  so we’re printing out mike and then  we’re printing out tom just like that so  hopefully you guys see the relationship  now whenever i say new book down here  when i say this it’s actually calling  that constructor function so that is  extremely important and this is actually  really useful so we can use this to do  all sorts of stuff in our classes and i  just want to point one thing out so down  here when we created these books right i  created book one and i created book two  but you’ll notice how many lines of code  it took us to do this so just to create  one book up here it took me four full  lines of code right i had to actually  like create the object and then i gave a  value to the title gave a value to the  author and gave a value to the pages  right so i had to individually say like  book one title is equal to harry potter  book one author is equal to jk rowling  like it was a lot of work for me to  create this book object and i had to do  it twice this was only creating two  books imagine if we had to create like  20 or 30 of these different books in our  program  the amount of lines of code it would  take it would be ridiculous like this  alone has taken up eight lines of code  so i want to show you guys how we can  use that constructor function that we  just looked at in order to make it a lot  easier for us to create these objects so  remember we can pass information into  this constructor right i specified that  this would take some parameters and then  we passed parameters in it down here and  everything worked  what we can actually do is we can pass  in the book’s title the book’s author  and the number of pages of the book  into this constructor function and then  inside this constructor function we can  give those values to the title the  author and the pages so essentially we  can do everything we did down here  but inside of this constructor so let me  show you guys how that works over here  in the constructor i’m going to specify  that i want to take in three parameters  i’m going to take in the first parameter  i’m just going to call it a title  and the second parameter i’m going to  call it a author and the third parameter  i’m going to call it a pages  and you’ll notice i’m putting this  little lowercase a in front of title  author and pages you don’t have to do  that i’m just doing that because it’s  going to be easier for us to see what’s  going on if these have  these are named like this but you’ll see  in a second we can name those whatever  we want so now that we specify that this  needs to take some parameters what we  can actually do is we can take the  values that the user passes in and we  can assign them to the values for the  object so i can basically do exactly  what i did down here but just up here in  this constructor  so what i want to do is i want to say  dollar sign this and this is actually a  keyword in php and it’s going to refer  to the current object and i’ll kind of  explain this a little bit more in a  second but i’m going to say this title  is equal to  a title  so i’m setting the title of the current  object equal to  the value that got passed in and i’m  going to do this for all of these so  i’ll just say this  author is equal to a  author  and then finally we’ll say  this  pages is equal to  a pages all right so we basically said  this title is equal to the title that  got passed in this author is equal to  the author that got passed in and this  page is equal to the pages that got  passed it now i want you guys to notice  some similarities i’m saying this  arrow title and then down here i’m  saying book one arrow title and actually  these are doing the same thing so when i  use this keyword it’s basically just  referring to the current object that’s  getting created right so down here i  create the object and then i manually  give it a title and author and a number  of pages over here when the object gets  created and this construct function gets  called i’m doing the same thing but i’m  just doing it over here  and i can use this keyword in order to  do the same thing that i did down here  so instead of saying book one i just say  this and like i said this keyword is  just a keyword that we can use inside of  this constructor function which  essentially just means that it’s the  title of the object that’s getting  created hopefully that makes sense all  right so what i can do now is whenever i  create a new book i can pass in all of  this information so i can pass in like  harry potter and i can pass in the  author rowling and i could pass in the  pages like 400  and now i can get rid of all of this  because we’re doing all that stuff  inside of the constructor and then i’m  gonna do the same thing for lord of the  rings real quick  all right so i put all this stuff in  there for lord of the rings and again  i’m gonna get rid of this so now instead  of it taking eight lines of code for us  to create these objects it only takes  two lines of code and i’m calling this  constructor and i’m passing in the title  that i want to give to the object the  author that i want to give to the object  and the number of pages i want to give  to the object and then up here in the  constructor we’re assigning the value of  title to the title that got passed in  the value of author to the author they  got passed in etc so basically now we  did exactly what we did before but we  just saved ourselves a bunch of time and  a bunch of codes all right so i’m going  to prove this to you guys i’m going to  come down here and why don’t we just  echo out like  book one  title so we’re going to echo out the  title of book one and we should get  harry potter so we do  so this is working exactly like it  worked before it’s just way easier for  us to create these  objects and the thing that’s cool about  this is i could still modify these  values so if i wanted to come over here  i could still say like book one title is  equal to and i could give this a  different value so we can make this like  hunger games or something and now the  book one’s title is going to be updated  so instead of being harry potter it’s  going to be hunger games the whole point  of using this constructor is that we can  give this information right up front so  i don’t have to manually set it i can  just do it right away and the object has  some initial information  so that is how constructors can be used  and constructors are extremely useful a  lot of times when people create classes  they’ll create constructors for those  classes  in this tutorial i’m going to talk to  you guys about object functions in php  an object function is basically just a  function that we can define inside of a  class and then the different objects of  that class can use that function so  these are super useful and a lot of  times we’re going to want to create a  bunch of these little object functions  that will either tell us information  about the current object or modify the  current object or do something like that  so i’m going to explain exactly what  these are exactly what they do and how  we can use them  so over here i created a  student class so this is basically a  class that i created and it’s  essentially just modeling a student in  my program and i’m basically saying that  every student has a name a major and a  gpa so it’s pretty simple um and then  down here i have my constructor function  so i’m able to pass in the name the  major and the gpa  and then down here i assign the name  that gets passed in equal to the name of  the actual object so then here down  below this class i created a couple  students so i have a variable called  student1 and it’s storing a student  object whose name is jim he’s a business  major and he has a 2.8 gpa and then down  here we have student two her name is pam  she’s an art major and she has a gpa of  3.6 so these are the two students that  we can work with in our program so so  far hopefully this stuff makes sense  right this is essentially what we talked  about in the last couple lessons but in  this tutorial i want to show you guys  how we can make object functions and  like i said an object function is  basically just a function that each of  these objects could use in order to like  do specific tasks or find out different  information so what i want to do is i  want to write a function which is going  to tell me whether or not the current  student is on the honor roll so let’s  say that we’re writing a program for  like a college or a university  and we want to be able to easily figure  out whether or not a particular student  is on the honor roll right and let’s say  that the rules for the honor roll is  like you need a 3.5 or above what i  could do is i could actually create an  object function up here inside of this  class  and that function will be able to tell  me whether or not the particular student  has honors so let’s go ahead and do that  and hopefully as we go through you’ll  kind of see what we’re doing  so down here below this constructor i’m  going to create a function and i can  create this just like i would create any  other function i’m just going to say  function and then i’m going to give it a  name so i’m basically going to call this  has  honors and this function is going to  return a true or a false value so if the  student has honors then it’s going to  return true if the student doesn’t have  honors then it’s going to return false  so i’m actually going to go ahead and  start writing this function so like i  said the rules for honors is going to be  if you have a gpa of 3.5 or above then  we’re going to say you have honors but  if you have a gpa lower than 3.5 then  you’re not going to have honors so  you’re not gonna be on the honor roll so  what i could do is i could just create  like a simple if statement so i could  say if and i basically just want to  check to see if the object that’s  calling this function has honors or not  so what i can do is i can come up here  into these parentheses and i want to  check to see if the student’s gpa is  greater than or equal to 3.5 and the way  that i can access the current student’s  gpa is just by saying dollar sign this  hyphen  greater than sign and then the name of  the attribute so it’s going to be gpa  and i want to check to see if it’s  greater than or equal to 3.5  if it is greater than or equal to 3.5  then down here we can just return true  if it’s not though down here will just  return false so this is actually going  to return a true or a false value now  here’s the thing whenever we’re printing  out true or false values php isn’t  actually going to be able to print out  false and it’s not going to be able to  print out true either so instead of  passing booleans back here i’m actually  just going to pass a string back true  and a string back false  but just know that you most likely you’d  want to be passing back booleans for the  purposes of this tutorial though i’m  just going to pass back these strings so  it’s a little bit easier for us to see  what’s going on so now what i’m going to  do is i’m going to come down here and  you’ll see i can actually use this new  function that we created on both of  these objects so i could for example  echo out and i could say  student1  hyphen greater than sign  has honors and what this is going to do  is it’s going to go ahead and it’s going  to call that has honors function  for student one so when i come over here  and i refresh my page and actually looks  like i forgot to put a semicolon over  here sorry about that so now when i  refresh the page this should say false  and the reason it says false is because  student one has a gpa of 2.8 right jim’s  gpa is a 2.8 so it’s not 3.5 or greater  and therefore jim is going to not have  honors if i came down here then i  changed this to student two so now this  is going to be pam  pam is gonna have honors so this should  give us back true and you can see over  here we’re getting true and that’s  because pam’s gpa is 3.6 which means  it’s above 3.5 so let’s go back over to  this function and i want to explain it a  little bit more you’ll notice that i  wrote this function one time and i was  able to use it on the pam object and i  was also able to use it on the gym  object and that’s one of the cool things  about these object functions is i can  write the function one time and then all  of the instances of the student class  can use it so every student object can  use this has honors function in order to  figure out whether or not they have  honors and here’s the thing you’ll  notice that when jim called this has  honors function we used jim’s gpa in  order to figure out whether or not he  had honors when pam called the has  honors function we used pam’s gpa 3.6 in  order to figure out whether or not pam  had honors and this is actually really  significant so when i call this function  on the pam object  over here when i say this arrow gpa that  means that we’re going to use pam’s gpa  when i call the has honors function on  the gym object again this is going to go  ahead and use jim’s gpa so by saying  dollar sign this gpa this is always  going to refer to the gpa of the object  that’s calling the function and that’s  why these are so powerful so a lot of  times what people will do is they’ll  create like a bunch of these little  functions inside of their class and you  can see like this has honors function  comes in handy a lot right because this  is essentially specifying the  qualifications for honors right so if i  wanted i could make the qualifications  for honors be a 2.5 and now technically  jim’s gonna qualify for honors or i  could make it a  you know  4.5 or a 4.0 or something like that but  in addition to a has honors function i  mean i’m sure we could think of a bunch  of other functions that we might want to  use on a particular student but  hopefully that makes sense and really  what you want to do is whenever you  create a class in php you always want to  think about what are the different  functions the different object functions  that you can include inside of that  class  in this tutorial i’m going to talk to  you guys about getters and setters in  php getters and setters are basically  special functions that we can create  inside of our php classes which allow us  to control the access that people have  to the attributes of those classes so i  understand that that definition might  not make any sense but in this video i’m  going to show you exactly why getters  and setters are useful and how we can  use them in our classes so first let me  just show you guys what i have down here  i created a class it’s called movie and  this class has a title and it has a  rating so it has two attributes title  and rating and then down here i just  created a constructor so we can pass in  a title and a rating and then those  values get assigned to the object  attributes then down here uh like right  below the class i created a movie object  so it’s just called avengers  and it’s just the avengers movie and  it’s rated pg-13 so pretty simple  basically we’re just creating a movie  inside of our program so this is pretty  cool and then down here i was printing  out the avengers rating just to kind of  prove that everything works so i want to  talk to you guys about one potential  problem that we might run into with this  movie class let’s say that we’re  designing a program that is going to be  able to work with and store a bunch of  different movies right and one of the  things about a movie is that every movie  has a rating right so this movie is  pg-13 some other movies are like g r  um pg nr right there’s a bunch of these  different ratings that we can give to  these movies now here’s the thing  there’s really only like a certain  number of valid ratings for a movie  right we have like g  pg  pg13 r and then nr so nr would be like  not rated right i mean for the most part  these are all of the like valid ratings  that we can give to a movie maybe  there’s some other ones but let’s just  say that these are the only valid  ratings that we could give to a movie  well here’s the problem with this movie  class is that over here i could actually  give this a invalid rating so i could  come over here and i could give this a  rating of like a dog right some random  word and now when we print out the  rating you’ll see we’re printing out dog  so i was able to give this movie a  rating that wasn’t one of the like valid  ratings and if you’re just creating like  a simple movie class you might not care  about that but if you’re designing like  a real application that’s going to be  able to store and work with movies you  might want to limit the ratings that  these movies can have so you might not  want this movie to be to have a rating  of dog right you might want it to only  be able to be one of these valid ratings  over here and often to this point we  haven’t really talked about how we can  control what values can be stored inside  of these object attributes but we’re  going to talk about that right now so  i’m basically going to show you guys how  we can make it so the user can’t give  the rating a value other than gp gpg 13  r or nr we’re basically going to tighten  down this class and make it so the  rating can only have a certain number of  values so in order to do this we’re  actually going to have to do a couple  different things so i’m going to change  this back to pg13 the first thing that  we’re gonna have to do is we’re gonna  have to modify this rating attribute so  up here in my program you’ll notice i’m  creating my rating attribute i’m just  saying public dollar sign rating so  right now down here in my program i’m  able to access the rating right i can  print it out i can modify it if i wanted  to i could basically do whatever i  wanted to it but the first step in  controlling what ratings can get put in  here is going to be to cut off access to  this rating attribute so i basically  want to make it so i can’t print out the  rating attribute so i can’t modify the  rating attribute so i can’t do anything  to it so for example like right now i  could come over here and say avengers  rating is equal to dog there’s nothing  stopping me from doing this and over  here it’s the same thing it’s dog right  what i want to do is make it so i can no  longer do that and what we can do is we  can come up here and we can make this a  private attribute so this brings me to  the first point that i want to talk to  you guys about in this tutorial which  are called visibility modifiers and a  visibility modifier is basically a  keyword that’s going to tell php what  code is able to access and use different  attributes in our programs now you’ll  notice over here that i have this public  keyword right here and what this public  keyword means is that this attribute is  visible to any other code in my php  program so when i say public over here  it means i can come down here i can give  this rating a value and i can print out  that value onto the screen  public is essentially just like it’s  open to everybody so everybody can  access it everybody can modify it  everybody can do whatever they want to  it previously in this course we were  using this keyword ver  and i intentionally use this keyword  there just because i didn’t want to talk  about these visibility modifiers until  this tutorial but vare basically does  the same thing so vair and public  for the most part are kind of  interchangeable generally though in like  modern day php you’re going to be seeing  people use this public keyword again i  wanted to introduce that in this  tutorial so that’s why we were using ver  in the previous tutorials but whenever i  say public and then specify the  attribute inside of a class it basically  means that this attribute is open to the  world it’s open to everybody  but what we want to do with this rating  attribute is we want to tighten it up so  we want to make it so that nobody can  access it outside of this movie class  what i could do is i could come over  here and i could give this a different  visibility modifier called private so i  could say  private rating and basically what this  means is that any code outside of this  movie class now so in other words any  code outside of these open and closed  curly brackets  isn’t going to be able to access the  rating directly so  down here we’re not going to be able to  set the rating like this and we’re not  going to be able to print it out so if i  came over here and i refreshed my  program you’ll see we’re getting this  fatal error it says  cannot access private property movie  rating basically it’s telling us like  hey that movie’s rating is private so  you can’t access it anymore and like i  said when we set something as private it  means that only code  inside of the class that it’s declared  is able to use it so for example down  here we’re able to set the rating just  fine right we have no problems doing  that it’s because this code is inside  the movie class but any code that’s  outside the movie class isn’t going to  be able to mess with this rating  attribute that’s the first step in order  to make it so we can only set specific  ratings on this movie the second set is  going to be creating something called a  getter and a setter and getters and  setters are special functions that we  can write inside of our classes which  will allow the user to  set the attribute so it’ll allow the  user to essentially do something like  this and it’ll allow the user to get the  attribute essentially do something like  this so because rating is private i  can’t set it directly anymore and i  can’t get it directly anymore down here  to print it out so what we can do is we  can actually come up here and we can  create some functions so i’m going to  come up here and create a function  and i’m just going to call this get  rating and what this is going to do is  it’s basically just going to  return  this  rating  so whenever somebody calls this get  rating function it’s basically going to  give them the rating and so down here  i’m actually just going to get rid of  this line of code  if i wanted to get the rating and print  it out instead of saying avengers.rating  remember this is throwing an error we  can’t do this  what i can do though is i can say  avengers aero  get rating and now i should be able to  get the rating even though the rating is  private so you can see now we’re able to  get pg-13 so i can get the rating using  this get rating function i can also  create a set rating function so i can  come down here say function and i can  just call this set rating  and this set rating function is actually  going to take one parameter it’s going  to take one argument up here i’m just  going to call this  rating so we’re going to allow the user  to pass in a rating into here and then  down here what i can do is i can say  this  rating is equal to  rating so i’m setting the rating for  this object equal to the rating that got  passed in and basically this will allow  me to update the rating so with this set  rating function i could come down here  and i could say like  avengers  set rating and i could technically give  this any rating that i wanted so if i  wanted i could give this a dog  right  and basically what this is doing is it’s  allowing us to set the rating and get  the rating even though the rating itself  is private so up here rating is private  we’re not able to access it outside the  class but by using this get rating  function and this set rating function i  can both get it and set it so now that  we have these set up we can actually  start enforcing some rules so like i  said the goal is to make it so we can’t  do this so we can’t set the rating equal  to something that’s not g p g p g 13 r  or n r right that is the overall goal  of this tutorial so what i can do is  inside of this set rating function i can  actually enforce that rule so over here  instead of just saying this rating is  equal to rating i’m going to get rid of  this and instead i’m going to put an if  statement in here and i’m going to check  to see if the rating that they passed in  is one of the valid ratings that we  specified down here and if it is then  i’ll let them set the rating but if it’s  not then we can do something else so i’m  basically going to create an if  statement  and inside of this if statement  parentheses i basically want to check to  see  like i said if the rating that they  passed in is one of the valid ratings  now there’s a couple of different ways  that we could check this i’m just going  to go ahead and use a bunch of or  statements so i’m basically going to say  if rating is equal to  g  or  rating is equal to pg etc so i’m going  to go off and create one of these for  each one of those ratings and then we’ll  come back and see how it looks so i  actually went ahead and i created this  like super long if statement and  honestly there’s easier ways that we  could do something like this  but just for simplicity’s sake i wanted  to do it this way so down here i said if  the rating is equal to g or  the rating is equal to pg  or the rating is equal to pg-13 or the  rating is equal to r or the rating is  equal to nr so if it’s equal to one of  these ratings in other words if it’s a  valid rating then down here we can  basically just say  this  rating is equal to  rating  and the reason i’m doing this is because  if it passes this if statement condition  that means it’s one of the valid ratings  so we can just go ahead and set it right  that means that they passed in a valid  rating and we’re good to go otherwise  though i’m going to create an else block  over here  if they didn’t enter in a valid rating  why don’t we just say this  rating is equal to nr so why don’t we  just pass it in nr so let’s say if they  try to enter in a rating of like dog or  some other nonsense then we’re basically  just going to say the movie’s not rated  because they didn’t give us a valid  rating all right so the whole point is  that now if i want to set the rating to  something like dog it has to go through  this set rating function and when it  goes through that set rating function we  have this if statement which is going to  filter out all of the invalid ratings  and set them equal to nr so now  essentially what’s happening is when i  run this program even though i passed in  dog over here  now when i print out the rating it’s  gonna be n r and that’s because i passed  in an invalid rating it didn’t make it  through that if statement and it just  got set to n r but if i set a valid  rating like if i set this equal to r  now it’s going to be fine with that so  it’s basically just going to be able to  print out r  because hey that’s a valid rating right  all right so that’s basically how that  setter is going to work and there’s  actually one more thing that we have to  do so there’s one problem with this  program the way it is and it’s this  constructor so the way that we have the  program written i could come over here  and i could still set this equal to dog  by passing it into the constructor and  you’ll see over here it’s still able to  be set to dog so  the last thing that we can do is just  come up here to this constructor and  instead of saying this rating is equal  to rating we can just say  this  set rating and we can pass in  rating into here so now every place  where this rating gets set is going  through this set rating so now we’re not  going to be able to get away with  putting dog inside of the constructor  now it’s going to be set to not rated  like that  so this attribute is officially locked  down we officially cannot give a movie  an invalid rating it’s impossible for me  to do that i cannot give a movie a  rating other than nrr pg 13 pg or g even  if i try to like even if i come over  here and pass it into the constructor or  i come down here  and you know i say like  avengers set rating and i you know pass  in some nonsense string that’s not gonna  work right and the way that i was able  to do that was i made this attribute  private so i set rating equal to private  and i created a getter function and i  created a setter function the getter  function basically just returns the  rating and the setter function has this  if statement which is going to filter  out any bad ratings for us and just set  them equal to nr and then finally up  here in the constructor i called that  set rating function so that is basically  how we can use getters and setters and  one important takeaway from this  tutorial was public and private again  before in the tutorial i was just using  this ver keyword and again i was just  using that because i wanted to explain  public and private in this tutorial and  i didn’t want you guys to get confused  but for the most part people are only  going to be using public and private you  won’t see people using ver too much  although you can use it but remember  public means that the attribute is open  to anybody any program anywhere private  though means that only code inside the  movie class is going to be able to  access this attribute  in this tutorial i’m going to talk to  you guys about inheritance in php now  inheritance is basically where a class  can inherit  all of the functionality all the  attributes from another class in php and  this is actually a really useful  situation and the best way to kind of  wrap your head around inheritance is  just to see an example so i’m going to  show you guys an example of what this is  how it works and then hopefully you can  understand like basically what’s going  on  all right so down here in my program  i’ve created a class called chef and  this is basically just like modeling a  chef in my program  and this is a very simple class the chef  can do three things the chef can make  chicken so we have this make chicken  function the chef can make salad we have  this make salad function and the chef  can also make a special dish and  basically all these functions do is like  when the chef makes chicken it just  prints out hey the chef’s making chicken  or when the chef makes a salad it prints  out the chef makes salad and for the  special dish it’s barbecue ribs so down  here outside of the class i created a  chef and i told the chef to make some  chicken so now over here in my web  browser you see when i refresh the page  it just says the chef makes chicken so  this is a you know like i said pretty  basic program we have our class it has a  bunch of functions in it we created an  object and we told the object to make  chicken but let’s say that i’m looking  at my program and i’m thinking to myself  hmm i want another chef in my program so  in addition to having this chef class  over here let’s say that i wanted to  create another chef alongside of it so  let’s say in addition to just having  like a generic chef up here i also  wanted to have an italian chef so like a  more specialized type of chef well i  could just create another class so i can  just come over here and say class and  why don’t we call this italian chef  and again i’m going to do the same thing  i’m going to make an open and closed  curly bracket and down here in the  italian chef class i could you know  write some functions or some attributes  or whatever but let’s say that in our  program we wanted the italian chef to be  able to do everything that the normal  chef could do plus a bunch of other  stuff right so just like this normal  chef  the italian chef can also make chicken  make salad and make a special dish so  the italian chef can do everything that  the normal chef can do and let’s say the  italian chef can also do some other  stuff as well well this is a situation  where we can use something called  inheritance an inheritance is basically  where i could have my italian chef  inherit all of the functionality from  the normal chef so i could just come  over here and say italian chef  extends  chef and basically what this means now  is the italian chef is going to be able  to use all the same functions as the  normal chef i could actually come down  here and in addition to just creating a  normal chef i’m also going to create an  italian chef so i’m just going to paste  this and i’m going to change this to  italian chef  all right so in addition to this normal  chef up here i also created an italian  chef it’s a new italian chef and i’m  telling this italian chef to make  chicken now here’s the thing up here in  my italian chef class i didn’t actually  define a make chicken method like  nowhere in here does it say  make chicken right it’s just not in here  but what i did was i extended all the  functionality from the chef class and  the chef class over here has a make  chicken function so because the chef has  a make chicken function  and i’m inheriting all the functionality  from the chef class my italian chef is  gonna be able to make chicken no problem  so now when i run my program you’ll see  it says the chef makes chicken and this  is that normal chef and down here the  italian chef is also making chicken so  without having to write out the make  chicken function down here i was still  able to use it because of inheritance  and that’s why inheritance is so cool  but here’s the thing like  this italian chef if it only has the  functionality of the chef class there’s  no purpose for it so over here we could  actually create another function i’m  going to call this make pasta so let’s  say in addition to making chicken salad  and special dish the italian chef can  also make a mean bowl of pasta so down  here we could just do basically what we  did before so i’m just going to echo out  like  so  now down here the italian chef could  make  pasta but here’s the thing this normal  chef isn’t going to be able to make  pasta so i couldn’t come over here and  say normal chef make pasta this is  actually going to throw an error and  that’s because the chef doesn’t have a  make pasta method only the italian chef  is capable of making pasta because only  the italian chef has that function so  over here now when i refresh the page  you can see the italian chef is making  pasta anytime we have two classes like  the chef and the italian chef where the  italian chef can do everything the chef  can do plus some other stuff we want to  use inheritance and this can be  extremely powerful i want to show you  guys one more thing that we can do with  this though and it’s called overriding a  function so down here i’m gonna have  both of these guys print out  make  a special dish so both chefs are gonna  go ahead and make a special dish  and you’ll see that both of them are  going to make barbecue ribs so  the special dish is barbecue ribs and  then the italian chef also makes  barbecue ribs  but let’s say that the italian chef  doesn’t want to make barbecue ribs so  the normal chef’s special dish is  barbecue ribs that’s awesome but let’s  say that we want the italian chef to  have a different special dish well i can  actually do something called overriding  a function  basically down here i could override the  make special dish function that i  inherited from the chef class it’s  actually really easy all i have to do is  just come down here and i can just say  function  make  special dish so you’ll notice i’m using  the same name as the function that was  up here in the chef class and then over  here i can basically just put my own  thing so i could just say like echo  the chef makes  chicken parm maybe that’s the chef’s  special dish so now when i come down  here and i say  chef make special dish an italian chef  make special dish they’re both going to  be making different special dishes so  now the italian chef makes chicken parm  and the normal chef makes barbecue ribs  and that’s what we would call overriding  a function basically we’re overriding  the make special dish function that we  got from the chef when we used  inheritance and that can come in handy  all the time so that’s sort of the  basics of inheritance and really the  basics of using this extends keyword as  your php programs get more complex and  as you start using more and more complex  classes  using something like inheritance can  become very useful so hopefully all that  makes sense and hopefully now you guys  have a solid understanding of what  inheritance is 

admin

Leave a Reply

Your email address will not be published. Required fields are marked *