Why do I get undefined index while using simple array function - php

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

Related

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')

Difficulties with is_int function in PHP

I'm writing a code to solve a problem.one of my functions is not working properly.
function check_factor($sqr,$num)
{
for($i = $sqr ; true ; $i++)
{
$n = pow($sqr,2) - $num;
$s = sqrt($n);
if(is_int($s))
{
return $i;
}
}
}
I know that $s is a "double" , but even when I limit my loop counter to 2,I'll get an endless loop.
What am I missing here? why the function doesnt simply return null? and why I get Infinite loop even when there are 2 iterations?
true is making it an infinite loop
you will always get an infinit loop because sqrt() returns float means
$s = sqrt($n);
$s is a float now
and your test is testing if $s is an integer so it will be an infinit loop even after $i=2 the loop will always stuck and the $i=2 in every loop but
if you change the code to this
<?php
function check_factor($sqr,$num)
{
echo "<br>";
for($i = 1 ; $i < 3 ; $i++)
{
echo " in the loop $i<br>";
$n = pow($sqr,2) - $num;
echo "$n<br>";
$s = sqrt($n);
echo "$s<br>";
if(is_int($s))
{
echo "in the if <br>";
return $i;
}
}
return 0;
}
$val=0;
$val = check_factor(5,2);
echo "<br>$val<br>";
?>
the out put should be like this
in the loop 1
23
4.7958315233127
in the loop 2
23
4.7958315233127
0
and that's it. i hope i helped.
According to the manual: PHP function sqrt() always returns a float and NEVER an integer.
Check http://php.net/manual/en/function.sqrt.php

Basic PHP clarification for variables on how executed/saved

I have the follow:
$sum = 10 + 10;
is the above line executed saved to $sum as 20 to use it from now on
or if everytime i echo $sum, it will run the 10+10?
It only calculates it once to set the value of $sum.
So from then on out $sum is equal to 20.
So the 10+10 is only calculated the first time.
During the lifetime of the script the value of 10 + 10 will be assigned to the $sum variable - no further 10 + 10 will be calculated when using $sum.
PHP is not inherently lazy nor it has lazy primitives, so value assignation is executed immediately. To simulate some sort of lazy functionality you can declare a function:
funciton sumTen() {
return 10 + 10;
}
sumTen() // will calculate the value every time.
$sum = 0;
echo $sum; // will print 0
$sum = 10 + 10;
echo $sum; // will print 20
$sum = 5;
echo $sum; // will print 5

How can I get the next letter alphabetically without incrementing? (php)

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.

Categories