I'm getting an undefined index notice, though the index is defined. PHP version is 5.6
Code:
$url = $_SERVER['REQUEST_URI'];
$array = explode('?', $url);
var_dump($array);
echo "<br>".$array[0]."<br>";
echo "this is value 1:".$array[1]."<br>"; //line 9
Output:
array(2) { [0]=> string(10) "/customers" [1]=> string(9) "name=test" }
/customers
this is value 1:name=test
This all makes sense, but I also get this notice:
PHP Notice: Undefined offset: 1 in
/var/source/united/magebo/api/api.php on line 9
Any idea where this notice is coming from?
I tried setting the url hard coded to the same value, the notice disappears. I found allot of questions about this issue but in my case the index IS defined.
Related
I'm using php 7.2.25
I don't understand the following code:
$x = [];
var_dump($x);
var_dump($x['X']);
$y = null;
var_dump($y);
var_dump($y['Y']);
output:
array(0) {
}
Notice: Undefined index: X on line
NULL
NULL
NULL
Why accessing $y['Y'] is just null without any notice?
How to receive any notice here?
Link: http://sandbox.onlinephpfunctions.com/code/2855ea8caca485174a84fd8d268b02725493392f
I have the following php code that is looking for the last entry of test entry in a mysql query result.
It checks if the last entry is valid for a device or it tries to search the latest one in the for that type of test (or failing that leave it as untested). After that, it does the same for the second device in the same manner. However I get errors pointing to lines on second foreach loop.
if ($device1_valid) {
$Results_d1 = $History[$TestNo][$iter]['Results_d1'];
$Colour_d1 = Colour($Results_d1);
$Date_d1 = $History[$TestNo][$iter]['Date_'];
} else {
foreach ($History[$TestNo]['iter'] as $item) {
$device1_valid = $History[$TestNo][$item]['d1_valid'];
if ($sf1_valid) {
$Results_d1 = $History[$TestNo][$item]['Results_d1'];
$Colour_d1 = Colour($Results_d1);
$Date_d1 = $History[$TestNo][$item]['Date_'];
break;
} else {
$Results_d1 = "----";
$DateTime_d1 ="----";
$Colour_d1 = 'white';
}
}
}
unset($item);
if ($device2_valid) {
$Results_d2 = $History[$TestNo][$iter]['Results_d2'];
$Colour_d2 = Colour($Results_d2);
$Results_d2 = $History[$TestNo][$iter]['Results_d2'];
$Date_d2 = $History[$TestNo][$iter]['Date_'];
} else {
foreach ($History[$TestNo]['EntryNo'] as $item) {
$device2_valid = $History[$TestNo][$item]['d2_valid'];
if ($device2_valid) {
$Results_d2 = $History[$TestNo][$item]['Results_d2'];
$Colour_d2 = Colour($Results_d2);
$Date_d2 = $History[$TestNo][$item]['Date_'];
break;
} else {
$Results_d2 = "----";
$DateTime_d2 ="----";
$Colour_d2 = 'white';
}
}
This results in warnings for the second loop as such:
Notice: Undefined index: EntryNo in /server/filename.php on line 129
Warning: Invalid argument supplied for foreach() in /server/filename.php on line 129
Why is this error occurring and how will I be able to remove it? The query does result in correct data (which is displayed later but I don't understand why theses notifications and warning are happening. This only happens in the second foreach loop and not the first.
Edit:
$History[$TestNo] is a multidimensional array.... so vardump gives array(49) { [0]=> array(25) {....} [1]=> array(25) [2]=> array(25){...} etc. I call this function setting $EntryNo to 0.
vardump $History[$TestNo][$EntryNo] simply gives array(25) {....}
There are no warnings in the first loop but second loop it says the index is undefined. This is key reason why the other question identified as duplicate does not address my issue. The question is why is this occuring in the second foreach loop and how can I avoid this.
For
`Notice: Undefined index: EntryNo in /server/filename.php on line 129
Warning: Invalid argument supplied for foreach() in /server/filename.php on line 129'
It must be like this at foreach($History[$TestNo]['EntryNo'] as $item) There is no element in array $History[$TestNo] with with key EntryNo.
Would you please var_dump($History[$TestNo]) and check it ?
Notice: Undefined variable: Colour_sf2 in /server/filename.php on line 184
For this, You have not included enough code here but it must be because you have not defined $Colour_sf2 before use it in any function or condition.
I have met this Notice while programming php with CodeIgniter
code like this.Its used for showing line and filename while logging.
I tried this
//get debug messages such as functionname, linenum,etc.
if ($level == 'debug') {
$debug_info = debug_backtrace(!DEBUG_BACKTRACE_PROVIDE_OBJECT
&& DEBUG_BACKTRACE_IGNORE_ARGS);
$debug_info = $debug_info[1];
$debug_message = '('.
isset($debug_info['file'])?$debug_info['file']:''.
isset($debug_info['class'])?$debug_info['class']:''.
isset($debug_info['type'])?$debug_info['type']:''.
isset($debug_info['function'])?$debug_info['function']:''.
isset($debug_info['line'])?$debug_info['line']:''.
')';
$message = $debug_message.$message;
}
and use array_key_exist() to make the judge ,but it still cause NOTICE like this
A PHP Error was encountered
Severity: Notice
Message: Undefined index: file
Filename: core/Common.php
Line Number: 364
Many thanks for answering the question
here is the dumped data
array(4) {
["function"]=>
string(5) "index"
["class"]=>
string(7) "Welcome"
["type"]=>
string(2) "->"
["args"]=>
array(0) {
}
}
'(' . isset(...) will always evaluate to true, hence $debug_info['file'] will always be evaluated whether it's set or not. The problem is the way you're chaining the conditions and operations. You need to delimit and group them explicitly, otherwise it doesn't do what you think it does.
'(' . (isset(..) ? 'foo' : 'bar') . ( .. ? .. : .. ) . ...
I experience something strange with an undefined index..
$vatcode = 'U25';
echo $this->vatcode_ids['U25']."\n";
echo $this->vatcode_ids[$vatcode]."\n";
foreach($this->vatcode_account_ids as $id => $vatcode){
echo $vatcode."\n";
echo $this->vatcode_ids[$vatcode]; // undefined index
}
this returns:
681
681
U25
Notice: Undefined index: U25 in /var/www/.....php on line 64
I don't get it?!
From empty line printed before Notice massage i assume your $vatcode variable contains some ending new line character. If so it does not match any key in $this->vatcode_ids array.
You should use some trimming function as Dan Lee suggested in comments.
Calling "index.php?pConta=1&pDataInicial=01-01-2000&pDataFinal=31-12-2000" I get this notices:
[08-Oct-2009 17:30:35] PHP Notice: Undefined index: pConta in index.php on line 1
[08-Oct-2009 17:30:35] PHP Notice: Undefined index: pDataInicial in index.php on line 2
[08-Oct-2009 17:30:35] PHP Notice: Undefined index: pDataFinal in index.php on line 3
index.php source:
<?php
$conta = $_GET['pConta'];
$dtIni = $_GET['pDataInicial'];
$dtFin = $_GET['pDataFinal'];
?>
And the "var_dump($_GET)" shows me following:
array(3) { ["pConta"]=> string(1) "1" ["pDataInicial"]=> string(10) "01-01-2000" ["pDataFinal"]=> string(10) "31-12-2000" }
And $conta, $dtIni and $dtFin have excepted values.
I found the problem! I'm using xajax (an ajax php library) and it was calling the url without params.