PHP Front To Back - [Part 13] Ternary & Shorthand Syntax

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you guys welcome to your next PHP front-to-back video in this video we're going to be looking at the ternary operator as well as shorthand if statements and shorthand loops so we're going to do is create a new file in our PHP sandbox and save it and shorthand dot PHP right and go ahead and throw in up teach P tags and we're going to set a variable here called login and we're going to put that to true okay so normally if if you were to use it just a regular if statement we could say if logged in and then we want to echo out you are logged in and then we could put an else statement run else and we could say echo you are not logged in okay and let's just run that sandbox slash and see what they call it shorthand dot PHP okay so we get you are logged in if I change it to false we get you are not logged in now we can actually save ourselves what is this one two three four five lines so we can save ourselves four lines and put this all in one line in a single state okay so what we can do is we can say echo and then in these parentheses we can put the condition so let's say login and then we can use the ternary ternary operator and that is a question mark and then what goes here is what we want to happen if that statement is true and then we can put a colon and then what we want to happen if that's not true okay so we want to echo this out if it's true and this if it's not okay so this one line will do the same exact thing that this five lines does so let's go ahead and just comment this out and let's go ahead and run that and we get you are not logged in we change this to true and we run that we get you are logged in now in this case we're echoing it but we can also do things like assign variables based on our condition so let's say we want to set is registered and we want to only set it we want to set it to true if logged in is true so we could just put in our condition which is logged in this could just as well be like that and it could be any condition and then we want to set it to true if that's true and false if it's false okay and then let's just echo it is registered and we should get a one okay if we set this to false and reload then it's going to give us this is also false now now we can also nest be statements okay so I'm going to use an example that I got from a blog post we're going to say that this is a game and we're going to set the age of the person to 20 and set their score I'll set their score equal to 15 okay and then we're going to do a nested ternary so let's echo out your score is and depending on the age and the score we're going to say it's either average or exceptional okay so let's just concatenate right here and we're going to say score is greater than 10 and then we do our question mark and then whatever we want to do if that's true and then the colon and whatever we want to do if it's false so we're going to nest another statement into these so if it's greater than 10 then we're going to say all rightful it's the age is greater than 10 then we're going to make this average if they're under 10 we're going to make this exceptional okay so that's the first nested statement then on this side we're going to again look at the age and we're going to say if the age is greater than 10 then those two and there then we want to say average I'm sorry not average horrible and then this would be average okay so let's go ahead and save that and we get your scores average so to reiterate we're saying if the score is greater than 10 then we're going to check for the age if that's greater than 10 then it's going to be average if the person is under 10 that's going to be exceptional okay so that's the first part and then here we're saying if the score is less than 10 and the age is greater than 10 it's horrible if it's under 10 then it's average okay so if we change let's keep the age of 20 and we'll change the score to 5 and reload and we get your scores horrible if we change the age to under 10 then it's going to be average and if the score is over 10 and the age is under 10 we're going to get exceptional okay so that's just an example of a nested shorthand statement like that all right so I'm going to just comment all this out okay and then we're going to look at an alternative syntax for conditionals and loops and pretty much anything else so this is just close our PHP so this is usually what you would use if you're embedding your statements and stuff inside of HTML so let's put a div here and I'm going to give you an example the regular way first so let's say we want to say if log in and what we would have to do here is either echo out the HTML but we don't want to do that so we would have to do it this way PHP closed we have to close it and go like that and here we just want to make them l we don't want any PHP well say welcome user okay so let's save that and let's set this to true okay we get welcome user now if we wanted to do an LS we'd have to go in here and do this open it and totally here we have to do close it and I'll put an h1 and we'll just say welcome guest okay so that's going to work if we change this to false save it we get welcome guest now I don't know about you but I don't like how this looks with the opening and then we have to close it down here with new PHP tags there's a better looking way to do this and it's not going to really save you lines but it's going to look a lot better so let's say we have our div and we want to do PHP if and then what we can do is we can use a colon you can go like that okay and then say logged in and then when we want to end it we can simply do end if like that and then we can have our welcome user alright so if we save that and reload it changes to true and reload and we get welcome user if we wanted to do an else here you could just do PHP else and a colon and then we can put our welcome guest okay so now we get to welcome users and we should get to welcome guests now you're not limited to just if statements here you can use loops so for instance let's create a simple array the array equals we'll just put numbers in here and then down here let's do a div and we'll say PHP for each and in here we'll say for each array as Val and then down here we can just do end for each okay and let's just HP echo Val save that and you can see we're looping through that array okay you can also do this with loops and I'm just going to paste it in because this is getting kind of long C we're using a for loop and we're just going to echo out the iteration so reload and we get zero through nine okay because the condition is if it's less than ten so just some some different ways to write your code if you're using PHP templates this is a much nicer way than doing it like this okay doesn't save you line get the same number of lines but it looks a lot neither alright so that's it for the shorthand and alternative syntax for conditionals in the next video we're going to take a look at filters and validation
Info
Channel: Traversy Media
Views: 51,517
Rating: undefined out of 5
Keywords: php, ternary, php ternary, php shorthand, php alternate syntax, php syntax, if statements
Id: NUq0ZT54zvw
Channel Id: undefined
Length: 10min 6sec (606 seconds)
Published: Sat Mar 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.