tPet Posted March 28, 2010 Share Posted March 28, 2010 Please forgive my newbie question, and I did search here first .. maybe my old eyes missed an an earlier post with the same issue. So please forgive that too (if applicable) ... But anyway ... I use a random banner generator script on my websites. It works just fine. However, checking my error log I have a slew of these … PHP Notice: Undefined offset: 1 in [page path] on line 70 The function in that area of the script is … function GetBanner($min, $max){ $mtime = ((double)microtime()* 100000); $mtime = explode(" ",$mtime); $mtime = doubleval($mtime[1]) + doubleval($mtime[0]); < -- THIS IS LINE 70 mt_srand($mtime); $randval = mt_rand($min, $max); return ($randval) ; } Like I said, the script works peachy, but I hate the idea of it clogging up my error log. Any idea on how I can clean this up? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/196770-undefined-offset-in-microtime/ Share on other sites More sharing options...
cags Posted March 28, 2010 Share Posted March 28, 2010 Undefined offset means that you have attempted to access an index in an array that doesn't exist, so in your case it is probably $mtime[1]. When you do this it will automatically use an 'empty' value ie '' but will throw a notice level error. I'm not entirely sure of the objective of your code so it's difficult to say how to prevent it. Something like this would probably work... $mtime = doubleval($mtime[0]); $mtime += (isset($mtime[1])) ? $mtime[1] : 0; But I feel sure the code you are using is overcomplicating the task somewhat. Quote Link to comment https://forums.phpfreaks.com/topic/196770-undefined-offset-in-microtime/#findComment-1033001 Share on other sites More sharing options...
tPet Posted March 28, 2010 Author Share Posted March 28, 2010 You are a frakin' genius .. Thanks that did it. You are probably correct, the overall code could probably use some cleaning up, but I've been using it for years, and it works great. I just got it into me about finally asking about the sitelog error this morning. Thanks so much again. Quote Link to comment https://forums.phpfreaks.com/topic/196770-undefined-offset-in-microtime/#findComment-1033009 Share on other sites More sharing options...
salathe Posted March 28, 2010 Share Posted March 28, 2010 Which version of PHP are you using? If it is PHP 4.2.0 or newer (which I really hope it is!!) then most of your function could be removed. It can just be: function GetBanner($min, $max) { return mt_rand($min, $max); } Now for why you were getting the notice in the first place. Stepping through your code piece-by-piece: $mtime = ((double)microtime()* 100000); $mtime = explode(" ",$mtime); $mtime = doubleval($mtime[1]) + doubleval($mtime[0]); microtime() returns a string like "0.123456 1234567890" (double) converts that to a floating point number, so the above string would become 0.123456 Multiplied by 100,000 that becomes 12345.6 So $mtime is given the value 12345.6 Next we explode 12345.6 by spaces. There are none so we get an array containing one item: array("12345.6") So now you can see there is no second array value (at offset 1) for the next line to use! P.S. Welcome to PHPFreaks P.P.S. Hmpf typo... d'oh Quote Link to comment https://forums.phpfreaks.com/topic/196770-undefined-offset-in-microtime/#findComment-1033031 Share on other sites More sharing options...
tPet Posted March 28, 2010 Author Share Posted March 28, 2010 Sweet!!! Thanks, salathe! Quote Link to comment https://forums.phpfreaks.com/topic/196770-undefined-offset-in-microtime/#findComment-1033033 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.