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')
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 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
$a = "Hello World I am Neha Singh Chouhan";
$length = 0;
do {
$length++;
}
while ($a[$length] != null);
echo "Length = ".$length."<br/>";
for ($i = $length - 1; $i >= 0; $i--) {
echo $a[$i];
}
?>
tried to calculate the string length without using inbuilt function strlen().
the output was
Notice: Uninitialized string offset: 35 in C:\xampp\htdocs\Neha\ProgramsFromClass\10_reverse_string.php on line 7
Length = 35
nahuohC hgniS aheN ma I dlroW olleH
What causes the notice and how can i get rid of it?
Your loop is incorrect. You increment your $length BEFORE you start using it, and run off the end of the string. Therefore you're testing offsets, 1,2,3,...n, where n is one past the end of the string.
And since you're testing for a non-existent "array" key, no matter how you look at it, you'll always get the error, even after you swap the loop types.
do/while is NOT the loop to use for this. You want a plain while loop, and use isset() instead:
$length = 0;
while(isset($a[$length])) { $length++; }
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
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.