I'm not sure why I am getting an Undefined Offset Notice on this:
<?php
$numbers = array('1','2','3');
$total = 0;
for($i=0;$i<=sizeof($numbers); $i++) {
$total += $numbers[$i];
echo $total;
}
?>
Output:
136
Notice: Undefined offset: 3 in E:\php\arrays\array_1.php on line 17
6
Your array has three elements at index 0, 1 and 2. There is no element with index 3.
Your loop should stop before it hits that...
for($i=0;$i<sizeof($numbers); $i++) {
}
Also, checkout array_sum, which might be what you're wanting anyway...
$total=array_sum($numbers);
You should loop to < the size of the array, not <=.
for($i=0;$i<sizeof($numbers); $i++) {
Change your condition from <= to <.
This will add properly:
$total += intval($numbers[$i]);
turnoff html errors
error_reporting(E_ALL);
ini_set('display_errors', 'On');
ini_set('html_errors', 'Off');
Related
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";
}
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++) {
for ($i=A;$i<L;$i++){
echo $i;
echo '->';
echo ++$i;
echo ', ';
}
gives me:
A->B, C->D, E->F, G->H, I->J, K->L
what I want is:
A->B, B->C, C->D, D->E, E->F, F->G
What's the simplest way to do this?
Simple:
for ($i=A;$i<L;){ // remove loop increment
echo $i;
echo '->';
echo ++$i;
echo ', ';
}
Use range() to get the alphabet as an array then use a proper int i++ incrementation.
How about just copying the value before incrementing it:
for ($i = 'A'; $i < 'L'; $i++) {
$j = $i;
$j++;
echo "$i->$j, ";
}
Ps. You really should quote your string constants. Otherwise your logs will be full of warnings like these:
PHP Notice: Use of undefined constant A - assumed 'A' in - on line 2
PHP Notice: Use of undefined constant L - assumed 'L' in - on line 2
As Julien mentioned, range is sexy for this:
$range = range('A', 'L');
// Had to subtract one from loop iteration total, otherwise the $i + 1
// would throw an undefined index notice
for ($i = 0, $count = count($range); $i < ($count - 1); $i++) {
echo sprintf('%s->%s,', $range[$i], $range[($i + 1)]);
}
More info on range.