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.
Related
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.
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I don't know why the code below giving an error on my laptop while not at my friend's.
<?php
function myfunction() : int {
return 10;
}
echo myfunction();
?>
Error
Parse error: syntax error, unexpected ':', expecting '{' in (my location) on line 2.
If I remove the ": int" on line 2 everything is fine, but can someone explain why this code can't run on mine?
Please read the documentation. It is a PHP7+ only feature.
What might be a good idea as a work around, until you migrate to PHP7, is to do the following:
function myfunction() {
return (int)10;
}
var_dump(myfunction());
That will convert the return to an integer.
It's worth noting, this won't throw any warnings if the return value cannot be resolved.
I.E. If you passed parameters and those parameters were letters in a string, for instance, you'd get Warning: A non-numeric value encountered. However, for now, I think the above solution will suffice.
I strongly recommend upgrading to the latest version of PHP, though.
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
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
This question already has answers here:
Fatal error: Unsupported operand types
(5 answers)
Closed 6 years ago.
This line of code has begun producing an error since I've begun converting my code to PDO:
$colcount = floor(($total / $columns) + ($total % $columns ? 1 : 0));
The error it returns is:
Fatal error: Unsupported operand types
I have searched for this issue but can't seem to find an answer to apply to my specific code.
If anyone could point me to an answer or give an explanation or example of what's causing this and how it can be repaired, I'd be very grateful.
I can also post the surrounding code, if it helps.
This error occurs when you attempt to use arithmetic operators on an array instead of proper numeric value(s). $total and/or $columns is an array. var_dump() them to see.