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";
}
Related
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 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.
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++) {
I'm trying to divide in PHP, but I don't get the correct answer when I echo it out.
<?php
$names = file('rating.txt');
// To check the number of lines
echo "In the textfile: " . count($names).'<br>';
$sum = 0;
foreach($names as $name)
{
$sum = $sum + $name;
}
echo "Total sum: " . $sum;
?>
I tried:
($sum/$name)
But that gives me the wrong result. If I try:
($sum/$names)
I get the following error message:
Fatal error: Unsupported operand types in C:\xampp\htdocs\lab5\rating\index.php on line 55
How do I get the $sum divided by the number of lines I have in the text file?
Change $sum = $sum + $name; to $sum .= intval($name);. intval converts the string into a number.
If this does not work, the problem is with rating.txt, which you are not showing. ($sum/$names) won't work because you can only divide a number by a another number (Which should be non-zero).
This is my example array:
$arrayy[0]=48.72;
$arrayy[1]=21.32;
$arrayy[2]=48.62;
$arrayy[3]=21.31;
$arrayy[4]=48.62;
$arrayy[5]=21.31;
This function
function writeDouble($array){
for($curr = 0; $curr<count($array)-1; $curr++){
echo $array[$curr]." - ";
echo $array[$curr+1]."<br>";
$curr++;
}
}
should write a couples (0-1 , 2-3 , 4-5) - an output like:
48.72 - 21.32
48.62 - 21.31
48.62 - 21.31
What am I doing wrong, why do I got an error?
Notice: Undefined offset: 6 in C:\xampp\htdocs\xampp\lg\functions.php on line 466
Or could you define a better function to make couples? I can't think anymore... thanks
Because in the last iteration in line echo $array[$curr+1]."<br>"; you'll be looking for $array[count($array)] which is ofcource not defined!!
You're using $array[$curr + 1] but you're iterating from 0 to $curr - 1. You need an isset in case you have an odd number of values in your array.
You're incrementing 2 times (one time in your for, one time in the scope of your for).
Code solution:
$arrayy[0]=48.72;
$arrayy[1]=21.32;
$arrayy[2]=48.62;
$arrayy[3]=21.31;
$arrayy[4]=48.62;
$arrayy[5]=21.31;
function writeDouble($array) {
for ($curr = 0; $curr < (count($array) - 1); $curr += 2) {
echo $array[$curr] . " - ";
if (isset($array[$curr + 1])) {
echo $array[$curr + 1];
}
echo "<br>";
}
}
writeDouble($arrayy);
Output:
48.72 - 21.32
48.62 - 21.31
48.62 - 21.31
No more warning.
Note that you are incrementing $curr two times:
for($curr = 0; $curr<count($array)-1; $curr++){
and
$curr++
This is the reason for going out of range in your loop