A particular line of my code is occasionally giving the PHP notice “A non well formed numeric value encountered” when multiplying two variables. It’s a production site and I’ve identified the variable responsible but it only happens occasionally for particular users. 99.9% of the time it works fine. I could cast it as a float but I’d rather find out what’s responsible (ie. what value the variable has).
What I’m struggling with is catching and logging the non well formed numeric value. is_numeric doesn’t catch it whereas !is_int($v) && !is_float($v) matches valid values because the variable is a string.
So does anyone know of a way of testing if a variable is a well formed numeric value?
$line[2] is the variable that’s “non well formed” and it should be a price. This code runs hundreds of times every minute without problem but occasionally I end up with a value that causes this PHP notice.
(Mildly Off-Topic)
This is when it inevitably arises that PHP’s error reporting would do wonderfully with just a bit more information… “not well formed value”… well WHAT value did you receive, error-message?
(/QuasiRant)
Thanks for your replies. I know it’s definitely $line[2] that’s the problem because I split it onto multiple lines, eg:
$price = $price +
($line[0] *
$line[2]);
and the Notice in the PHP log referenced the line with $line[2] on it.
You’ve given me some ideas though so I’ll have a play and report back.
Absolutely. It only happens occasionally though so I need to catch the error, log some details then figure out what’s going wrong. It’s just proving to be a little bit tricky.