Is this still necessary with php 8+? With all the type hinting and so on?

abhibeckert, (edited )

Is it necessary? No. It never was. But it is recommended.

Consider this code:

<span style="color:#323232;">function add(int $a, int $b) { return $a + $b; }
</span><span style="color:#323232;">
</span><span style="color:#323232;">$result = add(5, '10');

In strict_types mode, passing a string will throw an exception. In the default mode (including in PHP 8), the string will be silently cast to an integer.

Here’s a more realistic example:

<span style="color:#323232;">$result = add($_POST['a'], $_POST['b']);

HTTP values are always strings. So that code will only work with strict types disabled… and there is a lot of PHP code in the world that relies on this arguably “bad” behaviour.

I’d bet one day strict types be enabled by default. So if you want your code to be future proof… get used to enabling it now for all new files. Also I agree with @Dwoncount - you should really be using JSON instead of $_POST. And your JSON should have integer values, not strings.


Furthermore working with HTTP requests can be frustrating with strict typing enabled, since in HTTP requests everything is a string.

Sending the payload as JSON and you can be (quite) strict again.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • php
  • kavyap
  • ngwrru68w68
  • tacticalgear
  • DreamBathrooms
  • mdbf
  • magazineikmin
  • thenastyranch
  • Youngstown
  • Durango
  • slotface
  • everett
  • vwfavf
  • rosin
  • khanakhh
  • normalnudes
  • Leos
  • cisconetworking
  • cubers
  • InstantRegret
  • ethstaker
  • osvaldo12
  • modclub
  • anitta
  • provamag3
  • GTA5RPClips
  • tester
  • megavids
  • JUstTest
  • All magazines