Home > Article > Backend Development > PHP has a predefined constant true whose value is the integer 1. How to understand this?
When I executed get_defined_constants(), I accidentally discovered that PHP has an internal constant with the name true and the value 1. There are also constants named false and null.
Does PHP treat true as a constant? Shouldn't it be a "value"?
Shouldn’t it be a value with data type boolean?
I tried to execute echo(true), the browser output the character 1, and when I var_dump(true), it output bool(true). Isn’t this an obvious contradiction?
Also, true===1 is not true. true==1 is established.
So I want to know how php handles true false null.
When I executed get_defined_constants(), I accidentally discovered that PHP has an internal constant with the name true and the value 1. There are also constants named false and null.
Does PHP treat true as a constant? Shouldn't it be a "value"?
Shouldn’t it be a value with data type boolean?
I tried to execute echo(true), the browser output the character 1, and when I var_dump(true), it output bool(true). Isn’t this an obvious contradiction?
Also, true===1 is not true. true==1 is established.
So I want to know how php handles true false null.
echo input is a string, so true is type-converted. You can refer here
<code>Printing or echoing a FALSE boolean value or a NULL value results in an empty string: (string)TRUE //returns "1" (string)FALSE //returns "" echo TRUE; //prints "1" echo FALSE; //prints nothing!</code>
Reference documentation
Crooked building. Tell a C++ story.
Windows API has a data type BOOL
, with a similar definition
<code class="cpp">typedef int BOOL;</code>
Then there are TRUE
and FALSE
macros, the definitions are respectively
<code class="cpp">#define TRUE 1 #define FALSE 0</code>
C++ itself also has a macro NULL
, the definition is
<code class="cpp">#define NULL 0</code>
And I suspect that PHP treats true
as a boolean type 1. Just guessing, please feel free to object if I'm wrong.