![]() ![]() 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 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 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 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 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 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 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Īhead 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 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 ![]() 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 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Īnd 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Ĭoncatenate 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 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 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Ĭondition 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 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Īnd 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 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 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įour 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 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 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 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 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 PHP Front To Back – Ternary & Shorthand Syntax ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |