yazakib Posted August 1, 2008 Share Posted August 1, 2008 I haven't programmed in PHP in awhile, and i probably just never have seen anyone format something in this way, but I was just curious about it. I was looking around to find scripts/tutorials for making a dynamic calendar (go back and forth between months as well as post events to the calendar) and it starts out by defining the month and year variables like this $m = (!$m) ? date("m",mktime()) : "$m"; $y = (!$y) ? date("Y",mktime()) : "$y"; i've never seen that before so i'm baffled can someone enlighten me? I understand the date("y", mktime()) function, i just dont know what the first part and the last part are for the \"(!$y) ?\" and \": "$y"\" they also go on to use it again at the end when creating the heading for the calendar <?php echo $_SERVER['PHP_SELF']; ?>?m=<?=(($m-1)<1) ? 12 : $m-1 ?>&y=<?=(($m-1)<1) ? $y-1 : $y ?> and i just dont understand. I know that it creates a link to the next month, and the ?m&y represent the month and year in the link, but what is all the rest? any help would be great -brodie Quote Link to comment https://forums.phpfreaks.com/topic/117759-question-about-a-certain-format/ Share on other sites More sharing options...
DarkWater Posted August 1, 2008 Share Posted August 1, 2008 It's the ternary operator, which in my opinion is quite sloppy. It follows this format: (condition) ? TRUE : FALSE; If the condition is true, the TRUE statement is executed, otherwise the FALSE one is. Quote Link to comment https://forums.phpfreaks.com/topic/117759-question-about-a-certain-format/#findComment-605679 Share on other sites More sharing options...
yazakib Posted August 1, 2008 Author Share Posted August 1, 2008 awesome thank you so much. Ya i agree... it was really... blah. I actually just broke it down into a "if" function to try and figure out what it was... haha... oh well. thanks so much for the explanation though, really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/117759-question-about-a-certain-format/#findComment-605692 Share on other sites More sharing options...
DarkWater Posted August 1, 2008 Share Posted August 1, 2008 No problem. Quote Link to comment https://forums.phpfreaks.com/topic/117759-question-about-a-certain-format/#findComment-605693 Share on other sites More sharing options...
.josh Posted August 1, 2008 Share Posted August 1, 2008 what? ternary is the bomb diggity yo I <3 ternary Quote Link to comment https://forums.phpfreaks.com/topic/117759-question-about-a-certain-format/#findComment-605714 Share on other sites More sharing options...
DarkWater Posted August 1, 2008 Share Posted August 1, 2008 I use it once in a while, but not too much. >_< Quote Link to comment https://forums.phpfreaks.com/topic/117759-question-about-a-certain-format/#findComment-605731 Share on other sites More sharing options...
.josh Posted August 1, 2008 Share Posted August 1, 2008 well I mean, I only use it when I have to but when I do I'm like "yay ternary ftw!" I even stand up and do the wave. Quote Link to comment https://forums.phpfreaks.com/topic/117759-question-about-a-certain-format/#findComment-605733 Share on other sites More sharing options...
DarkWater Posted August 1, 2008 Share Posted August 1, 2008 The one-man wave is weaksauce though. And I just rename a file to FAIL if I see a nested ternary anywhere in it. $var = ($foo == 1) ? (($bar == 3) ? "lol" : "rofl") : "this sucks"; xD Quote Link to comment https://forums.phpfreaks.com/topic/117759-question-about-a-certain-format/#findComment-605737 Share on other sites More sharing options...
.josh Posted August 1, 2008 Share Posted August 1, 2008 Okay well I would certainly never do a nested ternary like that. Also, one-man wave I think not! I have many imaginary friends. It helps if I've been drinking... Quote Link to comment https://forums.phpfreaks.com/topic/117759-question-about-a-certain-format/#findComment-605738 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.