Testing for well formed numeric value

Hi,

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?

Thanks,

Martin.

Do you have commas in your string value? That can cause havoc converting to floats

There shouldn’t be any commas but I can’t rule it out.

I thought about using regex to test for any character that isn’t a number of a period but that doesn’t necessarily guarantee that it’s “well formed”.

Thinking out loud: perhaps I could run the variable through number_format() and compare that to the original.

Try is_string($v); and if true log the value.

Edit:
Can you show the function and the expected parameter types?

It will always be a string so I don’t think that’ll help. Here’s a snippet:

foreach ($cart_contents AS $v) {
 $line = explode(',', $v); 
 $price = $price + ($line[0] * $line[2]);
}

$v should be something like: 3,12345,10.99

$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.

What are you initiating $price with?

Because $v is a string I would try using the following:

`$v`  should be something like:  `3,12345,10.99`

$line[0] = trim( $line[0]);
// $line[0] = (int) $line[0];

// $line[1] = trim( $line[1]);
// $line[1] = (float) $line[1];

$total = $line[0] * line[1] ;

if( is_numeric($price) && is_numeric($total) ):
  $price += $total;
else:
  log( $v );
  log( $total );

endif;

I would try just using one conversion at a time and if that does not work then rem the previous test and try the next, etc

1 Like

should is the key word there. It must not be or it wouldn’t be errorring.

A quick guess is you’ve got an extra comma somewhere so the $line[2] is not what you think it is when it’s exploded.

1 Like

(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)

1 Like

I did notice and tried to pinpoint the error by adding a $total variable because as you mentioned the error could have been related to $price

// OLD
    $price = $price + ($line[0] * $line[2]);

// NEW
   $total = $line[0] * $line[2] ;
 $price = $price + $total;

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.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.