md5($ _ POST ['username'] + microtime())说它遇到了一个非数字值,因为新的php版本

I reinstalled WAMP and now I have PHP version 7.1.9 (before this I had 7.0 x).

A part of the code doesn’t work anymore without warning/notice reports.

 - Warning: A non-numeric value encountered in

And:

 - Notice: A non well formed numeric value encountered in

The script with the error contains the following code:

<?php $_POST['username'] = 'yourname';


$code = md5($_POST['username'] + microtime() )  ;


var_dump($code); ?>

I believe that my problem arises as a result of upgrading my PHP interpreter.

I had better results with setting microtime to microtime(true). The errors are even not there when I set $_POST['username'] to a number. But like I said: I want to know what causes it and how to solve it.

I also read through the php migrating docs to find anything about microtime or variables regarding md5 maybe, but nothing.

Could it be the settings are different in Wamp or possible it is a bug in php or so?

You should write line like :

<?php 
$_POST['username'] = 'yourname';
$code = md5( $_POST['username'] . microtime() )  ;
var_dump( $code ); ?>

You get this error because starting with PHP7 the interpreter is a little less lax when doing type conversions.

Going by parts, you are trying to add (as in: using the addition operator, ++) two values and whatever you have in $_POST['username'] is certainly not a valid number; and the return of microtime(), by default, is not a valid number either.

Before PHP 7.1, this would work silently, and the intepreter would perform a silent type cast behind the scenes, never complaining. But on PHP >= 7.1, you need to be a bit more careful with types.

The suggested workaround of using the concatenation operator (.) works because of the result of microtime() and the contents of $_POST['username'] are both strings, and md5() expects a string as a parameter anyway.

$code = md5( $_POST['username'] . microtime() )  ;