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
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 got an Error in my For Loop for some reason.
I just want to go backwards through an array in php.
The Error in my Browser is:
Fatal error: Unsupported operand types in /var/html/modules/getChat.php on line 18
Line 18 in this Code part line 1:
Here is the Code:
for($x = sizeof($result-1); $x > 0; $x--)
{
echo '<div class="message '.$result[$x].'"> <img src="'.$result[$x].'" /><span class="name">'.$result[$x].'</span>
<p>'.$result[$x].'</p>
</div>';
}
I hope you can help
Thanks
$result is an array, and substracting 1 from an array doesn't make any sense. You probably wanted to use this instead:
for ($x = sizeof($result) - 1; $x > 0; $x--) // ...
And yes, it seems that you unintentionally skip the very first element of your array here. If so, fix the condition ($x >= 0) - or just compact the whole loop into while:
$x = count($result);
while($x--) {
// output with $result[$x]
}
If that's not a bottleneck (and most probably it's not), you better show the real intent of the code with array_reverse:
foreach (array_reverse($result) as $el) {
// output with $el
}
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++) {
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.
I'm trying to define a new shortcode in WordPress, and I get the following error when the function is loaded (just loaded, I haven't tried to call it anywhere yet):
Parse error: syntax error, unexpected T_STRING in /pathtomytheme/user_functions.php on line 105
Here's the code; line 105 is "$cat_n = count($cats) – 1;":
function usertag_2colcats($atts) {
extract(shortcode_atts(array('parent' => 0,'depth' => 2,), $atts));
$cats = explode('<br />', wp_list_categories('title_li=&echo=0&depth=' . $depth . '&style=none&show_count=1&use_desc_for_title=0&child_of=' . $parent));
$cat_n = count($cats) – 1;
for ($i = 0; $i < $cat_n; $i++) {
if ($i < $cat_n/2) $cat_left = $cat_left . '<li>' . $cats[$i] . '</li>';
elseif ($i >= $cat_n/2) $cat_right = $cat_right.'<li>'.$cats[$i].'</li>';
}
echo '<ul class="leftcats">' . $cat_left . '</ul><ul class="rightcats">' . $cat_right . '</ul>';
}
If I change that line so that it doesn't use the count function, e.g. to "$cat_n = 5;", the function loads without error. It seems like I'm missing something really obvious; what is it?
The original code is here: http://pcsplace.com/blog-tips/how-to-split-categories-list-into-columns-in-wordpress/
This might sound weird, but the "-" sign in line 105 is a weird character. Try to rewrite that line at hand instead of copying and paste it. I did it, and the error went away.
Edit: Ok, so this is what I found. The character you have in line 105 has ASCII code 226. But the ASCII code for the minus sign is 45. So definitely your problem is there.
Avoid copy paste at all cost ;)
Have you tried to var_dump( $cats ) ?
Sometimes count() can return false depending on some situations, but on this case I'm pretty sure it only returned the entire string as it didn't found it.