Undefined offset when trying to get the next position in php [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
error message when trying to print the next position in loop?
$coba = "testing";
for($h=1;$h<(count($coba));$h++){
echo $coba[$h+1];
}

If this is your code, you would get that error:
$coba = "testing";
for($h=1;$h<strlen($coba);$h++){
echo $coba[$h+1];
}
The reason is you're trying to print something that don't exist.

You are incrementing $h to a number greater than the array. That's why you are getting the offset error.

Original code is incorrect here --> for($h=1;$h<(**count**($coba));$h++){
See the PHP reference guide for difference in count() and strlen().
count($coba) is 1
strlen($coba) is 7
If you use strlen then the code would correctly loop through the string:
$coba = "testing";
for($h=1;$h<strlen($coba);$h++){
echo $coba[$h+1];
}
Now, regarding the error you mentioned, when I run the above I get:
PHP Notice: Uninitialized string offset: ...
Two problems here with your original code. Since I do not know your original intent, I can only guess at what you were trying to do with the loop.
Problem #1: indexing into the string should start with 0, not 1
Problem #2: $coba[$h+1] will be an invalid index at the END of the array at h+1.
You can either adjust the indexing h+1 to just be h OR
change the loop to loop 1 less via for($h=0;$h<(strlen($coba)-1);$h++){
Thus, final code could look like:
$coba = "testing";
for($h=0;$h<(strlen($coba)-1);$h++){
echo $coba[$h+1];
}
Which outputs when run:
esting

Related

How to declare string variable? [duplicate]

This question already has answers here:
What is the proper way to declare variables in php?
(5 answers)
Closed 9 months ago.
The first appearance to this variable $totpro in my code is this way
$totpro = $totpro + $row['profitloss'];
I want to use it to sum all profits, however, I receive this warning message on running
Warning: Undefined variable $totpro
but if I put this code before the previous code it runs with no problems
$totpro = "0";
I don't like using that code to declare the function, it tried
String $totpro
but unexpectedly it didn't work. Now tell me how to define $totpro without to have to use $totpro = "0";
If you are summing numbers, the initial declaration should set the value to 0 (i.e. a number):
$totpro = 0;
You tried "0", which is a string. Technically this will work, but it is not the best way.

why the following error is showing? [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
the following error is showing in the web page but the code is working..i just want to know why the following error is showing?
Notice: Use of undefined constant food - assumed 'food' in E:\server\htdocs\table\action.php on line 33
enter image description here
the following links are the code source.
https://drive.google.com/open?id=1u59Z0WipMgPEE10KP1c12maoUS-KqhlA
https://drive.google.com/open?id=1ngHyhwOdCbryj7UvZ_8tE2pIqp2uoSmE
Even without the code sample where it is happening I believe you are not using quotations ' or " in your array key names.
All you need is to wrap the key name into the quotations such as:
$your_array['food'];
// or
$your_array["food"];
For example on 10 line $dep = $_POST['dep']; I think you get text value. And on 33 line you have if-else operator if($dep==food){. You need to change your right value food to 'food'. Then you haven't get Notice messages. Because all text values must be in single or double quotes.
Your current syntax (with notice) is: comparing variable $dep with constant food.

PHP - Inconsistency with "Undefined offset" error [duplicate]

This question already has answers here:
php array access on boolean
(3 answers)
Closed 5 years ago.
Why does PHP throw a "Notice: Undefined offset" error here:
<?php
$MyVar = array();
echo $MyVar[0]; //Notice: Undefined offset: 0
?>
But not here:
<?php
$MyVar = false;
echo $MyVar[0]; //No error
?>
It's ultimately because in your 2nd example $MyVar[0] is null which isn't an error. You could probably reference $MyVar[0][1][2][3] and get the same result.
The first example isn't null it's a missing index in an array so it warns you.
Undefined offset is provided when there are no values in an array and you're trying to reference an uninitialised index.
In case of the 2nd code, you've assigned(initialised) a value to the variable which is not different from variable[0].
If you assign values for 1st two indexes in 1st example, and refer to the 3rd index, you should get the undefined offset error.
I dont think it is about null or not, but a case of assigning values to indexes.
If you take a C parlance, array is an equivalent of malloc(upto a certain extent). Thankfully PHP does not crash, just throws an undefined index[offset] error

PHP Notice: A non well formed numeric value encountered [duplicate]

This question already has answers here:
number_format() causes error "A non well formed numeric value encountered"
(5 answers)
Closed 6 years ago.
I write a function like this:-
function loadTime() {
$load = microtime();
return number_format($load,2);
}
and then call it with a piece of HTML code:
Page generated in <?php echo loadTime() ?> seconds.
I think you can guess what i wanna do! i wanna display my page load time with this PHP function, i have to say that this function works but when i open error_log it shows this error:
PHP Notice: A non well formed numeric value encountered in
/home/coffeesc/public_html/index.php on line 12
as I searched, I think something is wrong with time or even date! no idea but I wonder if you can suggest me a way for displaying page load time with php (somehow in ms)
If you use microtime() and set the get_as_float parameter you get a simple floating point number that is just what you need for this kind of calculation
$page_start = microtime(1);
// all page code
sleep(2);
echo 'Page generated in ' . (microtime(1) - $page_start) . ' seconds';
The result being something like this
Page generated in 2.0001142024994 seconds

Strange offset error in PHP [duplicate]

This question already has answers here:
What causes: "Notice: Uninitialized string offset" to appear? [closed]
(5 answers)
Closed 9 years ago.
I am getting an error I cannot solve.
The error is:
Uninitialized string offset: 1
The code is:
if($play_count_within_45_minutes[1] > $play_history_old_over_45_minutes_ago[$i][1]){...}
The error is occurring on the $play_count_within_45_minutes[1] variable, because when I change the index to 0 (as in $play_count_within_45_minutes[0]), it works fine.
I had it display what the value would be, and it outputs fine, with this code:
print_r($play_count_within_45_minutes[1]);
The output is:
1.0E+80
The original variable declaration is:
$play_count_within_45_minutes = [0, 100000000000000000000000000000000000000000000000000000000000000000000000000000000];
I don't think the number is too large, as I tried changing it 1 and I got the same error.
I found it has something to do with this loop (print_r() is in there as I was testing it, and it gives the same error):
for($i=0; $i <= (count($play_history_old_over_45_minutes_ago)-1); $i++ ){
echo "<br>";
print_r($play_count_within_45_minutes[1]);
echo "<br>";
if($play_count_within_45_minutes[1] > $play_history_old_over_45_minutes_ago[$i][1]){
$play_count_within_45_minutes = $play_history_old_over_45_minutes_ago[$i][1];
}
}
Alright, I found the reason I couldn't figure it out.
What I didn't know about the PHP errors is that it treats everything after the original if statement as the same line, so even though the error was on line 105, it kept telling me it was on 104.
However, I still don't know why changing the original $play_count_within_45_minutes[1] to $play_count_within_45_minutes[0] solved the problem. Nonetheless, that would have still had a problem.
The fixed code is:
for($i=0; $i <= (count($play_history_old_over_45_minutes_ago)-1); $i++ ){
echo "<br>";
print_r($play_count_within_45_minutes[1]);
echo "<br>";
if($play_count_within_45_minutes[1] > $play_history_old_over_45_minutes_ago[$i][1]){
$play_count_within_45_minutes[1] = $play_history_old_over_45_minutes_ago[$i][1];
}
}
It seems like a dumb error, but I really didn't know about PHP treating the entire line following the if statement as the same line.

Categories