Jump to content

Moving to PHP7 : Warning: A non-numeric value encountered


ilovepie

Recommended Posts

I have a huge application(multi-year) where I do the normal $varX= $var1 + $var2. 

I get

Warning: A non-numeric value encountered because var1 or var 2 are string.  I know the right thing will be to intval(), but I just cant go over so much of the application.

I read to just disable warning, but I think thats worst because I will not be able to see new warnings.  Is there any php.ini solution were php can work as before on math operations?

I guess I can ini_set on all the old files and dont do it on the new ones).

If there is another accepted solution, please let me know

Thank you

 

 

Link to comment
Share on other sites

The problem isn't that you have to intval() things. The problem is that $var1 and/or $var2 are not proper numeric values. You need to figure out why that might be the case, then probably add or edit input validation so that the non-numeric values are not being allowed.

Link to comment
Share on other sites

15 hours ago, ilovepie said:

I know the right thing will be to intval(), but I just cant go over so much of the application.

Unfortunately, that is exactly what you need to do.   

PHP is telling you that you're trying to do arithmetic on duff data values - things that really are not numbers. 

It's not, as in other languages, telling you that you're using String-typed variables in an arithmetic expression and that it might have to do some [Evil] Type Coercion that might give you some strange results.  It's telling you, unequivocally, that the data values coming into that expression are wrong!  You need to find out where that dodgy data is coming from and fix it.  

Regards, 
   Phill  W.

Link to comment
Share on other sites

You have two posters telling you the same basic thing - you have a problem with your assumed values of things that needs to be addressed rather than ignored.

Moreover - you state that you "do the normal $varX= $var1 + $var2".  I"m not sure what you are really implying here but have to say that, as a rule, you are not validating your data most of the time if that arithmetic line is "normal" for you.  That further indicates to me that you have some serious problems with this application that up until now have been ignored but now has to be resolved.

Sorry to say but it does look like you have some thinking to do as to how to easily identify where these issues can arise.  One solution might be to replace these kinds of lines with a function that analyzes the incoming values before doing the math.  The hard part of course is finding these kinds of lines.

Good luck!

Link to comment
Share on other sites

18 hours ago, ilovepie said:

have a huge application(multi-year) where I do the normal $varX= $var1 + $var2. 

I get

Warning: A non-numeric value encountered because var1 or var 2 are string.  I know the right thing will be to intval(), but I just cant go over so much of the application.

I read to just disable warning, but I think thats worst because I will not be able to see new warnings.  Is there any php.ini solution were php can work as before on math operations?

I guess I can ini_set on all the old files and dont do it on the new ones).

If there is another accepted solution, please let me know

Thank you

it sounds like you are manually creating new code each year/season with hard-coded values in it. your time would be better spent making one instance of the code dynamically produce/operate on whatever is different each year, so that you only have to find and fix whatever is causing the current error once.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.