PHP has a predefined constant true whose value is the integer 1. How to understand this?

WBOY
Release: 2016-10-10 11:56:26
Original
1981 people have browsed it

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.

Reply content:

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>
Copy after login

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>
Copy after login

Then there are TRUE and FALSE macros, the definitions are respectively

<code class="cpp">#define TRUE 1
#define FALSE 0</code>
Copy after login

C++ itself also has a macro NULL, the definition is

<code class="cpp">#define NULL 0</code>
Copy after login

And I suspect that PHP treats true as a boolean type 1. Just guessing, please feel free to object if I'm wrong.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!