[SOLVED] Undefined Offset Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

[SOLVED] Undefined Offset Error

Post by Getran »

Sometimes when i refresh my page i get this error:

Notice: Undefined offset: 9 in *hidden*\random.php on line 9

this is the line it is referring to:
$RandomRotator1 = ereg_replace("\n","",$file[$randcount]);

This is the whole of random.php

Code: Select all

<?php
$RandomRotator1 = "";
$filename = "random.txt";
$file = file($filename);
srand((double)microtime()*1000000);
while ($RandomRotator1 == "") {
if(!isset($sp)){
$randcount = rand(0,count($file));
$RandomRotator1 = ereg_replace("\n","",$file[$randcount]);
}else{
$RandomRotator1 = ereg_replace("\n","",$file[$sp]);
}
}
print "$RandomRotator1";

?>
where i have it appearing i just use include("random.php");

As you can probably see it is supposed to take a random line from a file called random.txt and display it. It does work (it displays a random advertisement) but i get the error on top of it...

Can anybody help please ?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

It's $randcount = rand(0,count($file)); that's causing it.
You probably want $randcount = rand(0,count($file)-1);
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

ah, i think that did the trick..thx man
Post Reply