Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

Why this result in PHP?

Why does the following return 1 instead of true?

echo 5===5;  //1;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First of all, echo is not a function, it is a language construct and it does not actually "return" anything. echo is for outputting strings. The reason it outputs (not returns) 1 instead of true is because true is not a string, it is a boolean value and therefore when it is typecast to a string, PHP converts it to "1". If you want to see the real value of something, you need to use something like var_dump().

var_dump(true);
var_dump((string) true);
var_dump(5 === 5);
var_dump(false);
var_dump((string) false);
var_dump(5 === 6);

Output:

bool(true)
string(1) "1"
bool(true)
bool(false)
string(0) ""
bool(false)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...