I am getting this PHP error, what does it mean?
Notice: Undefined offset: 0 in C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41
From this code:
I am getting this PHP error, what does it mean?
Notice: Undefined offset: 0 in C:\xampp\htdocs\mywebsite\reddit_vote_tut\src\votes.php on line 41
From this code:
You are asking for the value at key 0
of $votes
. It is an array that does not contain that key.
The array $votes
is not set, so when PHP is trying to access the key 0
of the array, it encounters an undefined offset for [0] and [1] and throws the error.
If you have an array:
$new_array = array('1','2','3');
We can now access:
$new_array[0]; $new_array[1]; $new_array[2];
If we try and access:
$new_array[3];
We will get the error "Notice: Undefined offset: 3"
Use print_r($votes);
to inspect the array $votes
, you will see that key 0
does not exist there. It will return NULL and throw that error.
getAllVotes()
isn't returning an array with the indexes 0
or 1
. Make sure it's returning the data you want by calling var_dump()
on the result.
first, check that the array actually exists, you could try something like
if (isset($$votes)) { // Do bad things to the votes array }
This answer helped me https://stackoverflow.com/a/18880670/1821607 The reason of crush ― index 0 wasn't set. Simple $array = $array + array(null)
did the trick. Or you should check whether array element on index 0 is set via isset($array[0])
. The second variant is the best approach for me.
As explained it happens because there is no data in the $cur_votes[0] and hence it throws an error. To ensure your code works fine, before performing "$votes_up = $cur_votes[0]+1;" echo the $cur_votes[0] value to see if there is any value stored or not. Surely, there is no value stored.
function getEffectiveVotes($id)
According to the function header, there is only one parameter variable ($id
). Thus, on line 27, the votes[]
array is undefined and out of scope. You need to add another parameter value to the function header so that function getEffectiveVotes()
knows to expect two parameters. I'm rusty, but something like this would work.
function getEffectiveVotes($id, $votes)
I'm not saying this is how it should be done, but you might want to research how PHP passes its arrays and decide if you need to explicitly state to pass it by reference
function getEffectiveVotes($id &$votes)
Lastly, call function getEffectiveVotes()
with both arguments wherever it is supposed to be called.
Cheers.
If you leave out the brackets then PHP will assign the keys by default.
Try this:
$votes = $row['votes_up']; $votes = $row['votes_down'];
As you might have already about knew the error. This is due to trying to access the empty array or trying to access the value of empty key of array. In my project, I am dealing with this error with counting the array and displaying result.
You can do it like this:
if(count($votes) == '0'){ echo 'Sorry, no votes are available at the moment.'; } else{ //do the stuff with votes }
count($votes)
counts the $votes
array. If it is equal to zero (0)
, you can display your custom message or redirect to certain page else you can do stuff with $votes
. In this way you can remove the Notice: Undefined offset: 0
in notice in PHP.
its just a warning use:
error_reporting(0);
it shows when we do not initialize array and direct assigning value to indexes.
somefunction{ $raja[0]="this"; $raja[1]="that"; }
instead :
somefunction{ $raja=array(0=>'this',1='that'); //or $raja=array("this","that"); }
it just notification, do not generate any output error or any unexpected output.