i keep getting this one certain refreshes of a random text generator i'm using. this host doesn't seem to want to turn off notices either, so i'm stuck with seeing it.
Notice: Undefined offset: 16 in W:\www\newvat\functions\randomtext.php on line 29
here's the code:
function randomtext($filename) {
$file = file($filename);
srand((double)microtime()*1000000);
while (empty($quote)) {
// offending line follows
$quote = ereg_replace("\n","",$file[rand(0,count($file))]);
}
echo $quote;
}
// use this: randomtext("path/to/file.txt");
?>It's happening because array's are zero based, but count() of an array is not. An array with 10 items goes from 0-9, but count(array) will return 10. So anytime the rand function returns 10 for an array with 10 items, you will get that notice. To fix it, just subtract one from count($file):$quote = ereg_replace("\n","",$file[rand(0,count($file)-1)]);makes perfect sense, thank you!no, that NOTICE means you used a variable before it was defined. it is best to define those before you use them. or you can turn them off in the ini file or in code.but because he references the array index of 10 it classes that as the undefined variable. Therefore you are both correct but IKLOP's solution would solve the problem I believe.I have never seen a undefined variable on an array like that. usually when you get "Notice: Undefined offset:" is means $offset is the variable in mention.
where is the 16 coming in at?scoutt, if its an undefined variable, the error will say "undefined variable: variable_name"
undefined offset is an error that means you are trying to access an array value that doesnt exist. so if its an array with three values in it, and you try to access the 5th item in the array, it will give you an undefined offset error.well that makes sense. I must never had come up on that as I don't make that error
see, I am still learning, but I have to make mistakes to see those kind of errors, lol
thanks N8yeah, it doesn't do that now. thanks guys!
Notice: Undefined offset: 16 in W:\www\newvat\functions\randomtext.php on line 29
here's the code:
function randomtext($filename) {
$file = file($filename);
srand((double)microtime()*1000000);
while (empty($quote)) {
// offending line follows
$quote = ereg_replace("\n","",$file[rand(0,count($file))]);
}
echo $quote;
}
// use this: randomtext("path/to/file.txt");
?>It's happening because array's are zero based, but count() of an array is not. An array with 10 items goes from 0-9, but count(array) will return 10. So anytime the rand function returns 10 for an array with 10 items, you will get that notice. To fix it, just subtract one from count($file):$quote = ereg_replace("\n","",$file[rand(0,count($file)-1)]);makes perfect sense, thank you!no, that NOTICE means you used a variable before it was defined. it is best to define those before you use them. or you can turn them off in the ini file or in code.but because he references the array index of 10 it classes that as the undefined variable. Therefore you are both correct but IKLOP's solution would solve the problem I believe.I have never seen a undefined variable on an array like that. usually when you get "Notice: Undefined offset:" is means $offset is the variable in mention.
where is the 16 coming in at?scoutt, if its an undefined variable, the error will say "undefined variable: variable_name"
undefined offset is an error that means you are trying to access an array value that doesnt exist. so if its an array with three values in it, and you try to access the 5th item in the array, it will give you an undefined offset error.well that makes sense. I must never had come up on that as I don't make that error
see, I am still learning, but I have to make mistakes to see those kind of errors, lol
thanks N8yeah, it doesn't do that now. thanks guys!