Warning: Illegal string offset in PHP 5.4 [duplicate] - php

This question already has answers here:
Illegal string offset Warning PHP
(17 answers)
Closed 9 years ago.
I upgraded to PHP 5.4 today and I am receiving some strange warnings:
Warning: Illegal string offset 'quote1' in file.php on line 110
Warning: Illegal string offset 'quote1_title' in file.php on line 111
Those lines are this part of the code:
for($i = 0; $i < 3; $i++) {
$tmp_url = $meta['quote'. ($i+1)];
$tmp_title = $meta['quote' . ($i+1) .'_title'];
if(!empty($tmp_url) || !empty($tmp_title)) {
$quotes[$src_cnt] = array();
$quotes[$src_cnt]['url'] = $tmp_url;
$quotes[$src_cnt]['title'] = $tmp_title;
$src_cnt++;
}
}
So the $tmp_url and $tmp_title line.
Why am I receiving this odd warning and what is the solution?
Update:
This code is being used as a Wordpress plugin. $meta includes:
$meta = get_post_meta($post->ID,'_quote_source',TRUE);
So I am suspecting that whenever the quotes fields are empty, this warning appears. Is there any way that I can fix this for when the fields are empty?

You need to make sure, that $meta is actually of type array. The warning explicitly tells you, that $meta seems to be a stringĀ and not an array
Illegal string offset
^^^^^^
To avoid this error you may also check for the needed fields
for($i = 0; $i < 3; $i++) {
if ( !is_array($meta) || !array_key_exists('quote'. ($i+1), $meta) ){
continue;
}
// your code
}

If $meta is null whenever there's no data to process:
if( !is_null($meta) ){
for($i = 0; $i < 3; $i++) {
// ...
}
}
You should be able to do more checks if necessary. That depends on what that get_post_meta() function is designed to return.

Related

Migration to PHP8

I have code from old PHP.
But when I tried to execute it by PHP 8.
The first code was:
PasteBin
I had error:
Fatal error: Array and string offset access syntax with curly braces
is no longer supported in **** on line 550
On line:
for ($i = 0; $i < strlen($text); $i++) $res .= ord($text{$i}) . "-";
I changed it to:
for ($i = 0; $i < strlen($text); $i++) $res .= ord($text[$i]) . "-";
But I had another error:
Warning: Trying to access array offset on value of type bool in ***
on line 76
On line:
$real = $row['sip'];
I have no idea - how to rewrite this string.
Can you help me?
the problem is that you are trying to access a boolean value as you do with an array.
i imagine that $row is the result of a query, and that query does not return any matching rows, so it's false.
just check if $row is false before you access it.
<?php
$row = false;
echo $row['test'];
this returns that warning.
as per your comment, it depends on what you wanna do.
if it exist return the values, if it doesn't?
if($row){
// if it contains something, do something with it
}else{
// do something else if it doesn't
}
i don't know what's the flow of your code, so I can't really help you, it's just a check to see if the $row variable is not false

Warning: A non-numeric value encountered in /templates/section-home.php on Line 34 [duplicate]

This question already has answers here:
Warning: A non-numeric value encountered
(26 answers)
Closed 4 years ago.
I have changed my php version from 5.6 to 7.2.
Warning: A non-numeric value encountered in
/templates/section-home.php on Line 34
This is the line
for ($j = 1; $j <= 10; $j++) { if ( px_opt('home-gallery-'.$j) ) { $slideCount+=1; $slideNumber=$j;} }
In this code
<?php
$slideCount = "";
$slideNumber= "";
if ( px_opt('home-type-switch') == 'home-slider' ) {
for ($j = 1; $j <= 10; $j++) { if ( px_opt('home-gallery-'.$j) ) { $slideCount+=1; $slideNumber=$j;} }
if ( $slideCount > 1 ) { ?>
What do I have to change?
Thank you
try to initialize your $slideCount and $slideNumber variables as 0 instead of ""

PHP for-loop not working [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I have this for-loop in PHP which isn't working:
for ($i = 0; $i <= 100; $i = $i + 10) {
echo $i;
}
and this one that does work:
for ($i = 0; $i <= 100; $i = $i + 10) {
echo $i;
}
I'm sure that I'm missing something, but they appear to be exactly identical to each other. What's the difference? The first one was copied and pasted, the second one typed by myself. What's the difference? Why isn't the second one working? The error is this:
This is the error message:
Parse error: syntax error, unexpected ')', expecting ';' on line 12
Line 12 is the first line of the code in the sample (it's from a larger script).
The loops are exactly the same and they both work, you must have any other error on the script, although, you can use :
$i+=10;
instead of:
$i = $i + 10

Why i'm getting Undefined offset: 0 in this php code

i wrote this code and i'm getting this error, i tried every this but doesn't seem to work btw i've made a comment for line 55 where i'm gettin error
function calculate_result()
{
$option_number = array('option_a'=>'1','option_b'=>'2','option_c'=>'3','option_d'=>'4');
$answers = array();
$total_questions = $this->quiz_model->return_number_of_questions($this->input->post('quiz_number'));
if($total_questions > 0)
{
for($i=1; $i <= $total_questions; $i++)
{
//line 55 $answers[$i] = $option_number[$this->input->post('question_'.$i)];
}
print_r($answers);
}
else
{
show_404();
}
//print_r($answers);
}
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: controllers/quiz.php
Line Number: 55
Change this...
for($i=1; $i <= $total_questions; $i++)
To this...
for($i=0; $i <= $total_questions; $i++)
On that line there are two indexes, on the left side, there is $i and it is non-zero.
So, problems has to be with right side expression, with $this->input->post('question_'.$i) which probably returns 0.

php undefined offset 150 error in for loop

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++) {

Categories