Đây là loại đơn giản nhất. Một bool thể hiện một giá trị sự thật. Nó có thể là true
hoặc false
.bool expresses a truth value. It can be either true
or false
.
Cú pháp
Để chỉ định một bool theo nghĩa đen, hãy sử dụng các hằng số true
hoặc false
. Cả hai đều không nhạy cảm trường hợp.bool literal, use the constants true
or false
. Both are case-insensitive.
<?php
$foo = True; // assign the value TRUE to $foo
?>
Thông thường, kết quả của một toán tử trả về giá trị bool được chuyển vào cấu trúc điều khiển.bool value is passed on to a control structure.
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
Chuyển đổi thành Boolean
Để chuyển đổi rõ ràng một giá trị thành Bool, hãy sử dụng các phôi (bool)
hoặc (boolean)
. Tuy nhiên, trong hầu hết các trường hợp, dàn diễn viên không cần thiết, vì giá trị sẽ được tự động chuyển đổi nếu toán tử, hàm hoặc cấu trúc điều khiển yêu cầu đối số bool.bool, use the (bool)
or (boolean)
casts. However, in most cases the cast is
unnecessary, since a value will be automatically converted if an operator, function or control structure requires a bool argument.
Xem thêm Loại tung hứng.
Khi chuyển đổi sang BOOL, các giá trị sau được coi là false
:bool, the following values are considered false
:
- chính boolean
false
false
itself - Số nguyên 0 (không)
- Phao 0,0 và -0,0 (không)
- Chuỗi trống và chuỗi "0"
- một mảng có phần tử không
- Loại NULL đặc biệt (bao gồm các biến số không đặt)
- Các đối tượng SimplexML được tạo từ các yếu tố trống không được quy kết, tức là các phần tử không có trẻ em cũng không thuộc tính.
Mọi giá trị khác được coi là true
(bao gồm mọi tài nguyên và false
1).true
(including any resource and false
1).
Cảnh báo
false
2 được coi là true
, giống như bất kỳ số khác không khác (dù là âm hay dương)!true
, like any other non-zero (whether negative or positive) number!
false
4
Fred Koschara ¶
9 năm trước
false
5
false
6
false
7
false
8
false
9
true
0
true
1
true
2
Mark Simon ¶ ¶
5 năm trước
true
3
true
4
true
5
true
6
true
7
true
2 Quản trị viên tại Eexit Dot Fr ¶ ¶
13 năm trước
true
9
false
0
false
1
false
2 Goran77 tại Fastmail Dot FM ¶ ¶
6 năm trước
false
3
false
4
false
5
false
6
true
2 Terminatorul tại Gmail Dot Com ¶ ¶
15 năm trước
false
8
false
9
true
2 Steve ¶ ¶
14 năm trước
<?php
$foo = True; // assign the value TRUE to $foo
?>
1
<?php
$foo = True; // assign the value TRUE to $foo
?>
2
<?php
$foo = True; // assign the value TRUE to $foo
?>
3
<?php
$foo = True; // assign the value TRUE to $foo
?>
4
true
2 artktec tại gmail dot com ¶ ¶
15 năm trước
<?php
$foo = True; // assign the value TRUE to $foo
?>
6
<?php
$foo = True; // assign the value TRUE to $foo
?>
7
<?php
$foo = True; // assign the value TRUE to $foo
?>
8
<?php
$foo = True; // assign the value TRUE to $foo
?>
9
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
0
<?php
$foo = True; // assign the value TRUE to $foo
?>
9
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
2
true
2 Mark Simon ¶ ¶
5 năm trước
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
4
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
5
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
6
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
7
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
8
<?php
// == is an operator which tests
// equality and returns a boolean
if ($action == "show_version") {
echo "The version is 1.23";
}// this is not necessary...
if ($show_separators == TRUE) {
echo "<hr>\n";
}// ...because this can be used with exactly the same meaning:
if ($show_separators) {
echo "<hr>\n";
}
?>
9
(bool)
0
(bool)
1
true
2 Quản trị viên tại Eexit Dot Fr ¶ ¶
14 năm trước
(bool)
3
(bool)
4
(bool)
5
(bool)
6
artktec tại gmail dot com ¶ ¶
6 năm trước
(bool)
7
(bool)
8
true
2 Terminatorul tại Gmail Dot Com ¶ ¶
6 năm trước
(boolean)
0
(boolean)
1
true
2 Terminatorul tại Gmail Dot Com ¶ ¶
9 năm trước
(boolean)
3
(boolean)
4
(boolean)
5
(boolean)
6
(boolean)
7
(boolean)
8
true
2 Mark Simon ¶ ¶
5 năm trước
false
0
false
1
false
2
false
3
false
4
false
5
false
6
false
7
true
2 Quản trị viên tại Eexit Dot Fr ¶ ¶
13 năm trước
false
9
false
0
true
2 Goran77 tại Fastmail Dot FM ¶ ¶
6 năm trước
false
2
false
3
true
2 Terminatorul tại Gmail Dot Com ¶ ¶
15 năm trước
false
5
Steve ¶ ¶
13 năm trước
false
6
Goran77 tại Fastmail Dot FM ¶ ¶
6 năm trước
false
7
false
8
false
9
false
00 Terminatorul tại Gmail Dot Com ¶ ¶
14 năm trước
false
01
false
02
false
03
false
04 artktec tại gmail dot com ¶ ¶
13 năm trước
false
05
Goran77 tại Fastmail Dot FM ¶ ¶
6 năm trước
false
06
false
07
true
2