Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fix my "PHP Warning: A non-numeric value encountered" error?

Tags:

php

I have read through many other posts about this non-numeric value encountered error, but unfortunately, I may be dense. I'm not sure how to fix this annoying error.

Here is my code block:

 foreach ($local_words as $local_word_to_check){

                $letters = mb_str_split($local_word_to_check);
                $letters = array_map( 'addslashes', $letters );

                $local_total_letter_box_length = "";

                $query2 = "SELECT GROUP_CONCAT(Alpha) AS Alpha,
                                 GROUP_CONCAT(Letter_Box_Width) AS Letter_Box_Width
                          FROM Font_Krinkes
                          WHERE Alpha IN ('" . implode("','", $letters) . "')";

                $result2 = mysqli_query($con, $query2) or die("unable to query database!");

                if ($row = mysqli_fetch_assoc($result2)) {
                    $widths = array_combine(explode(',', $row['Alpha']), explode(',', $row['Letter_Box_Width']));
                    $total_word_box_width = 0;
                    foreach (mb_str_split($local_word_to_check) as $letter) {
                        $local_total_letter_box_length += $widths[$letter];
                    }
                }

                $complete_font_values[] = $local_total_letter_box_length;  

                unset($letters);
        }

The error happens on this line:

$local_total_letter_box_length += $widths[$letter];

Any help is very much appreciated.

like image 792
Wyatt Jackson Avatar asked Jan 28 '26 13:01

Wyatt Jackson


2 Answers

$local_total_letter_box_length = ""; is a string when it is first declared.

$local_total_letter_box_length += $widths[$letter]

That's not how you concatenate a string in php.

Use .= to concatenate.


If you mean to increase a numeric value, declare your variable with 0.

+= is okay to use on numeric values and arrays.

Code: (Demo)

$integer = 5;
$integer += 10;
echo $integer , "\n---\n";


$array = ['good' => 'one'];
$array += ['two'];
var_export($array);

Output:

15
---
array (
  'good' => 'one',
  0 => 'two',
)
like image 76
mickmackusa Avatar answered Jan 31 '26 03:01

mickmackusa


Use

$local_total_letter_box_length = 0; 

Instead of

$local_total_letter_box_length = "";
like image 33
Ahmad Saad Avatar answered Jan 31 '26 02:01

Ahmad Saad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!