skip to Main Content

I know that to enforce the scalar types, we use

declare(strict_types=1)

But my question is, why use the integer 1? Is it representing TRUE’s boolean value, or does it have to do something different?

2

Answers


  1. PHP is written in C. Older versions of C before 1999 did not support boolean data type. First version of PHP was developed in 1994. The probable reason is backwards compatibility. There are generally no configuration variables in PHP in boolean data type https://www.php.net/manual/en/ini.list.php

    Login or Signup to reply.
  2. There is a relevant RFC for this feature. According to this RFC the values permissible for strict_types are 0 and 1 corresponding 0=weak types 1=strict types so in practice these are essentially boolean values.

    Now why use 1 and not true? There doesn’t seem to be an actual answer to this question. The implemented RFC was forked from this one both of which specify the use of strict_types values of 0 or 1 corresponding to disabled and enabled.

    There is no explanation for why this decision was made i.e. why use the integer values instead of boolean values. There is no restrictions documented in what can be used in declare for new features. Declare has been in PHP since v4 so if I had to guess I would think it’s either (a) because it makes implementation easier or (b) it follows a convention.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search