1

Every so often in PHP we accidentally use the '+' operator instead of the '.' operator when intending to concatenate two strings, especially on sites that use both PHP and JavaScript since usage differs. Doing an arithmetic operation on two strings is never used on our sites (we covert strings to numbers first when necessary).

Is there a way to get PHP to report a warning/error when attempting an arithmetic operation on two strings? This would be very useful in preventing programming errors.

  • 1
    Because PHP is loosely typed and does type juggling it will try to sum the two strings. – Jay Blanchard Jul 16 '15 at 18:28
  • Investigate a few [static code analyzers](http://stackoverflow.com/questions/378959/is-there-a-static-code-analyzer-like-lint-for-php-files) if that's really a common issue. Else switch to an IDE with more helpful watch schemes and warnings. – mario Jul 16 '15 at 18:32

1 Answers1

0

s there a way to get PHP to report a warning/error when attempting an arithmetic operation on two strings?

No, PHP offers no such option. You may want to configure your IDE's color schema so . (dot) is differently colored (I personally use different background color for dot) so you'd be able to spot this more easily.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141