Lee
Lee

Reputation: 4323

A non well formed numeric value encountered - what does this mean?

I have the following message flagging up when notices are turned on:

Notice: A non well formed numeric value encountered

Does this mean the value expected was not a simple integer? The line referenced is showing this:

$per = ($raw>0?ceil((($raw/$cast)/5)*100):0).'%';

Upvotes: 2

Views: 18196

Answers (3)

Lee
Lee

Reputation: 4323

Thanks very much for your comments, but after looking at this with fresh eyes, I was able to look abit further into the code, and it dawned on me what was happening.

As asked above, I dumped the values of $raw and $cast, which were returned as:

string(2) "14" string(7) "3 votes"

So clearly, the '3 votes' is supposed to be a number, or the calculation isn't going to work. I had a look at the line above, which read:

$cast = (get_post_meta($id, '_kksr_casts', true)?get_post_meta($id, '_kksr_casts', true):'0').' votes';

Bingo. I removed the ending 'votes' output, leaving just the number. And the message disappears. I still want the original text to be outputted though, so I found the next line after 606 contained this:

$row = $avg . ' (' . $per . ') ' . $cast;

So I just added 'votes' on the end of this line, which results in the correct output in the WP Admin, and the error now gone.

Seems fixed, and a pull request created. Thanks for all your help guys.

Upvotes: 1

Álvaro González
Álvaro González

Reputation: 146410

To reproduce:

var_dump("14" / "3 votes");

(demo)

... prints a warning since PHP/7.1:

Notice: A non well formed numeric value encountered in /in/8dsH7 on line 3
float(4.6666666666667)

So they apparently added new checks to prevent errors.

Maths with numbers is reliable and well defined. Maths with arbitrary text is not. Or, do you think this makes sense:

var_dump("pink" * "apples");

int(0)

Upvotes: 5

Ben Hillier
Ben Hillier

Reputation: 2104

Are you entering it for an obfuscated code contest or something? :-D . I can't reproduce the notice message, but if you split the code up into separate lines, perhaps you will find out which part of the formula causes the message.

Why do you check if $raw > 0? If $raw is zero, then $per will simply end up being zero anyway.

If you're trying to avoid a division by zero error, then you need to be checking $cast, not $raw.

$per = 0;
if ($cast > 0) {
    $per = ($raw / $cast) / 5 * 100;
}
$per .= "%";

As a final comment, from this fragment of code, it's not clear why you divide by five. It's just a magic number. It would be clearer to make this a constant or variable; at the very least, leave a comment what it is.

Good luck!

Upvotes: 1

Related Questions