NOTICE Undefined index: file while using isset and array_key_exists() - php

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') . ( .. ? .. : .. ) . ...

Related

How to access a variable in a PHP object structure

I want to parse this MongoDB error message. I can't manage to access the variables in the object. I assume I overlook something trivial, but I can't figure it out.
The var_dump var_dump($error);
object(MongoDB\Driver\WriteError)#11 (4) {
["message"]=>
string(362) "E11000 duplicate key error collection: database.collection index: c_address collation: { locale: "nl", caseLevel: false, caseFirst: "off", strength: 1, numericOrdering: false, alternate: "non-ignorable", maxVariable: "punct", normalization: false, backwards: false, version: "57.1" } dup key: { address: "0x", city: "0x" }"
["code"]=>
int(11000)
["index"]=>
int(0)
["info"]=>
NULL
}
I try to access message and print it out with echo.
echo $error->message;
Throws: Warning: Undefined property: MongoDB\Driver\WriteError::$message
echo $error["message"];
Throws: Uncaught Error: Cannot use object of type MongoDB\Driver\WriteError as array
The class documentation suggestion from El_Vanja did the trick.
MongoDB\Driver\WriteError has a method [getMessage][1].
My first assumption is to check with var_dump what I get back and it's impossible to see if those variables are protected/private or not (and if they have a function).
I did some more research, converting an object to json and echo that, at least shows which variables are public (in this case none).
echo json_encode($error);
Gives: {}.

PHP foreach over an array twice results in some warnings undefined index

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.

Faulty notice undefined index

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.

Why is filter_input returning NULL when the variables exist?

I'm trying to retrieve some GET data and filter it to make sure it contains an integer. The url string is events.php?id=22&month=2, but when I try to get either variable filter_input returns NULL for some reason, I can't figure out why. This is my code:
if(!isset($pageId)) {
$pageId = filter_input(INPUT_GET, 'id', FITER_VALIDATE_INT) ?: 4;
}
if(!isset($month)) {
$month = filter_input(INPUT_GET, 'month', FITER_VALIDATE_INT) ?: date('n');
var_dump($_SERVER['QUERY_STRING'], filter_input(INPUT_GET, 'month', FITER_VALIDATE_INT));
}
The var_dump returns string(13) "id=22&month=2" NULL. I've also tried var_dump($_GET) and get back array(2) { ["id"]=> string(2) "22" ["month"]=> string(1) "2" } as expected.
Why is the filter_input returning NULL? I've never had a problem retrieving GET data this way.
FITER_VALIDATE_INT should be FILTER_VALIDATE_INT. You are missing L in FILTER in all instances of the constant. Turning on error reporting would have thrown several Notice: Use of undefined constant FITER_VALIDATE_INT - assumed 'FITER_VALIDATE_INT' errors.

PHP suppress errors in function parameters

Extension from https://stackoverflow.com/a/55191/547210
I am creating a validating function to check several attributes of string variables, which may or may not have been set. (One of the attributes which is checked)
What I am trying to do with the function is receive arguments an unknown number of arguments in the form (See below), and suppress errors that may be caused by passing an unset variable.
I'm receiving the variables like validate([ mixed $... ] ) by using func_get_args()
The previous post mentioned that it was possible by passing by reference, now is this possible when the variables are passed implicitly like this?
If you pass a variable that is not set in the calling scope, the array returned by func_get_args() will contain a NULL value at the position where the variable was passed, and an error will be triggered. This error is not triggered in the function code itself, but in the function call. There is, therefore, nothing that can be done to suppress this error from within the code of the function.
Consider this:
function accepts_some_args () {
$args = func_get_args();
var_dump($args);
}
$myVar = 'value';
accepts_some_args($notSet, $myVar);
/*
Ouput:
Notice: Undefined variable: notSet in...
array(2) {
[0]=>
NULL
[1]=>
string(5) "value"
}
*/
As you can see, the variable name notSet appears in the error, telling us that the error was triggered in the caller's scope, not that of the callee.
If we want to counter the error, we could do this:
accepts_some_args(#$notSet, $myVar);
...and prefix the variable names with the evil # operator, but a better solution would be to structure our code differently, so we can do the checks ourselves:
function accepts_some_args ($args) {
var_dump($args);
}
$myVar = 'value';
$toPassToFunction = array();
$toPassToFunction[] = (isset($notSet)) ? $notSet : NULL;
$toPassToFunction[] = (isset($myVar)) ? $myVar : NULL;
accepts_some_args($toPassToFunction);
/*
Ouput:
array(2) {
[0]=>
NULL
[1]=>
string(5) "value"
}
*/

Categories