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
Related
I don't know if it is a bug or what but here it is. I var_dump my
variable status_id it says:
var_dump($status_id);
string(4) "null"
but when I var_dump it says:
var_dump($status_id != NULL);
bool(true)
How can I transform it to null again because I need it to my filter?
You $status_id is a string 'null', not null or NULL. Check the live demo for a good understanding.
You can compare it with $status_id == 'null'
or set it to null with $status_id = $status_id == 'null' ? null : $status_id;
Your variable $status_id is not null, it has a string value as "null". Check this out:
<?php
$val="null";
var_dump($val);
// Output: string(4) "null"
$val2;
var_dump($val2);
// Output: NULL
Just this var_dump($status_id != NULL); will trigger an PHP Notice Error
Notice: Undefined variable: status_id
Better to use
if( !isset($status_id) ) {}
Or if it is set, but without value
if( !empty($status_id) ) {}
The PHP var_dump() function returns the data type and value:
so you are getting string value "null" not NULL
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'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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
I got error:
Notice: Undefined variable: null
Line in code is:
$input = new $class($company, null, $sablonas, $null, $value);
Class constructor is:
public function __construct($company, $document, $sablonas, $options, $value = null) {
How I can pass a null value?
$input = new $class($company, null, $sablonas, $null, $value);
// ^ ^
// (1) (2)
It's talking about (2), not (1). You have a typo with $null.
The notice message "Undefined variable: null" is a little misleading here, but consider the following case:
<?php
error_reporting(E_ALL | E_NOTICE);
echo $lol;
// Output: "Notice: Undefined variable: lol in /t.php on line 3"
?>
You can see that the $ isn't included in the name that the notice message gives you, so if you follow this logic back you arrive at the conclusion I made at the top of this answer.
You have $null as a variable:
$input = new $class($company, null, $sablonas, $null, $value);
//------------------------------------------^^^^^^^^^^
// Guessing that's supposed to be
$input = new $class($company, null, $sablonas, null, $value);
//-------------------------------------------^^^^^^^^
$null = NULL;
I've got an array, but when I try to use it, I get the Undefined variable notice.
Here's the relevant lines:
$varEvents = array();
...
if ($selectedResult) {
while ($row = mysql_fetch_assoc($selectedResult)) {
array_push($varEvents, $row['eventID']);
}
mysql_free_result($selectedResult);
}
...
print_r($varEvents);
if (is_array($varEvents)) {
if (count($varEvents) > 0) {
if (in_array($id, $varEvents)) {
$varRegistered = 1;
}
}
unset($varEvents);
}
and the result shows as:
Array ( [0] => 4 )
Notice: Undefined variable: varEvents in /home/.../www/registration.php on line 143
Notice: Undefined variable: varEvents in /home/.../www/registration.php on line 145
line 143: print_r($varEvents);
line 145: if (is_array($varEvents)) {
All relevant lines are in the same loop and I do get most of the results I expect, except $varRegistered never changes to 1 and that messes up my result.
It is most likely because of this line:
unset($varEvents);
You are unsetting the variable within the loop and next iterations don't find it again.