tibberous Posted June 30, 2010 Share Posted June 30, 2010 I just spent 20 minutes swearing at my computer. This is one of the most annoying and hard to find bugs ever: function dateToSQL($m, $d, $y, $h=0, $m=0, $s=0){ if($h || $m || $s) return "$y-$m-$d $h:$min:$s"; else return "$y-$m-$d"; } See the error? It's that $m for month and $m for minute are the same. Why doesn't PHP throw an error? I have no fucking idea, but it probably should, since it would make no sense to ever intentionally do this. Since the function is so simple, I just first thought it was a problem with the query after it, then with the code before it. Then I thought that 07 was somehow getting converted into the int 0 because of the leading 0.... Just one of those nights... Quote Link to comment https://forums.phpfreaks.com/topic/206249-hard-to-find-bug/ Share on other sites More sharing options...
trq Posted June 30, 2010 Share Posted June 30, 2010 Another reason to name your variables with a bit more meaning. Quote Link to comment https://forums.phpfreaks.com/topic/206249-hard-to-find-bug/#findComment-1079009 Share on other sites More sharing options...
tibberous Posted June 30, 2010 Author Share Posted June 30, 2010 I generally do, it's just that to me: $y-$m-$d $h:$m:$s Is more readable than: $year-$month-$day $hours:$minutes:$seconds The second one just looks like a long string, not a date format. Quote Link to comment https://forums.phpfreaks.com/topic/206249-hard-to-find-bug/#findComment-1079012 Share on other sites More sharing options...
Philip Posted June 30, 2010 Share Posted June 30, 2010 Take your pick, but the shorter the var name the more likely you're going to accidentally use a previous var. $y-$mo-$d $h:$mi:$s $year-$mon-$day $hour:$min:$sec $year-$month-$day $hours:$minutes:$seconds Quote Link to comment https://forums.phpfreaks.com/topic/206249-hard-to-find-bug/#findComment-1079055 Share on other sites More sharing options...
zq29 Posted June 30, 2010 Share Posted June 30, 2010 I generally always use single letter vars for parts of dates and times when the functionality is contained, but I use $i for minutes, in line with the usage in date(). Quote Link to comment https://forums.phpfreaks.com/topic/206249-hard-to-find-bug/#findComment-1079091 Share on other sites More sharing options...
salathe Posted June 30, 2010 Share Posted June 30, 2010 Surely your code was issuing an E_NOTICE ("Undefined variable: $min ...") due to the use of the $min variable which had not been defined. I'd say that would point out the "bug" pretty darned quickly. Quote Link to comment https://forums.phpfreaks.com/topic/206249-hard-to-find-bug/#findComment-1079096 Share on other sites More sharing options...
Daniel0 Posted June 30, 2010 Share Posted June 30, 2010 See the error? It's that $m for month and $m for minute are the same. Why doesn't PHP throw an error? I have no fucking idea, but it probably should, since it would make no sense to ever intentionally do this. http://bugs.php.net Quote Link to comment https://forums.phpfreaks.com/topic/206249-hard-to-find-bug/#findComment-1079249 Share on other sites More sharing options...
tibberous Posted June 30, 2010 Author Share Posted June 30, 2010 Surely your code was issuing an E_NOTICE ("Undefined variable: $min ...") due to the use of the $min variable which had not been defined. I'd say that would point out the "bug" pretty darned quickly. It was return "$y-$m-$d $h:$m:$s"; originally - I fixed it, then changed it back to post it, but I didn't change $min back to $m. Quote Link to comment https://forums.phpfreaks.com/topic/206249-hard-to-find-bug/#findComment-1079314 Share on other sites More sharing options...
salathe Posted July 1, 2010 Share Posted July 1, 2010 Ahh, now that makes sense. Do you feel that the behaviour (not giving any warnings or notices) is a bug in PHP, if so then feel free to file a report at the link that Daniel posted. Quote Link to comment https://forums.phpfreaks.com/topic/206249-hard-to-find-bug/#findComment-1079582 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.