sanchez77 Posted August 16, 2009 Share Posted August 16, 2009 What does the ? and : mean in this statement. Can someone break it down for like a small child, hold my hand, so i better understand please? thanks $color = strtotime(sprintf('%s/%s/%s', $m, $d, $y) > time()) ? '#00F' : '#F00'; Quote Link to comment https://forums.phpfreaks.com/topic/170539-solved-and/ Share on other sites More sharing options...
steveangelis Posted August 16, 2009 Share Posted August 16, 2009 Google http://answers.yahoo.com/question/index?qid=20080202121355AAAMSl9 Quote Link to comment https://forums.phpfreaks.com/topic/170539-solved-and/#findComment-899537 Share on other sites More sharing options...
waynew Posted August 16, 2009 Share Posted August 16, 2009 $result = (conditional statement) ? (result if true) : (result if false); Quote Link to comment https://forums.phpfreaks.com/topic/170539-solved-and/#findComment-899538 Share on other sites More sharing options...
sanchez77 Posted August 16, 2009 Author Share Posted August 16, 2009 thanks, i get it now, cheers Quote Link to comment https://forums.phpfreaks.com/topic/170539-solved-and/#findComment-899541 Share on other sites More sharing options...
Daniel0 Posted August 16, 2009 Share Posted August 16, 2009 It'll not work as intended though. Match up the parentheses and see why for yourself Quote Link to comment https://forums.phpfreaks.com/topic/170539-solved-and/#findComment-899542 Share on other sites More sharing options...
sanchez77 Posted August 16, 2009 Author Share Posted August 16, 2009 what won't work, the principal or the code above? Quote Link to comment https://forums.phpfreaks.com/topic/170539-solved-and/#findComment-899557 Share on other sites More sharing options...
Daniel0 Posted August 16, 2009 Share Posted August 16, 2009 Take a close look at this statement: strtotime(sprintf('%s/%s/%s', $m, $d, $y) > time()) Evaluating from the inside and out we've got: A = sprintf('%s/%s/%s', $m, $d, $y) - presumably, this is the some date in the format m/d/y. This is a string. B = time() - this is the current UNIX timestamp and an integer. So we are evaluating A > B. A will get cast to an integer because we are doing a numeric comparison. The way PHP handles this is by chopping off from the first non-numeric character. So essentially we are checking if the given month is larger than the current UNIX timestamp. This will never be the case, so the operation will return false (a boolean value). Now, a boolean value is an invalid argument to strtotime(), and the way it handles this is by returning false. This means the ternary statement will always return the "false" component, i.e. "#F00" in this case. You can of course fix this by fixing the parentheses: strtotime(sprintf('%s/%s/%s', $m, $d, $y)) > time() I'll let you work out the logic in that statement yourself Quote Link to comment https://forums.phpfreaks.com/topic/170539-solved-and/#findComment-899565 Share on other sites More sharing options...
sanchez77 Posted August 16, 2009 Author Share Posted August 16, 2009 wow, I understand now, I appreciate you taking the time out to explain it to me. Thanks Again. Quote Link to comment https://forums.phpfreaks.com/topic/170539-solved-and/#findComment-899595 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.