php undefined offset 150 error in for loop - php

I keep getting an undefined offset 150 error, but i am not sure what it means or what i should do to debug it. Do to the error line i believe that it has something to do with my for loop.
// Get Datafile
$MyData = file("./tmp/test.txt");
// Get Column Headers
$ColHeads = explode(" ", $MyData[1]);
unset($MyData[1]);
$LastHeader = "";
for ($i = 0;$i <= count($ColHeads);$i++) {
if ($ColHeads[$i] == $LastHeader) { //<---this is the line that errors
$ColHeads[$i] = $ColHeads[$i] . "1";
}
$LastHeader = $ColHeads[$i];
}
Would anyone have any ideas as to where I am going wrong?
and the error is:
Undefined offset: 150
I am sorry if this is vague. I am not familiar with php and not sure where to start on debugging this... any help would be much appreciated! Thank you!

Change your for loop:
for ($i = 0;$i < count($ColHeads);$i++) {
The problem is that on the last iteration of the loop, when $i == count($ColHeads), that's going to set $i too high for the number of items in $ColHeads.
You started off right by setting $i = 0. If $ColHeads has 5 items in it, those items have indexes from 0 to 4. Your for loop, as it is, goes up to 5 - and there is no $Colheads[5], so the error is thrown.

Array index starts from zero. And ends at Length-1. So it should be:
for ($i = 0;$i < count($ColHeads);$i++) {

Related

Migration to PHP8

I have code from old PHP.
But when I tried to execute it by PHP 8.
The first code was:
PasteBin
I had error:
Fatal error: Array and string offset access syntax with curly braces
is no longer supported in **** on line 550
On line:
for ($i = 0; $i < strlen($text); $i++) $res .= ord($text{$i}) . "-";
I changed it to:
for ($i = 0; $i < strlen($text); $i++) $res .= ord($text[$i]) . "-";
But I had another error:
Warning: Trying to access array offset on value of type bool in ***
on line 76
On line:
$real = $row['sip'];
I have no idea - how to rewrite this string.
Can you help me?
the problem is that you are trying to access a boolean value as you do with an array.
i imagine that $row is the result of a query, and that query does not return any matching rows, so it's false.
just check if $row is false before you access it.
<?php
$row = false;
echo $row['test'];
this returns that warning.
as per your comment, it depends on what you wanna do.
if it exist return the values, if it doesn't?
if($row){
// if it contains something, do something with it
}else{
// do something else if it doesn't
}
i don't know what's the flow of your code, so I can't really help you, it's just a check to see if the $row variable is not false

How can I fix this error "Undefined offset: " at Regex?

I get this Error for every row in the data. So around 500 times, but every time with an other Undefined offset.
Heres my Code:
$fl_array = preg_grep('/^\d+\s('. $SelectedTime .':)\d+/', explode("\n", $finalLog));
$count = count($fl_array);
for ($x = 0; $x < $count; $x++)
{
echo "$fl_array[$x] \n";
}
as written here - http://php.net/manual/en/function.preg-grep.php: Returns an array indexed using the keys from the input array. so maybe smth wrong with your keys:
if (isset($fl_array[$x])) {
echo "$fl_array[$x] \n";
}

Why does this function generate an uninitialized string offset notice?

I'm having this error when I run my program:
Notice: Uninitialized string offset: 7 in C:\xampp\htdocs\demo\str_rev.php on line 21
What causes that?
<?php
//strrev($arg);
/*$str = "ademola";
echo strrev("$str");
*/
function reverse_String($str){
$i = 0;
while(!empty($str[$i])){
echo $str[$i];
$i++;
}
for($r = $i; $r > -1; $r--){
$reverse = $str[$r];
echo $reverse;
}
}
reverse_String("Ademola");
?>
Output:
Ademola
Notice: Uninitialized string offset: 7 in C:\xampp\htdocs\demo\str_rev.php on line 21
alomedA
The $i++; in your first while loop increments $i to 7 in its last iteration. The condition !empty($str[$i]) is no longer satisfied, so the loop does not execute again, but $i is still 7 when the next loop starts, which is an index beyond the end of the string.
There are various ways to fix this, a simple way is to subtract 1 from the counter when you define your second loop to set $r to the index of the last character in the string.
for($r = $i - 1; $r > -1; $r--){ ...
As mentioned by Don't Panic there are many ways to fix this,
you can use isset as:-
for($r = $i; $r > -1; $r--){
if(isset($str[$r])) {
$reverse = $str[$r];
echo $reverse;
}
}
Or to reverse the string you can simply use the php's built in function (strrev)
echo strrev('Ademola')

I am getting Undefined offset error php

I am trying to split the Content that is fetching from DB a few words next to image and a remaining content in the below paragraph
I am getting undefined offset error : 41
the below is the code... it executes correctly but with notice error as above... Can you please help me to fix it.
<div class="contentfont" style="padding-left:20px; text-align:left;">
<?php
$words = explode(" ",$dcontent);
$cntWords = count($words);
$splitWords = round($cntWords/2);
for ($i=0;$i<=$splitWords;$i++)
{
print $words[$i].' ';
$halfPoint = $i;
}
?>
<p class="contentfont" style="padding-top:10px;">
<?php
for ($i=$halfPoint;$i<=$cntWords;$i++)
{
print $words[$i].' ';// This print statement is causing me this error Notice: Undefined offset: 41 in D:\xampp\htdocs\training\admin-panel\php\sai_devotees_detail.php on line 106
}
?>
</p>
Undefined offset in this case means you are reading past the length of the $words array. Try $cntWords - 1 for ceiling of the loop like this:
for ($i = $halfway; $i <= $cntWords -1; $i++) {
$halfPoint = $i; is also outside the loop. Try this instead:
for ($i = 0; $i <= $splitWords; $i++) {
print $words[$i] . ' ';
$halfPoint = $i;
}
When you make a loop in PHP without brackets, only the first statement after it will be executed. So in this case, $i doesn't exist for you to assign it to $halfPoint. Don't think it's clever to compact everything into one line. It will only cause you problems.

PHP post method inside for loop causes infinite loop

i have a set of fields say field1, field2,... field10.
i pass this value from one page to another and get the value using post method inside a for loop as follows..
for ( $i = 1; $i <= 10; $i++) {
$txtfield.$i = $_POST[field.$i];
echo $txtfield.$i;
}
This makes an infinite loop printing the value of field1 continuously..
Finally this error occurs..
Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\...
what is the mistake in this code???
I don't know what is the value "field", shouldn't it be a variable with a $?
Anyway, I think you wanted to do this:
for ( $i = 1; $i <= 10; $i ++) {
$varname = ($txtfield . $i);
$$varname = $_POST[field.$i];
echo $$varname;
}
You're reassigning $i here each time to $_POST[field.$i]
$txtfield.$i = $_POST[field.$i]
Kind of like this, with the second line being a non-expression:
$i = $_POST[field.$i];
$txtfield.$i;

Categories