PHP - Inconsistency with "Undefined offset" error [duplicate] - php

This question already has answers here:
php array access on boolean
(3 answers)
Closed 5 years ago.
Why does PHP throw a "Notice: Undefined offset" error here:
<?php
$MyVar = array();
echo $MyVar[0]; //Notice: Undefined offset: 0
?>
But not here:
<?php
$MyVar = false;
echo $MyVar[0]; //No error
?>

It's ultimately because in your 2nd example $MyVar[0] is null which isn't an error. You could probably reference $MyVar[0][1][2][3] and get the same result.
The first example isn't null it's a missing index in an array so it warns you.

Undefined offset is provided when there are no values in an array and you're trying to reference an uninitialised index.
In case of the 2nd code, you've assigned(initialised) a value to the variable which is not different from variable[0].
If you assign values for 1st two indexes in 1st example, and refer to the 3rd index, you should get the undefined offset error.
I dont think it is about null or not, but a case of assigning values to indexes.
If you take a C parlance, array is an equivalent of malloc(upto a certain extent). Thankfully PHP does not crash, just throws an undefined index[offset] error

Related

How to fix error of Undefined offset: 2 using list explode [duplicate]

This question already has answers here:
PHP: Undefined offset
(4 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
list($s1,$s2,$s3)=explode("-",$EndDate);
It is showing:-
Undefined offset: 2 in
C:\xampp\htdocs\finishingbrands_new\include\checkReportDates.php on
line 3
There are three parameters in the list($s1, $s2, $s3), but the $EndDate could be exploded into two array. So the first index of the exploded array will be assigned to $s1. Similarly, the second will be assigned to $s2, and the third will be assigned to $s2. As there is no index of 2, php certainly raise a notice: Undefined offset: 2.
You obviously have incorrect data in $EndDate.
The code runs without problems with 2016-06-03
https://3v4l.org/BIJai
But if we remove one part of the date we get the error you say.
2017-06 returns error.
https://3v4l.org/AmbKP

Do I have to check every variable is set before echoing it? [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I am currently in a process of writing my PHP script, and I am doing it with
E_ALL set in php and I can not allow ANY errors, even
PHP Notice: Undefined variable
And I have many places where i ECHO $myvariable without checking if it's empty, which is causing error mentioned above.
Is it just me or it's extremely stupid to do this for every variable that can be undefined:
if(!empty($myvariable)) {
echo $myvariable;
}
Is this only way to avoid these errors?
EDIT:
Question is not a duplicate, what you refereed to as duplicate has nothing to do with what i asked here.
Three possible solutions:
1st: initialize your var at the very beginning of your code (or method, or class, whatever you're doing)...
$var = "";
// Do stuff with $var
echo $var;
2nd: use a ternary operator
echo (isset($var)) ? $var : "";
3rd: use #
#echo($var);
It is a notice which gives you an insight that your code could produce unexpected results since you did not assign a value (constant or calculated).
You can avoid this error by checking if it's set using the isset function or set a value for that variable at the top of your script.

Undefined Variable : kd_user [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I just started coding so i dont know whats wrong with my code
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: kd_user
Filename: views/dokter.php
Line Number: 1
Controller
function read_user()
{
$this->load->model('dokter_model');
$data['datauser']=$this->dokter_model->read_user();
$this->load->view('dokter', $data);
}
Model
function read_user()
{
$q="SELECT a.*, b.*
FROM users a
LEFT JOIN dokter b ON a.kd_user=b.kd_user
WHERE a.kd_user='".$this->session->userdata('kd_user')."'";
$query=$this->db->query($q);
return $query->result();
}
Views
<p align="center">Selamat Datang <?php echo $kd_user;?></p>
Sorry for my poor english
Change :
$data['datauser']=$this->dokter_model->read_user();
to:
$data['kd_user']=$this->dokter_model->read_user();
In general, the error
Message: Undefined variable: kd_user
Means that you are referring to a variable (in this case, $kd_user), and it was never defined (you never initialized it and/or assigned a default value to it.)
Looking at your code, you are referring to kd_user in two places -- once in the associative array user_data in your model, and once in your view as kd_user.
To begin, you may wish to debug in each of those files (most probably the view) if that variable actually exists.
Without seeing all of your app's code, its hard to say for sure, but I'd verify that your view has a variable available called $kd_user for you to access. If it isn't there, you either need to find a way to get it into there, or reference another variable to get to the value you need.

Newbie PHP. Why is this an undefined index? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I've skimmed through docs and this site but still unsure. I'm working with Drupal 7.
Printing the variable with this code:
<?php print $info['comments'] ?>
If there are zero comments I get
Notice: Undefined index as a message.
If it exists, it prints the correct #.
Any help would be appreciated. How do I set it so if there are no comments it displays a zero?
Thanks!
To print an integer no matter what:
<?php print #intval($info['comments']) ?>
Or to irrevocably suppress the notice:
<?php print isset($info['comments']) ? $info['comments'] : 0 ?>
I don't know drupal, and don't know your context exactly, but if you try to access an index that doesn't exists from an array, this kind of notice is displayed.
To avoid such a thing, you can do something like :
if(isset($info['comments'])) {
print $info['comments'];
}
PHP arrays are associative by nature, thus you can have an array composed of all types of keys/values:
arry['stringkey'] = 'somestring';
arry[1] = 'some other string';
etc..
If you were to try and reference an index in the array that is not present without doing any error checking, like so
if(arry[2] == 'some third string')
you'd get an 'undefined index' error. Look into the isset() function for a solution.
You could test if the array has that key:
if (array_key_exists('comments', $info)) {
...
}
But I am not sure if that is what you really want to do.

Notice: Undefined index: submit in sample.php on line 25 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
When do we get this error?
Notice: Undefined index: submit in C:\wamp\www\sample.php on line 25
What is the exact meaning of this perticular error?
This means exactly what it says: you're addressing to an undefined index in an array
$arr = array();
echo $arr['foo'];
In the example above the array is empty but I tried to output 'foo' item value, which doesn't exist.
It means you are trying to access a part of an arraythat isn't there.
If you have an array with 5 elements, you ca get to them via:
$array[0] through to $array[4]
But if you try $array[76] which doesn't exist, you will get an undefined Index error.
You've probably got an array that you're accessing like $_POST['submit']. That error message is saying is the element 'submit' of the array doesn't exist, and it's throwing a warning.
You should check that array elements exist before using them isset() before you access them to avoid avoid the warning.
Edit: possible duplicate of this: Undefined index in PHP
have you correctly mentioned the method in your form GET or POST ?? I think you are accessing/testing it without declaring it. Let me know if this is the case.

Categories