Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/196770-undefined-offset-in-microtime/
Share on other sites

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.

 

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.

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 :shy:

P.P.S. Hmpf typo... d'oh

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.