My name is 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 the work together perfectly. There are millions of websites and web applications
that are currently online, which use PHP and rely on PHP on the back end. 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 a 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 a 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 structure. 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 setup 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 gonna 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 gonna 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 dotnet. 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 it's going to go ahead and download this zip file. And the 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 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 an A, 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. Alright, 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 HB. So down here, I'm going to
go on my search bar. And I'm basically just going to start typing in environment. So n
vi, r o n n, 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 gonna 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 gonna 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, pa th 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 want to make sure that that folder is showing up there, as long
as it's showing up there, then we were simply successfully updated the PATH variable. Now
the last thing that we want to do is just check to make sure our PHP is working. So
I'm just going to type in PHP, hyphen, a 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, and 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, or 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 TextEdit, 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 can
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 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 gonna come down here to my web browser and I'm going to go
ahead and just do a Google search for atom, 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 OSX on Windows. So I'm just going to click this.
And it's going to go ahead and give me this Adam setup dot 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. 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 then I'm just going
to come down here and just search for the command prompt, I'm going to search C, D,
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. And 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 a 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 going to take 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, it basically just says PHP 7.1 point one, one 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 local host 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 Mike D. Now if you're on
OSX, 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
Mike D So this is where that died. document and root cause In other words, this is where
PHP is going to start looking for files that we want to run. So what I'm going to do is
I'm actually just going to create a new folder here. So I'm just going to click New Folder.
And I'm just going to call this folder, www. So www is just gonna stand for like our website,
you can name it whatever you want. But I'm just going to name mine 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 gonna 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 gonna 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 dot php. And you can name this, whatever
you want doesn't matter where you name it, I'm going to, I'm going to name this site,
and you need to have this dot php extension. So I'm going to click Enter, and this file
is going to go ahead and get created for us. So once we have this file open, now we can
start writing some code. And here's the thing about PHP is PHP is very tightly coupled with
a language called HTML. And HTML stands for Hypertext Markup Language, it's basically
a language that we can use to build websites. So 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 in 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 gonna 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 dot 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 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 can 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 of 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. Alright. And I'm just going to show
you guys a one simple PHP instruction that we can write. And then in the next tour 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 a go. And I'm going to make an open and close parentheses, and I'm going
to type a semi colon after this. And in here, I'm going to make an open and closed 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 users, Mike
D. And it has this address up here, localhost 4000. I'm going to go ahead and copy this.
And we're going to put this into our web browser. So I'm going to open up my web browser. In
my case, I just have Google Chrome. And over here in the address bar, I'm going to put
in localhost four 1000 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 dot 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 dot 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. 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 semi colon. And when I was
when I went over here and refresh 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 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 semi colon and that's semi colon 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 refresh 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 can 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 can come down
here, and we can 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 can 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 semi colon here.
So I'm going to put a semicolon there, and a semi colon 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'll 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 request this website over here, so for example, whenever
I like refresh this web page, 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 forward slash site dot 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 site, done
the horizontal rule, then the paragraph. And that's because that's the order that I specified
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 and
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 web page. So when the user requests site,
dot 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. 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 gonna 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 that 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 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 characters 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 gonna come over here, and we'll change this to john. And we can change this down
here to john as well. So now we've successfully updated the characters 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, I think I will also want
to change the the character's name. Well, I can basically do the same thing right, get
head down here. And everywhere. The characters age, as 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 characters 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. 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 and 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 character's age, hundreds of times, we're 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 characters 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 can create a variable and then I could store the characters
age and the characters 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 $1 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 the 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 characters name. So I'm just gonna 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 gonna say about 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 take the
dollar sign. And now I'm going to call this variable, character age. And I'm going to
set this equal to the characters 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 characters name and the characters 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
$1 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 named
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 can come down here, and I can 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 going to 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 can 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 named 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 can 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 characters 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 variables
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,
while also 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, and PHP, we're going to be working with all different types
of information. And as we go forward in the course, when 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 of 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 anytime 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 gonna put an open and close quotation
mark. So anytime 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 closed quotation marks. And
anytime 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 and PHP. There's a 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 int is basically
going to be a whole number. And so those are just like counting numbers like 12345, 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
in PHP, you can also make them negative. So I can make this a negative number. 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. 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. 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 them 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. Alright, so there's one more 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 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 in something called a Boolean. So I'm gonna 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 anytime we have a boolean variable, or anytime 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 Boolean 's are useful more later in the course. But for
now, just know that you can store a true or 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 no like this in PHP, this
basically stands for no value. And a lot of times in PHP, we're going to 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 no 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 gonna be representing information
in the form of text numbers, or Boolean, and really, with just this information, you can,
you know, essentially build any program that you want. And 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 can do the
same thing for a number. So I could just put a number down here like 4.57. And it's going
to 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 can 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.
And 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 draf 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 gonna 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 lowercase. 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 str to lower, open, 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 can
say string to upper. And now this will convert it entirely into uppercase. So you can see
now we just get draft Academy uppers. And you don't just have to pass the 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. 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 lowercase.
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 le n. 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
graph 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, I can actually come over here, and I'm going to get rid of this string length.
So 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 can actually put zero 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 in that
index position in the string. And whenever we have a string like this in PHP, we're going
to start the indexing off at zero. So I would say that this first character G is an index
position zero in the string. The second character I is an index position, one RS at two, a three
f4 excetera. 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 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 getting
at this capital M, another thing we can do is we can actually modify individual indexes
in the string. So I can actually come down here, and I can say, like phrase, and I can
say like phrase zero, is equal to, and I can 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 burapha Academy instead of draf. Academy. And that can be really useful. So you can
actually modify like individual characters inside of your string. Alright, 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 substrings 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 calling this draft Academy, what 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 closed 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 can 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 draf 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 going to 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, the binary, there's
one more of these that I want to show you guys. And it's basically going to allow us
to get like a substring. So over here, I could just type out STB, str.
And basically, this is going to allow us to just grab like a section of this overall string
over here. So I can just grab like a single word from it, or I can grab just like a couple
different characters. And this will allow us to do that. So I'm going to make an open
and close parentheses after I say substring. And now inside of here, I want to pass a couple
of 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 the substring from. So remember, I have all
of these different characters in this string have indexes. So like this is index position
0123 excetera. 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 what index Academy starts.
So we could actually just start counting at 012345678. So if I put an eight right here,
now, this is going to grab a substring from this string, starting at index position eight
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 123 characters, so it's going to grab ACA, and
then it's going to 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 string
functions that you guys can use in PHP. And to be honest with you, there's a lot more
of these. Now 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 there'll be like
a huge list of all 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 gonna show you guys the ins and outs of working with numbers in
PHP and 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 it's going to be pretty cool. Down here
in my PHP tags, I'll just sort of show you guys the bare basics. So I can basically just
like if I wanted to, I could print out a number. And whenever we're typing out 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
can make a negative number just like that, I can also make a decimal number. So I can
say like, you know, 847 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 in 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 a 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. Alright,
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 Asterix so I can go ahead and multiply like 5.7 times nine,
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 10% sine three, and we would read this
as 10 mod three. And basically what this means is, modulus operator is going to take 10,
it's going to divide it by three, and it's going to give us the remainder. So 10 divided
by three is going to be three with a remainder of one. So over here on the browser, we should
get one 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 five, times 10, what
this is going to do is it's going to multiply the numbers first. So it's going to do five
times 10 first, and then it's going to add four onto it. So we should get 54, which you
can see we do. 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 and parentheses. And now PHP is gonna 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, you're familiar
with just like normal, you know, math order of operations, and it's the same thing. 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 could 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 can
say like num, and then I can say plus plus. And plus plus is basically just going to add one on to 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 and 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 gonna end up getting a nine instead of a 10. So that can be pretty useful. Another
thing we can do is we can add a number on to this number so I could say like num and
let's say I wanted to do something like num is equal to num plus 25. Right. So basically
Oh 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 we'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, Eagles would just be the same as saying, times 25,
right, it's basically doing the same thing. It's just a shorthand. 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 exponent 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 numbers negative or positive, it's always
just going to give you like the value. So I can just say abs, I can make an open and
close parentheses, and inside of this parentheses, I can put a number so I can put like negative
100. And this is actually going to give me back the absolute value of 100. So if I was
to print this out over here, you can see we just get 100, instead of negative 100. You
can also do a bunch of other math operations. So for example, let's say we wanted to take
a number to a power, like I wanted to do like to race 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 Sq RT. 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 can actually come over here and I can say max. And when I say Max, I can
pass two numbers into this parentheses. So I could say like two separated 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
men, 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 two, because two 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 can just
say round. And in here, I could just pass a number. So I could say like, two, and this
will round it down to three. So it's going to round according to just like standard rounding
rules. If I had like 3.7. Now this will rounded up to four. There's also two other functions.
So one's called seal. And this is called the sealing function. And basically, no matter
what decimal point is over here, it's always going to round it up. So no matter what, it's
always just going to round this number up. So you'll see here we get four, even though
this is a 3.3. And I can do the same thing 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 going to round this number down.
So we're always going to 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. 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, I'm 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
gonna show you guys how we can do that in PHP. 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 will be able to pass the information that
the user enters over to our PHP programs. So the form is kind of like the middleman
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 in 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 gonna 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 gonna 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 dot php file. And
this is just the file that we've been using throughout this course. And this is the file
where I want to handle what happens with the form. So down here, I'm just going to put
the name of this page. So it's just site dot php. So we're 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,
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 gonna 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 gonna make an input. So I'm
just gonna 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 gonna say input
type is equal to text. And this is just going to give us a basic textbox. 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 a name. And this will kind of like, tell us what this textbox 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, 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 basically get submitted. So what I'm gonna do down here is I'm going to create
a button. So I'm just gonna say input. And type is going to be equal to submit. So this
Submit button is special is basically just going to submit all the information in the
text boxes up here. So when I click this Submit button is 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 setup. 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 gonna 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 I'll make $1 sign underscore, and I'm going to type out je 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 close 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 can 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 user name, if I wanted and then down
here, I could change this to user name. 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. And 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 the textbox.
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 will print out their name. So now it'll
be like a little bit more of a explanation. So you'll see down here now it says like your
name is a DSF. 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 going
to 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 of input, because it's an age, and
I can 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 a And 30. So that's sort 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 in 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 gonna
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 gonna be a pretty simple
calculator. But this should show you guys how we can get numbers from the user in PHP.
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 dot 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. And 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 result. 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 going to create two of these little inputs,
I'm just going to say input. And I'm going to 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 num one. 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 gonna 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, two. 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 boxes, is going to get submitted, and we'll 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 num one and number two. 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 num one. 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 num two 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 gonna
say dollar sign underscore get open and close square brackets. And we're going to say num
two. Essentially, what we're saying here is I want to echo out into the HTML num one plus
num two. 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 going to come
over here and I'll say, answer, and then we'll be printing out the answers. So let's go over
to our HTML or to my browser, I'll refresh the page. As see over here we have our two
text boxes, we have our Submit button. And then over here, it says the answer is 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 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 that says num
one is equal to 10. ampersand num two 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 on to the URL. So if I was to change this up here in the URL, like if I change
num, two to like 50, and then I clicked 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 one of the key concepts 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 num one is equal to 100. And now, since I said num,
one is equal to 100, up here in the URL, it's going to take the value of num one as 100.
So when I click enter, now, number one becomes 100, and 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 answers going to be. So that's sort of like how that works 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 instead of getting
taxed, 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 to see this is kind of just like a basic Madlib. 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 Madlib 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. This is
kind of like a classic poem. But I think it would be a lot better if we MADD 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 would just be like
a color. Instead of saying the 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, 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 it 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 says action site
dot 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 of 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 textbox. 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. Alright, so let's go ahead and
test this out. I'm going to go over to my web page. 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 are 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 now and 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 gonna 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 madlibs now, now obviously, the one problem
with this Mad Libs is that, 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 user has entered in all the stuff
and not before. And actually later in the course, we're going to 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, 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 Madlib 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 dot 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. And 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 notice that up here, it says,
site dot 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 can come over here, and I can 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 the
name of 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 textbox altogether. And I
can 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 want it 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 can come down here. And instead of echoing the name,
I can 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 webpage 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 stored
in the URL, 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, aq s is equal to Chrome dot, dot, whatever. So Google is doing similar things
inside of 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 us 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 can 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 form a 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. And this tutorial, I'm going to talk to you guys about two different
ways that we can get information from users. 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 have a look at what they're doing. And when you should you 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 textbox, which like is a password. So you'll see over here in this textbox, 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 this 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 can like enter in a password. And
then when I click Submit, the password will show up down here. So I just my password was
word pass. 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 doesn't really matter with certain pieces of information. But with
a password, if your password is actually showing up inside of the URL that is extremely insecure. And also I can
come up here and I can change it. So I can 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 gonna 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 of 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, 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, credit card number, I mean, any type
of like secure information, you don't want it to show up inside that URL, you want it
to be passed more securely back to PHP. And we can use post in order to do that. So like
I said, in certain circumstances gets 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 posts
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 be want to be able to store large groups
of values. So a lot of times in 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 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
in array can store more than one piece of information inside of it. So an array could
have 10, or 20, or 100, or 1000, or even a million values, a million pieces of information in side 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. But 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 $1 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 a ray and I'm
gonna 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 mark,
we do one more, Jim. So inside of this array, I have 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 I'm 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 of 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 can throw in Unlike 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 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 can do is I
can 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 you have to do in order to access
this 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 zero 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 little printout 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 an index position one, and so forth. So we're going to start 01, 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 can 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 index position 01. So I'm gonna put a one over here, and I can
just give this a new value. So I can 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
gonna work. So I'm able to print out 400. Alright, 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 could do is I could also add information on to this array. So
you'll notice over here, we have four elements in this array. And it's an index position
0123. So there's no array element at index position for, but I could actually add an
array element in if I wanted to. So I can come over here and I can say like friends
for and I could add in another friend. So now I could name this friend like Angela.
And down here I can print out friends for 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 can say like, friends for 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
going to be tons of situations where you want to use them so, so you want to 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 checkboxes in PHP. This is going to be 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're actually get information from the user will 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 forum and you can see the action is site dot php. That's
this file that I'm currently working on. And then over here to 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 checkboxes. And a checkbox 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 checkboxes. 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 going to 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 a raise. 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
checkboxes, 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 checkbox 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 pears, so this one's going to be pears.
And then once again, over here, I'm going to change this to pears. So we have a checkbox
for three different fruits. And you know, making the checkboxes 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 checkboxes 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 checkboxes 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 gonna 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 gonna leave off the square brackets, we don't need that. So down here, I now am
storing all the fruits that the user checked in 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 can 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 checkbox 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 check 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 pears now we'll be printing out pairs. So that's basically
how these checkboxes 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.
And 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, and 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 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 can actually
represent data like that. So I'm going to create this just like i'd create a normal
array, I want to make $1 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 parentheses. 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 students name. So let's say the students 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 the 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. Alright, 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. I notice I'm separating these different students
with this comma, just like I would normal array elements. So over here, we're just gonna
say, Oscar. And let's say that Oscar got a C plus on the test. All 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 gonna 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 Jim. 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 gonna say echo. This is gonna tell me what grade Jim got. So we
can see over here, Jim gotten a plus, I can 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, within
when I tried to access Jim, 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, Jim. And I could give this a value.
So I can say grades, Jim 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, 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 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 dot 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 gonna make another input. And I'm just gonna 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 textbox, we're
going to be printing, we're going to be typing in the student's name, whose grade we want
to figure out. And so what I can do is when the user submits, when they click this Submit
button, I can 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 textbox. 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 can 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
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 closed 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 gonna 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 an 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 can 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 gonna
say dollar sign, and I'm just gonna 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 Mike.
And now this value is going to get stored inside of this name variable. And it's going
to print out Hello Mike down here. So now when I run my program, you'll see it's printing
out Hello, Mike, instead of printing out Hello user, and I can 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 can come down here, and
I can just copy this and I can 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 could 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 can 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 Oscars
ad. 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, 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 in as 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 and capacity 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 number, you're basically taking it to the power of
three. So if I was to say like two raised to the power of three, that's the same as
two times two times two, right? This is me cubing a number. And what we want to do is
we're going to write a function, which will cube a number so I can pass a number into
it. And it'll go ahead and cube that number. For me, that'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. But to
make an open and close parentheses and an open and closed curly bracket. Inside of these
open and closed 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 qubit,
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. Alright, 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 for
the value of cubing, four 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 four cube,
right, four times four is 1616, times four 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, 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 out the middleman. And I could just print this out. So I'm just
gonna go ahead and put this in here. And now we're just printing out cube four. 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 functions.
Let me show you guys, if I was to come down here and say like Echo, hello, you think when
I call this cube function that it will 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 can return an array, you can 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 can
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. 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
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 as 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 going to 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 going to eat breakfast, right. So if this
condition up here is true, that I'm going to perform this action down here, I'm going
to 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 going to eat breakfast, I'm just
going to 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 its
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 going to 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 is 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'm going 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. Alright, so I'm
going to go back over here to my site dot 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. Alright, 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 F, and then I'm going to make an open
and close parentheses and an 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
gonna say is male. And 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 going to move on and we're not going to print out you are male. So let's
go ahead and run this program. And you'll see over here, it says, You are male, the value of his mail was true, but if I change
this to false, now, when I run my program, we're not going to be printing out you our
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 and for simplicity 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 mailed? 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 can actually come down here and I can print out Echo, and I can 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 is male 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 an 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 izmail, 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
want him 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 can 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 izmail 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 gonna 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 can 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 gonna 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 gonna say you are not male. So obviously,
we can change the text for that. But you guys, we'll 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 differences 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 izmail 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 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 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 F 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 f just like that. And then I'm going to make an open and close parentheses. And
I'm gonna 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 aos F with the opening curly bracket and 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
down here to the house F 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 specified. So I can 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. So I would read this else if is male and not is tall, so is
not tall. And so down here, what we can do is we can actually print out, you are a short
male, so I can say you are a short male. And so now if I set his tall equal to false, like
I did up here, this if statements 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 his male is true,
and his 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 can actually
come down here and I'm gonna make another elsif I'm just gonna say elsif, open and close
parentheses open and close curly bracket. And I'm gonna 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 is male 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, recovering the situation where they're male and short
recovering the situation where they're tall but not male. And then finally down here,
this Alice is going to cover the condition where they're not male and not tall. So I
can say you are not male, and 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 said 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 going to 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 going to 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 LSA, 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 can compare like numbers or strings, or you know,
different different types of information. And I can 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, and 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 going to do is it's going to 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'm going 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 functions already using it. So I don't want to like confuse myself. So I'm just gonna
say function get max. And then over here, I'm gonna 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 num, one and num, two. 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 num one is bigger, or if num two 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 make an 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 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 can say like if num one is greater than num, two, 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 num. One is either greater than num two, or it's not, right,
it's either true or its false. Like this comparison num, one greater than num two is either true
or its false. No one's 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 num, one is greater than num two, then down
here, I can do something. So I'm actually just going to return num one. 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 num, one back, and that'll be the end of this function. Otherwise, though,
I'm gonna say else. So if this condition isn't true, in other words, if num one isn't greater
than num, two, then I'm just going to return num two. 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 num, one is greater than num two, then I'll return num one, right? Because
if this condition is true, I know for a fact number one's the biggest. Otherwise, though,
we're going to return num two. 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 three 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, it looks like I forgot to put
a semicolon over here. That's my bad. Alright, so now when I refresh the page, we shouldn't
get an error. And you'll see we're getting 90. So it looks like this function worked.
Now I'm going to say let's try num, one 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 number one, and number two,
I also wanted to pass in a num three. 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 num one is the biggest first. So I can check to see if num one is greater
than or equal to num two. And so I'm going to use this and operator num one is greater
than or equal to num three. 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 num one is
the biggest so I can just return num one just like that. So basically, I'm just checking
to see if num one is greater than or equal to num two. And if it's greater than or equal
to num, three. But let's say that this isn't true, right. So let's say that num one isn't
the biggest we can actually come down here and I can say else if so I basically want
to check another condition. So if num one's not the biggest let's check to see if num
twos the biggest so I can just say num two greater than or equal to num one and num two greater than or equal
to num three. And basically if this condition is true, that means that num two is the biggest
right because it's bigger than num one and it's bigger than num two or greater than or
equal to and over here I can just return num two because that means num two is the biggest.
Then finally we can just make an else and I can say else let's return num Three. And
this one's a lot easier because if num ones not the biggest and num twos, not the biggest,
then we're only left with one option, which is num, three being the biggest. So by process
of elimination, we figured out that num three is the biggest. Alright, 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 3000. 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 3000. So it looks like our function is working, we tested
out all the different possibilities, and we were able to be successful. So down here,
you'll notice that I use 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 num one is equal to num two, 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 num one is not equal to num, two. 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. And 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 dot 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 numbers. 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 num one.
And then I'm just going to put a break over here. So this is going to allow us to get
num one. 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
gonna change this to number two. And the last thing we want to do is we want to get the
operator so I'm just gonna say op, and that's gonna 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 textbox. So we're gonna 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
gonna say name and we're just going to say this is going to be Opie, 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 webpage, 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 num one. 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 num, one number box. And then I'm going to do the same thing for num two.
So I'll say num two is going to be equal to post and num two. 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 Opie.
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 and 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 will allow our
program to respond to this value. So down here, I can just say if I 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 num one plus
two. So if it's a plus sign, then we'll just write out the answer. So it's going to be
number one plus number two. 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 num one minus num two, 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 signs, 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 Asterix, 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 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 two.
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 draf. And then we'll just say 3535. 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 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. And actually,
let me do it again. So we get that the nearest values are four and five. So it didn't actually
let me put in here, a decimal number. And that's actually has to do with how HTML works.
And basically, it 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 actually modify
that so we can 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 going to specify
that we can take numbers to this decimal point. So we can basically say numbers to the 10s
place. So over here, now I should be able to enter in a 4.6 with no problem, so I can
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 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, 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 are 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 gonna 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 can put a plus in here. And
then I can 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 a 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 some Calling here. And then I'm going
to say break right here. And I'll explain to you guys what break does in a second. But
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 could 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. And 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. Alright, 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 did see 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 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 gonna
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 into 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 can actually come down here and use what's called a default statement.
So I can 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 going to do is 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 GE 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 will 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 one. 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
of 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 and 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 five. 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. Alright, 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 12345. 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 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 five. If index is less
than or equal to five, 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 two. 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 conditions 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 this In time, we're going to print out
to 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 five, we're going
to come down here, we're going to print five out, and then five is going to get incremented,
up to six. And then we're going to come up here and check the condition, and six is not
less than or equal to five. 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 is 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 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 one. 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 gonna go ahead
and terminate this. So I click 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 gonna add
this back in. And now we'll go back to the normal program. infinite loops happened to
everybody. And as you you know, start learning more about while loops, I'm sure it'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 six.
So index initially has a value of six. And 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 six, 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 five,
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'll 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 are 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 six 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 do while 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 four loops.
in PHP. a 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 four 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 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. Alright, 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
12345. So I'm basically just printing out 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 indexes 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 five 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 loops.
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 for. And let me bring this over here for I want to make an
open and close parentheses and an open and closed curly bracket. So so far, the while loop and the for loop look pretty similar,
right? The for loop has a similar structure, we have this open and close parentheses and
these open and closed 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 for loops, parentheses,
we're actually going to specify three separate things. So the for 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 for 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 one 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 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 can 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
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 the 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 1234 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 can 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. Alright, 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 four 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 gonna
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 four 815 1623 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 zero in here. And that would give me
access to this first element. So I'm gonna 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 one, I actually want to start 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 five. 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 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 gonna refresh the page 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 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 gonna 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 42. 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 going to do exactly the same
thing, as you can see over there. So one more time, I'm just going to walk you guys through
what we have here, we started 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, the programmer can use and
sort of look at so you can use comments to leave little notes, you can 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 can 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 is 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
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 a 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 and Asterix. And now you'll see after I type this in everything down here, changed
color, everything basically became a comment. That is until I make another Asterix 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, it 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 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
could do whatever you want with the 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 a another file inside of our
php file. So I could set up like another php file or an HTML file, and then I can 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 this is
where 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 gonna show you guys how we
can do this. Now down here, I just have a very simple file setup. 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 have 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 100 different places, well, we can actually do in PHP is
we could write a HTML file for the header of our website, we can 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 dot html file and this header dot html file. And these
are like the world's simplest HTML files. And 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 on
my website. 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 of 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 dot html. So all I had to do was just say, include header
dot 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 is just say, include header dot 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 dot 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've updated the header, I made
a simple change. And I actually don't have to change any of the code inside of this site
dot 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, and 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 can 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 dot php file, and
I'm gonna come up here and we'll just make a new file. And I'm going to go ahead and
call this article header dot php. 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 field. 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 going to make sense. So this is
going to be our article header. And I'm actually just going to make a header too. And what
I'm going to do in here is I'm going to make some PHP tags. So I'm just going to say less
than sign, question mark, PHP, and then question mark greater than sign. And in here, I'm just
going to print out the title inside of this header to 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, too, I'm also going to create a header for for the author. So in here, again,
we're going to put some PHP tags. And I'm going to throw these here in the header form.
And this time, instead of printing out the title, we're going to print out the author.
And then finally down here, we're going to print out the word count. So I'm just gonna
say word count, colon, and again, I'm going to put these PHP tags in here, and we're going
to echo out the word count. So you'll notice that this is all this file is right I have
a header too. I have a header for 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 very Tables 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 dot 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 dot 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 view the page source, you'll see over here in the page source,
we have this header two, we have this header four, 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. And so what we can do is inside of this site, dot 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 that 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 article header dot php file.
And I was basically like, okay, we're gonna put the title in here, we're gonna 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
dot 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, dot php, file, assign, 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 sight dot 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 useful tools dot php. And 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
feat in mile attribute. But I wanted to use it inside of my site dot php file, I can actually
come down here and I can just say include useful tools dot php, and now that I included
this file, I can use all of that functionality and I can use that variable. So I can come
over here and I can say like, say hi and I can 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, Mike.
I could also use that variable so I could print out feet and 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
dot php While 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 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, 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 a 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 can create an age variable over here, and
I can set it equal to like 90, right? Maybe someone's nine years old, for something simple
like that, like someone's age, we can easily represent it with, you know, something like
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 of 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 can represent like, a keyboard, or I could represent a water
bottle, like I could represent anything I can 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 gonna 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 gonna 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 an 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 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 $1 sign, and I'm going to type out the name of the attribute. So
like I said, a book is going to have a title. And I'm gonna type a semi colon. A book is
also going to have an author. And a book is also going to have a number of pages. So I'm
just going to 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, and author, and pages. So every book inside of our program will have a title
and author and pages. And 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 can 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. 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 can do is I can actually come down
here and I can print some of this information out. So I can echo out like book one, title.
And now this is going to 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 can 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, it's 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 going to 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 going to change this. So this is going to be a Lord of the Rings
book, and the author is talking. 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 the
Lord of the Rings book, The author is token 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 can print
out Like, Book Two, and the author of Book Two is going to be token.
So now we're printing out token. 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 can 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 an A book objects, 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 seven hundreds. 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 want 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 will 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 going to have a title
and 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 token 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 take 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 curly
bracket. So so far, this is just a normal function, right. And it looks exactly like
any other functions that we've used throughout this course. 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 save 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 instil 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 Mike, 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 save this, it's actually calling that construct 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, I just want to point one thing out. So down
here, when we created these books, 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, four 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 is 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 would be ridiculous, like this alone is taken up eight lines of
code. So I'm going 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 and it down here and everything
worked, what we can actually do is we can pass in the books 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 gonna 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. Alright, 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
in. 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 in 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. Alright,
so what I can do now is whenever I create a new book, I can pass in all this information.
So I can pass in, like Harry Potter, and I can pass in the author rolling, and I can
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 going to 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. 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 excetera. So basically now we did exactly
what we did before, but we just saved ourselves a bunch of time and a bunch of code. Alright,
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 ones 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. 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 assigned 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 student one. 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 can do is I can actually create an object function up here inside of
this class. And that function will be able to tell me whether or not a 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 gonna 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 going to 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 students
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 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, we'll 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
gonna 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 can say, student one, 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 change this to
student too. So now this is going to be pam, pam is going to 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 use 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 objects 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
the 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, a getters and setters are basically a 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, 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, PG, and R, right? There's a bunch of these
different ratings that we can give to these movies. And here's the thing, there's really
only like a certain number of valid ratings for a movie, right? We have like G, PG, PG,
13, 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 can come over here. And I can give this a rating of
like 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, then 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 up into
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
of value other than g PG, PG, 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 pG 13. The first thing that we're going to have to do is we're going
to 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 can 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 can 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
the visibility modifier is basically a keyword that's going to tell PHP, what code is able
to access and reuse 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 there. And I intentionally
use this keyword there just because I didn't want to talk about these visibility modifiers
until this tutorial, but they're basically does the same thing. So they're in 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 there 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 refresh 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 of 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 going to is 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 gonna get rid of this line
of code. If I wanted to get the rating and print it out, instead of saying Avengers dot
rating, remember, this is throwing an error, we can't do this. What I can do though, is
I can say Avengers arrow, 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 could 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 can come down here and I can say, like, Avengers set rating, and I can 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, a 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 PG, PG, 13 r or
nr, 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 easy 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 an R. So let's say if they tried to enter
in a rating of like dog or some other nonsense, then we're basically just going to say the
movies 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 n r. 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 going to be an AR. 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 an 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? Alright, 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 can come over here, and I can 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 and invalid rating, it's impossible
for me to do that. I cannot give a movie a rating other than nr PG, 13, PG or G even
if I try to, like even if I come over here in passing 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 going to 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 is just using this
var keyword. And again, I was just using that because I wanted to explain public and private
In this tutorial, and I don'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 their too much
although you can use it um, 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
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. Alright, 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 and just prints out,
hey, the chef's making chicken, or when the chef makes a salad, it prints out the chef
make 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, 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, 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, we can
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 gonna 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. And
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 can 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. Alright, 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 going to 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 going to have both of these guys print out make special
dish so both chefs are going to 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 dishes, 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 dysfunction that I inherited from the chef class. And it's actually really
easy. All I have to do is just come down here and I could 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 and 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 use 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.