This code below has Undefined offset error on line 5.
I don't know why this appears, I'm fighting with this for about an hour.
It says it's in a line where for is, but as I see syntax is correct :/
<?php
function palindrom($broj) {
$brojniz=str_split($broj);
for ($x=0; $x<3; $x++) {
if ($brojniz[$x] != $brojniz[5-$x]) {return;}
}
return($broj);
}
$n=100;
$m=$n;
while ($n<1000) {
while ($m<1000) {
$br=$m*$n;
palindrom($br);
++$m;
}
$m=100;
++$n;
}
?>
but as I see syntax is correct
Yes, the syntax is correct. But the runtime values are not. This syntax is also correct, but will produce an error:
$x = 1 / 0;
The line in question is indexing an array:
if ($brojniz[$x] != $brojniz[5-$x])
And the value $x goes from 0-2 in that loop. So you're indexing as such:
if ($brojniz[0] != $brojniz[5])
if ($brojniz[1] != $brojniz[4])
if ($brojniz[2] != $brojniz[3])
Does that array go from 0-5? If not, then you're referencing an undefined index.
Related
I cleared the $_SESSION using session_unset()
now my program isn't working. Please help
if (isset($_POST['Add_To_Cart'])) {
if(!isset($_SESSION['cartridge_scale'])){
$_SESSION['cartridge_scale'] = array();
}
if(($_SESSION['cartridge_scale']+findItemHeight()) <= 3){
more code etc etc
{
Now I get a
Fatal error: Uncaught Error: Unsupported operand types
this error happens on the line
if(($_SESSION['cartridge_scale']+findItemHeight()) <= 3)
Again, I recently used session_unset() to clear the $_SESSION but I don't think that would cause the program to crash.
Change the array to integer
if(!isset($_SESSION['cartridge_scale'])){
$_SESSION['cartridge_scale'] = 0;
}
because you cannot add a number with array
if(($_SESSION['cartridge_scale']+findItemHeight()) <= 3)
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 created a function to see if a lat/lon are inside a polygon. I'm getting these notice: undefined variable: xx.xx but its an array of lat/lons being passed and its the value that is undefined not the array. I'm confused help please. The line is the second / the last if statement
public function checkCoordinates($lat, $lon, $polylat, $polylon) {
$j = count($polylat)-1;//number of sides and -1 because its an array
$result = true;
for($i=0;$i<count($polylat);$i++){
if($polylat[$i]<$lat && $polylat[$j]>=$lat
|| $polylat[$j]<$lat && $polylat[$i]>=$lat){ //if the latitude at the beggining is bigger than and at the end is smaller than or vise versa
if($polylon[$i]+($lat-$polylat[$i])/($$polylat[$j]-$polylat[$i])*($polylon[$j]-$polylon[$i])<$lon){ //
$result = false;
}
}
$j=$i;
}
return $result;
}
I didn't notice the extra $, that explains it
Showing error as undefined index cnt.
please help me regarding this issue.thanks in adcance
if($_REQUEST["cnt"]!=""){
$count=$_REQUEST["cnt"];
$cntprev=$count-2;
}else
{
$count=1;
}
You have to check if the index is set. Actually you just check if it is an empty string. But you have to check if it is set, not empty and the value is numeric (if you want to cat it to int float ...). This should work:
if(isset($_REQUEST["cnt"]) && !empty($_REQUEST["cnt"]) && is_numeric($_REQUEST["cnt"])) {
$count=$_REQUEST["cnt"];
$cntprev=$count-2;
} else {
$count=1;
}
I am trying to check array using another loop.
for( $i=0;$i<count($allCheckBoxId);$i++ ) {
if( $allCheckBoxId[$i] != ''){
unset( $contactlist[$cntctnum] );
}
}
I have named $allCheckBoxId, having contactnumber as value. Second array I have named $contactlist having contactnumber as key element.
For particular condition I am retrieving values of first array. Means I would have contactnumber as value retrieve in first array. IF it is not null I am unsetting second element with value contactnumber. but its giving me error on unset( $contactlist[$cntctnum] ); as Illegal offset type in unset in
Here comes interesting part.
You know, programming is not just writing the code.
Most of time programming is looking for errors. Not by asking questions on stackoverflow, but by amending your code and studying it's output and error messages. Some sort of investigation.
If you have got such an error message, doesn't that mean that somewhing wrong with offset type? Why not to print the problem variable out? just print it out:
for( $i=0;$i<count($allCheckBoxId);$i++ ) {
var_dump($cntctnum);
var_dump($allCheckBoxId[$i]);
var_dump($contactlist[$cntctnum]);
if( $allCheckBoxId[$i] != ''){
unset( $contactlist[$cntctnum] );
}
}
and see what's particularly wrong with your offset
Try casting your key into a string. Replace:
$contactlist[$cntctnum]
With
$contactlist[(string) $cntctnum]
OR
for($i = 0; $i < count($allCheckBoxId); $i++) {
if($allCheckBoxId[$i] != '') {
$key = (string) $cntctnum;
unset( $contactlist[$key] );
}
}
PHP associative arrays, as of PHP 5.4 will issue a PHP Warning: Illegal Offset Type if you use something other than a string as a key.
Furthermore, if this doesn't help, head over to the PHP Array Manual and do a Ctrl/Cmd + F for "Illegal Offset Type."