Different behavior in Laravel Homestead & Laravel Forge

112 views Asked by At

I had a simple mistake in my PHP Code:

$string += 'something' . $Car->id . ',';

Which resulted in different behavior in Homestead and Forge:

Homestead Result of $string:

0

Forge Result:

Error: A non-numeric value encountered

Does Anybody know why? And how can I change the Homestead behavior to the same like the forge one? It's absolutely better..


Update

I'm sorry I wasn't totally clear in my question. The question wasn't about the mistake itself += instead of .=, I was aware of that.

The question is why in one environment the error appears and in the other one not.

Both environments are configured the same way (php.ini):

error_reporting = E_ALL
display_errors = On

And it was not just a notice, it's an error.

3

There are 3 answers

0
ndberg On BEST ANSWER

The PHP Version of Laravel Homestead was 7.0.8 and the PHP Version of Laravel Forge 7.1.0-3.

I updated Laravel Homestead to the newest which uses PHP 7.1.0-2, and now Homestead shows up the right error:

Error: A non-numeric value encountered

I'm not shure if it's just the PHP Version or if it's an other change in Homestead with the new version. Thats just what I've found out.

0
Jonathon On

Your issue is because of the += operator. That is used to add numbers together, whereas you're trying to concatenate strings.

You should be using .=

$string .= 'something' . $Car->id . ',';

or alternatively:

$string = $string . 'something' . $Car->id . ',';

The reason you're seeing that message in forge is because it seems to have PHP warnings turned on.

0
Lajos Arpad On

+= adds a number (right value) to a variable's value (left value) and stores it in the variable.

Since your code of

$string += 'something' . $Car->id . ',';

has $string as the left value and contains non-numeric characters, hence the error. You probably meant .= which concatenates a string (right value) to the value of a variable (left value) and stores it in the variable.