PHP Front To Back
Lesson 7 of 22 · PHP Front To Back [Part 7] – Conditionals & Comparison · IT & Software
PHP Front To Back [Part 7] – Conditionals & Comparison
hey guys in this video we're going to cover an important part of not only PHP but programming in general and that's conditionals
alright so conditionals are used to test for something test for a value compares compare stuff whether it's comparing if it's equal to something a greater or less than I'm going to go over all the
different comparison operators as well okay so I'm going to create a new file in PHP sandbox and I save it as conditionals dot PHP okay let's open up our PHP tags and we're going to actually
just comment in here now I'm going to specify the different types of comparison operators so we have equals which is a double equals and then we also have this triple equals which
matches not only the value but also the datatype okay so this is actually identical and then we have less than great what greater than I also have less than or equals two we have greater than
or equals two we have not equals two and we have not identical okay so let's go ahead and try some of these I'm just going to create a variable called num and such as equal to five okay so if we
want to test to see if this is equal to five we can do if and then we can do num is equal to five okay and if that's true let's just echo so we'll just echo five past
okay so let's save that and then let's go to localhost / PHP sandbox slash conditionals dot PHP oops forgot the Oh okay so we get five past because of course num is equal to five now for this
one here identical I'm going to show you if I change this to a string and I just put quotes around it and we reload it still passes okay because this is just being the value not the datatype now if
I put an extra equals in there and we reload it doesn't pass because this is an integer and this is a string okay and this tests for it to be identical all right we can also do so you'll just take
that off you can also do let's say greater than so if num is greater than five if we reload we don't get anything because five is not greater than five if I were to say greater than or equal to
five then that's going to work all right we can also test if it's not equal to so if I change this to 52 and reload it's going to pass because num is not equal to five
all right so this is very basic stuff so let's let's add another equal there and let's say else echo and it'll just say did not pass okay so if we save that reload we get five passed if we change
the number let's say six and reload we get did not pass now if we wanted to test for another value we could also do an else if so let's go on this line and say else if and then go like that and
let's say num is equal to six and then we'll just echo out six passed okay so now if we save that we get six past if we change it to five reload we get five past if we change it to four we
get did not pass all right so that's very very basic itself in elseís so let's comment this out now you can also nest if statements so let's say a nesting if and let's do num equals five
and then we'll just do if num is so state yeah greater than five and then in here we could actually keep putting it statements as well as many as we want and we'll say if num is let's say less
than ten so let's do greater than four less than ten and then in here we'll just echo it'll say num past okay so if we save that reload we get five past we changes to six reload we also get six
past okay because it's still greater than four but less than ten now this is an ideal there's a better way we could we could do this and that's by using logical operators okay we can put more
than one condition in one if statement so let me show you what I mean and I'm first just going to show you the logical operators so we have and which can also can also be formatted as a double
ampersand we have or which could be the double pipe characters and we have XOR which is exclusive or so a better way to write this would be to do one if statement and then do if num is greater
than four in numb is less than 10 all right and they will just do the echo numb past okay so I'll comment this out and let's save that reload and we still
get six past okay so this is a much better way to write it than this now we can also use the double ampersand okay now if you wanted for instance this if you have an else then you would want to
nest it because you have to put this house in here and then you do let go you know whatever all right so we also have or which means that one of these has to be true so let's do that we'll put in
our double pipes or the word or and save that reload and we get six paths okay because one of these is true even if we were to let's say make one of these false by saying two two because it has
to be greater than four to isn't but if I reload it still passes because only one of these has to be true okay and then we also have exclusive or EXOR which means that one of them has to be
true but not both so if we save this that this should pass because one of them is true but if we were to change this back to six which makes it true for both and we reload it doesn't work
alright so just remember that an XOR means that one has to be true but not both alright so let's take a look at switches now okay let me just comment this out
okay so switches will test for value and then it'll test it with different cases and depending on which case matches it'll do something whatever you want to do so it's a switch and what we'll do is
create a variable I'm going to call it fav color and we'll set that equal to red okay so let's say we want to test for the save color value we can put any switch and we want to test these color
okay now let's specify a case so we'll say case let's use single quotes case red and then we want to put a colon right here and then whatever we want to happen if it's red so let's say echo I
will say echo and your favorite color is red okay and then you just want to put in a break when you're done with whatever that functionality is okay so that's one case let's go ahead and copy
this and paste it in do three more times so this will be blue and change this to blue and let's say green all right so this bottom on here I'm actually going to change this from case
red to default and then let's echo out we will say your favorite color is something else okay we actually don't need the break here so basically it's testing the saved color if it's red this
will happen blue this screen this and then if it doesn't match any of those the default will fire ox all right so let's go ahead and save that I'm sure you know what's going to happen we get
your favorite color is red okay we change it to blue and reload your favorite colors blue change it to green your favorite color is green and then anything else turns into yellow will say
your favorite color is something else okay so pretty simple so that's going to be it for if statements and switches in the next video we're going to talk about dates
and formatting dates and how to create dates from strings and so on alright so thanks for watching and I'll see you in the next video
In this video we will look at if statements as well as if…else and switches in PHP. This video is for PHP and programming beginners. There are more advanced topics to come
CODE: Code for this video
http://www.traversymedia.com/downloads/phpsandbox/phpsandbox5.zip
VISIT US:
http://www.traversymedia.com
EDUONIX COURSES: Pleas use affiliate links from website below
http://www.traversymedia.com/eduonix-courses
SUPPORT: We spend massive amounts of time creating these free videos, please donate to show your support:
http://www.patreon.com/traversymedia
http://www.paypal.me/traversymedia
FOLLOW TRAVERSY MEDIA:
http://www.facebook.com/traversymedia
Posts by traversymedia
http://www.linkedin.com/bradtraversy