hello again as you know I'm Eli the
computer guy over here for Eli computer
guy.com and today's class is if else
statements in PHP programming so we're
finally really going to start getting
into what programming is all about it is
about dynamically doing things so with
if else statements basically what we
will be doing is we will say if one
condition is true then do this else do
something else so we are going to create
a little scoreboard app uh during this
class and I'll show you where you put
scores into a form and if team one has a
higher score than team two it will print
out team one wins and if team two has a
higher score than team one it'll say
team two wins and if team one and team
two have the same score uh it will say
that it is a draw so this is the
fundamental idea behind programming that
the user is able to input information
and then the program will do something
depending upon what information has been
inputed into it so if else statements
are uh are just just the core of what
programming is going forward so
basically if one thing is true do this
if not do something else so in today's
class we're going to be talking about if
else statements and if else if and else
statements so basically this is all it
is doing is it's looking to see if
certain conditions apply and if those
conditions apply then something within
PHP will be run um with all of these
classes now remember that in this PHP
programming track I am trying to keep
things very very very very very very
simple so when I show you the if else
and else if statements today realize
there are other ways to come about to
the same results in PHP programming but
I think these are the simplest concepts
for you to understand and learn uh right
now if you want to learn more about this
please go to php.net or do some Google
searching to learn more uh but again
with these classes I'm trying to keep it
as simple as possible so that you don't
get frustrated and frankly uh give up so
uh so this class as always is going to
be easy if you've been following uh what
I've taught you up until now nothing
that I show you today uh will seem that
that that that amazing uh you may have
to stop the video every once in a while
during the demonstrations uh just to
make sure you know what's going on but
again if you've been following me up to
this point today is just an addition to
what you've learned before but it's
going to be no big deal so as you know
I'm Eli the computer guy this class is
if else statements in PHP programming
and if you give me a second we'll Dive
Right
In so before we get into the if else and
else if statements uh we have to talk
about something called conditionals so
conditionals again go back to your math
classes back when you were 10 or 11
years old even before pre-algebra
remember greater than less than equal to
greater than equal to less than equal to
well you are going to have to remember
those uh conditionals for when you do
PHP programming the reason is is these
if else statements are based upon
whether
two variables uh how they relate to each
other so basically what you'll say is
let's say if and then you'll do
parentheses I'll talk about this more
but you'll have variable one so VAR one
and you'll have VAR 2 right so basically
what we're going to be asking is is VAR
one greater than VAR 2 is VAR one less
than VAR 2 is VAR one greater than or
equal to VAR 2 or is VAR one less than
or equal to V 2 so
these you should know like I say all the
way back in fifth or sixth grade you
should have learned these so when we do
these conditionals we're talking about
greater than less than greater than or
equal to or less than or equal to this
is all old standard math the only two
new conditionals that you're going to
have to learn today are equal to or not
equal to so if you simply use one set of
equal signs what this will do is it will
set a variable to the value of the other
variable so if all I do is I have VAR
one and I do equal in here what that
will do is it will set the value of
R1 as the value of r two so if VAR 2 is
two then it would mean VAR one is two if
you just simply use a single equal sign
in a conditional statement if you are
looking for are these two things equal
you do two equal signs so equal equal so
if you're asking is VAR one equal to VAR
2 you do equal
equal if you're asking is VAR one not
equal to VAR 2 you do exclamation point
equal so these are just the conditionals
that you'll have to remember again this
should be simple for you so you have
less than greater than greater than or
equal to less than or equal to equal is
equal equal sign not equal is is
exclamation part equal sign so these are
the conditionals just keep them in mind
what that's 1 2 3 4 5 6 things you just
have to keep in your head there are some
other conditionals but again I'm trying
trying to keep this all simple for you
so you don't get frustrated and give up
so those are the conditionals and those
are going to be part important in about
2 seconds so when we are doing the if
else statements or else if it's very
simple within the PHP programming all
we're going to do is we're going to
write if and so this says if something
is true we do parentheses and then we
would do let's say if VAR one is greater
than VAR 2 and then you close the
parentheses so it's saying if and the
condition parentheses so everything
within the parentheses is a condition
then you do the little squirely bracket
so it says if this if bar one is greater
than bar two do this so you do a
squirely bracket and let's say you say
print and then you say
VAR
one semicolon close squiggly bracket so
if VAR one is greater than VAR 2 then
you simply give it what PHP command you
want run you do the squiggly bracket
then you do whatever the PHP command is
so print and then I'll just say print
the value of var1 we will show this to
you better when we go on the computer I
just want you to get the idea before we
go on the computer so you know what's
going going on so that's all if this
condition is true do this then
else and then you do squiggly bracket
and then you tell it what you want it to
do if VAR one is not greater than VAR
two so if this condition print VAR one
else print VAR 2 that's all there is to
it what you have have to remember with
these if L statements is you just do if
parentheses and give it the conditions
you want var1 is greater than VAR 2 VAR
one is less than VAR 2 VAR one is equal
to VAR 2 close parenthesis now the
important part is there's no semicolon
here right no semicolon don't do a
semicolon for this for the LL statements
then what you do is you do the squiggly
bracket and then give it the PHP command
whatever you want to have happen here
right now I'm saying to print but in the
demonstration I'll show you you can set
the variable to a value you could run
any PHP command you want here so you
just do that just like you normally
would again with a semicolon here
because you're actually running a a PHP
command and then you close the squiggly
bracket so if this is true do this else
again squiggly bracket give the PHP
command close squiggly bracket so
remember remember with PHP you always
open you always close squiggly bracket
close squiggly bracket squiggly bracket
close squiggly bracket so this is all
there is to the simple if else statement
so if this is true do this else do this
the only thing is if parentheses
conditions squiggly bracket Clos
squiggly bracket else squiggly bracket
close squiggly bracket see that that
that's pretty simple is it that's all
there is to these if else statements so
the question you may have is what about
if you have multiple things you want to
check for So within um the little
scoreboard application I'm going to show
you team one could be greater than team
two and then this will happen or team
two could be greater than team one and
then this will happen or Team 1 and team
two could be equal
well then what do you do here well in
this process if VAR one and VAR 2 are
equal then it fails so what we will do
is we'll use the else if
statement so what happens is we stay up
here with if and the condition but down
here where the else is we will just make
this else if so instead of else it now
sells else if and we give it a condition
and so now we
say VAR one is less
than VAR
2 and then we use a squiggly brackets
and then we say whatever we want so
print let's say VAR
2 squiggly bracket so if condition do
this else if again condition do this and
and then you do have to close this with
an
else so if this is true do this else if
if this is true do this else none of
it's true then again you do squiggly
bracket and you go print and you say I
don't know you
suck see how that works this is all
there is to if else and else if
statements with these else if statements
we won't do it in the demonstration but
you can have multiple else ifs if this
is true do this else if this is true do
this else if this is true do this else
if this is true do this else if this is
true do this else do this this is all
there is to these if if else statements
and again I know a lot of you this
whiteboard is really junky right now but
I want you guys to get the idea of
what's going on before we get to the
computer so the main thing is remember
these conditionals greater than less
less than greater than or equal to less
than or equal
to equal equal means equal to
exclamation equal means not equal to if
you understand this when we go to the
computer everything will be pretty
easy so in this PHP programming series
we are about to create our second app so
the first app we created was that that
newsletter app where we we requested
somebody's email address and if they
gave it to us we wrote that into a file
so that was your first app now we are
going to create a scoreboard app again
the scoreboard app we're going to have
Team two teams team one and team two if
team one wins basically when you hit
submit it'll say team one W if team two
wins and you hit submit it'll say team
two1 and if both of them are equal then
it will say there is a draw or no one
one again in the programming world in
the technology world it's all about
confidence it is not simply what you are
able to do but what you tell people
you're able to do so by saying that you
are now an app developer people will now
treat you more like an app developer so
I want you to go out after this is done
and tell people you have created your
second app it may not be a cool app it
may not be a fancy app but to be honest
with you at the end of the day once you
understand all this PHP programming you
understand a little JavaScript you're
probably going to be a little depressed
on how simple a lot of this stuff is to
create so a lot of those people that
pump themselves up and are professional
app developers like I say at the end of
the day you're going to be like oh oh
that that's what you're doing that's it
uh remember for those of us older folks
Napster was created you know the the
thing that brought down the entire music
industry was created in a month by a
college kid who had no programming
experience before he sat down with an
HTML book for dummy so this stuff really
isn't that complicated so let's go to
the computer we're going to create this
little scoreboard app again I know this
is this is a mess but we go there I'll
I'll show you how all this works at this
point again make sure that if you're
confused at all just pause the screen
pause my little video and look at
exactly what I have typed out because
everything that I've typed out I really
do upload to my GoDaddy account so if it
works for me it will work for you so if
you get confused just make sure just
just pause the video take a look most
likely you forgot to put in a little
squiggly bracket or you forgot to put in
a semicolon or you did some other small
little mistake so again honestly that's
why I don't like complicated programming
because I make lots and lots of small
little mistakes so if something doesn't
work for you just pause the video take a
look make sure you you see exactly
what's going on and I guarantee if
something fails it's because like I said
you forgot a semicolon or you forgot to
close a semi uh or a squiggly bracket or
so on so with that let's go on to the
computer I'll give you a demonstration
on how this works and like I say this is
starting to get really exciting because
now that we can do IFL statements we can
really start getting into the fun
stuff so here we are back at my Windows
7 computer so now that we're getting
into a little bit more sophisticated PHP
programming I want to show you the
results we're trying to get to before we
actually program it now so before um
I've just coded everything out and then
shown you the results now I want to
start showing you the results and then
create the the program and then show you
the results after we've created the
program so you have an idea of what is
going on what we are trying to do so
this is going to be uh one of our first
little uh little apps in PHP so we have
this little scoreboard app here and what
this scoreboard app does is you put in
the score of team number one and then
you put in the score of team number two
and then basically depending on Whose
score is higher when you press the
button declare winner um it will say
either team one one or it'll say team 21
or it will say no one one uh it's very
simple it's very simple but this will
show you how the if else statements work
or else if um so basically this just
gives you a general idea of how these
conditional statements work so to show
you uh if I plug in here and I say team
one got 11 points and team two got 10
points and I hit declare winner we can
see it now prints out it says team one
wins so if we go back and then we say
that team two got let's say 13 points
and we do declare winner it says team
two wins and then if we go back and we
say that both teams scored 13 points and
we declare winner is it says no one wins
so we are starting starting get to get
into the real power of PHP now this is a
completely Dynamic website so you plug
in information and depending upon the
information that you plug in um a
different web page will be created so
when team one wins it'll say that team
one won if when team two wins it'll say
that team two win won and if we uh if
there's a tie it'll say that no one wins
so this is all completely done
dynamically and it's all done through
the if else statements and with else if
so what we do since you know what is
going on now so we have the form and as
always with these HTML forms this form
passes the information to a PHP script
that then prints this out so if we go
over to notepad++ right now we can see
the form that I've already created now I
am not going to rewrite this out uh if
you need to pause the video and copy
this please do but we've already done a
lot of forms and so there's no reason
for me to to type out the form every
time on these videos so just going over
this again to make sure you understand
what's going on remember this is an HTML
form so we have to print this out on an
HTML page so we open the HTML tag we
open the head we close the head we open
the body and then here is the H1
scoreboard app so this is where we tell
the user what it is they're using and
then underneath is the form so we open
the form and then we say the action is
what the action is remember is that we
are sending the data to a PHP script so
we're sending this data to the if elor
script.php and we're doing it by the
method of post all uppercase uh remember
there's post and get so far I've only
taught you post just to make life
simpler then over here first we open
with the HTML wording so team one score
so this tells the US user what we're
asking for and then we have a text box
so input type equals text name equals
Team 1 so remember this is important
because this is how the PHP script
will'll know how to differentiate
between the data that it's receiving
then the size of the text box and then
we close paragraph team two score input
type text again name equals team size
equals 3 close and then input type now
this is still a submit button so I've
been talking about submit buttons we've
been doing a lot of submit buttons but
you'll notice on this web page the
submit button is a little different so
if we go back you'll see that the submit
button says declare winner versus submit
well the reason for that is when you're
creating the submit button remember you
do the input type equals submit that
means submit button the name of it
equals submit again so that the program
can track it but then the value the
value equals declare winner so the value
is is the wording that shows up on the
button itself declare winner so that's
one of the new things uh with
programming that I haven't shown you
before that's how you can change the
name of the submit button is by simply
changing the value to equals declare
winner and then of course we close the
body and then we close the HTML so this
is the HTML form that we are dealing
with um again if you need to pause this
for a second uh pause it and copy all of
this we are not going to be modifying
this form so all the modifications we do
during this this little training session
will be done on the if Els script.php
nothing else will be modified on this
form so this form is this form so if you
want to take a second and uh copy it uh
please do so now uh as I said we are
going to be passing the data from this
form to the script if unor elore pH uh
script.php so I've already created that
script and as you saw before it already
runs well this is a class and so it's
not fair if it's all done again the form
you should already know but I do want to
write out this PHP script so that you
understand what's going on and I will
make a couple of versions of it again so
you understand what's going on now when
we create the PHP script remember we
have to actually tell The Interpreter
that we are doing a PHP script so we
open the PHP script we do a couple
enters question mark bracket and then we
close the PHP script so this way
um we make sure that we open and we
close the PHP script the next thing that
we're going to do is we have to create
the variables and set the values for the
variable for both team one and team two
so that PHP can figure out who the
winner is so again dollar sign and then
team one this creates the team one
variable equals and then remember we're
getting the information from that form
so we get the information from the post
and we do a bracket single quotation
marks and then we do the name from the
form so name here is team one and name
here is team two so we just do team
one semicolon so that creates the
variable team one and sets the value for
it and then here we
create the variable and set the value
for team
two now this is where it's going to be
important for you guys to start getting
used to writing uh this code in similar
syntax so if you'll notice whenever I do
these programs I I always have the same
way of writing stuff out or naming
things so in a score form I would just
do the name of team one and team two
that means when I come over to the
script I already know in my head what
the the the the values that are coming
over so team one and team two if you
come up with like a lot lot of random
names you'll forget what those random
names are and then you always have to go
back to the form and figure out what you
named something if you come up with a
naming convention a way that you name
things uh it will just make life easier
because it'll be easier for you to
remember them but um so yeah so we've
created the PHP script and now we have
created the variable team one and we've
set it to the value of of what came over
from the text box named team team one
and then team two we set it to the value
of the text box for team two now we're
going to go down and we are going to
create the if else statement so right
now we are just going to create an IFL
statement so what we do is we just do if
and then we do parentheses and we give
it a condition so what we're going to
say is if team one's value is greater
than the value of Team two then
something is going to happen
so I come down here and then I do the
little squirly bracket so this says if
the value of Team 1 is greater than team
two then what I'm going to do is I'm
going to print so this sends the PHP
command and I'll say team
one
wins so what I've done here is I've done
the normal print command just like we've
been doing all along then the double
quotation marks team one wins Clos
double quotation marks and again
semicolon and this is all within these
these squirly brackets so if you do
parenthesis you do the conditions close
parenthesis then the squirly brackets
and then print just like you would do a
normal PHP statement then semicolon and
then close the squirly brackets so if
team one is greater than team two it
will print out team one wins now we're
going to go down and I'm going to do
else so what this says if Team 1 is
greater than team two do this
else we do the squirly brackets again so
remember anything within these IFL
statements will be within the squirly
bracket we'll do
print
team two
wins squirly bracket so if the condition
squirly bracket print just like you
normally would then close it lse team
two print team two wins semicolon
squirly bracket so that's all you have
to do for what we're going to do at
first so if team the value of team one
is greater it'll print team one wins if
the value of of Team two is greater
it'll do team two wins so all I'm going
to do is I'm going to save that and then
I'm going to upload
that and now if we do this I'll just do
a refresh and let's say if team two team
one gets a score of two and team score
gets uh two team two gets a score of
four and I declare the winner it now
says team two
wins if team one gets a score of 10 and
team two gets a score of four team one
wins so notice all it's doing is if the
value of team one is greater than the
value of Team two it prints this
statement as else the value of Team two
is greater than the value of Team One it
prints this statement that is all that
is happening right now it's not very
complicated now the question you're
going to have though is you'll say well
Eli what happens if there's a draw so
team one scores 10 and team two scores
10 and you declare the winner and then
it says team two wins well that might be
great if you're team two but if you're
not team two that's kind of that's kind
of sucks because it's a draw well that's
the thing is is if there's a draw then
the second variable will win so what we
have to do is we have to go in there
with the else if
statement so that um if there is a draw
we know what to
do so all we have to do in order to do
the else if statement is what we're
going to do is again reuse code whenever
you can so I'm just going to edit the
code that's here so we have if and then
we have the conditions parenthesis Team
1 is greater than team two now we'll do
else if and then we'll give it
conditions
parentheses and we'll say Team 1 is less
than the value of Team
two and so this is the condition now if
team one is greater than team two print
team one wins else if team one is less
than team two print team two wins and
then what we're going to do is is now
we're going to say
else so if neither of these are true if
team one is not greater than team two
and team two is not greater than team
one that means that there is a draw and
so what we're going to say
is
print it's a
draw squirely so if team one is greater
than team two team one wins if else if
team one is less than team two team two
wins
else if the neither of these are true
which a draw would be print it's a draw
so now we hit save and now we hit
upload
now we go back to my web page that I've
created my little app and again if score
one team team one gets a score of two
and team two gets a score of three team
two wins but now if team one gets a
score of three and team two gets a score
of three it's a draw see so that's how
the if else and else if works so before
if Team 1 was greater than team two it
printed one thing the other way uh and
if it wasn't then it printed the other
the issue is that if it was a draw well
well then what do you do then that that
doesn't work for you so that is the
basic idea of how these if else
statements and else if statements work
now again since uh whenever we do most
of this stuff I always want you thinking
in variables what we're going to do now
is we are going to use these if else
else if statements to change the value
of a variable so right now uh we're
printing all of this out right well
instead of printing if these conditions
are true I want the results of a
variable to be changed so what I'm going
to do first now is I'm going to create a
variable but I'm not going to set the
value of it so I'm going to do
result and then I just do semicolon so
what I'm doing here is I'm creating a
variable but I'm not setting the value
because the value will be set down here
in the if and else if and else
statements so what I'll do here is I
will delete this print and instead of
doing print what I'm going to do is I'm
going to make the variable result equal
something so we're going to set the
value of the variable result and so what
I'm going to do is I'm going to set it
to the string team one so instead of
printing something right now I'm setting
the variable's value to team one so if
team one wins we set the variable value
to team one now I'm going to go down
here and if team two wins result equals
I'm going to set the value of the
variable result to team
two
so again this is all just standard PHP
coding it's just in a little different
format and then down here for the lse so
if team one wins result the value of
result goes to team one if team two wins
the value of result goes to team two and
if nobody wins if it is a draw then the
value of
result is set
to no
one now this will make a little more
sense in a second so so in this we've
created the variable result and we have
set different values to the variable
result depending on what's happened with
the score so now what we're going to do
here now is we're going to go down a few
lines and now we're going to do a print
so this print command is outside of this
if else statement so basically when the
PHP code is running these variables get
created and set then the if else
statement will be run and then then the
print command will be run so now what
we're going to do is we're just going to
do a simple print we are going to call
the variable result so that will print
the value of the variable
result and then just say
wins so now this will be completely
Dynamic and completely variable so the
the the numbers will be sent over from
the form they will the values of the
variables will be set here if team one
is higher
the variable result will be set to team
one if team two is higher the variable
result will be set to team two and if
they're equal the variable result will
be done to no one so now if we hit save
and we hit
upload what we can do is we'll just go
back and we'll refresh
this and if uh Team one gets a score of
11 and team two gets a score of 10 and
we declare a winner it now says team one
wins
so that was simply dropping the value of
the variable result in there to say team
one
wins if we go
back and then let's say team two scores
12 when we declare winner team two wins
and now if they're absolutely equal
declare a winner no one wins so I've now
shown you how to program these if else
and else if statements uh very simply
first by simply if one one condition is
true um that a print statement is run uh
and then after that I showed you how to
do this with the variables again
whenever you're working with PHP try to
work with variables as much as possible
this will make your life much easier um
if you do this with variables than if
you try to hardcode this because again
if I want to change the statement down
here you know some body wins or what it
says with the programming with PHP since
I dropped the value of the variable I
only have to change this one line of
code if I had written three lines of
code I would have to go back and modify
all three lines of code simply for what
this simple sentence will stay if that
makes any sense I hope it does so with
that uh let's go back out to the real
world and and talk a little bit
more
so that's all there is to if else
statements in PHP programming so I
showed you how to do the if else
statement and then I showed you how to
add in the else if um in the middle so
basically if condition do this else if
condition do this else do this pretty
simple the main thing to remember with
these conditionals is again you have to
remember back from when you were nine or
10 in grade school is remember greater
than less than greater than or equal to
less than or equal to the main new ones
that you may have to remember are equal
to as in questioning is this equal to is
equal sign equal sign and then saying
that it's not equal to is exclamation
part equal sign so that's that's really
all there is to it again uh with his PHP
programming at this point you should be
playing around so I showed you all this
so you should have created all this and
then once you've done created this make
once you made sure that all this works
now start playing with it see what you
can do see see what other stuff you can
mess with again this is the ninth class
in this PHP programming track so at this
point you should be looking at going to
php.net you should be looking at picking
up some PHP programming books again just
to flesh out what I'm talking about um
all of my classes all of my tracks the
entire way that I try to train you is by
keeping everything as simple as possible
so you don't run into any stupid
problems um the only issue with that in
all honesty is is I gloss over a lot of
stuff so there is more to if else
statements than what I have taught you
today but if I try to teach you all that
you might get confused then you get
frustrated then you put your hands up
and go screw this I'm going to go have a
beer and I want you to have a beer but I
want you to have a beer when you're
happy not when you're frustrated um so
yeah so so this this will do you well as
we continue on in this PHP program this
is going to be very important so if you
didn't understand what I showed you
today go back and watch it again and
again and again and again until you do
understand because going
forward this and the other things that
I've have taught you are going to become
more and more important in programming
remember you build on what you have
learned before so if you did not learn
it well enough you're not really going
to be able to learn the next topic so uh
so so make sure you understand this but
uh but yeah but this is cool you just
made your first really dynamic app uh
it's a little scoreboard
app doesn't seem like a big deal but
again you will be surprised um how close
that is to many of the things that you
see up on the web once we get done with
this PHP track again it's going to be
kind of sad um how easy creating some of
these these these these complicated apps
uh can be so uh so as you know I'm Eli
the computer guy over here for Eli the
computer gu elithecomputerguy
this class was if else statements and
PHP Pro programming as always I enjoy
teaching this class and look forward to
see you in the next
one