Điều này sẽ làm những gì bạn muốn:
function notempty[$var] {
return [$var==="0"||$var];
}
Chỉnh sửa: Tôi đoán các bảng chỉ hoạt động trong bản xem trước, không phải trong các bài nộp trả lời thực tế. Vì vậy, vui lòng tham khảo các bảng so sánh loại PHP để biết thêm thông tin.
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true
Về cơ bản, notempty [] giống như! Clan [] cho tất cả các giá trị ngoại trừ "0", mà nó trả về true.true.
Chỉnh sửa: Nếu bạn đang sử dụng Error_Reporting [e_all], bạn sẽ không thể chuyển một biến không xác định cho các hàm tùy chỉnh theo giá trị. Và như Mercator chỉ ra, bạn nên luôn luôn sử dụng e_all để phù hợp với các thực tiễn tốt nhất. Liên kết này [nhận xét số 11], anh ấy cung cấp thảo luận về lý do tại sao bạn không nên sử dụng bất kỳ hình thức giảm lỗi nào cho hiệu suất và khả năng bảo trì/gỡ lỗi.error_reporting[E_ALL], you will not be able to pass an undefined variable to custom functions by value. And as mercator points out, you should always use E_ALL to conform to best practices. This link [comment #11] he provides discusses why you shouldn't use any form of error suppression for performance and maintainability/debugging reasons.
Xem câu trả lời của Orlandu63 về cách sử dụng các đối số được chuyển đến một hàm tùy chỉnh bằng cách tham khảo.
[Php 4, Php 5, Php 7, Php 8]
trống - xác định xem một biến có trống không — Determine whether a variable is empty
Sự mô tả
trống rỗng [hỗn hợp $var
]: bool[mixed $var
]: bool
Thông số
var
Biến cần được kiểm tra
Không có cảnh báo nào được tạo ra nếu biến không tồn tại. Điều đó có nghĩa là trống [] về cơ bản là tương đương ngắn gọn với! ISSET [$ var] || $ var == Sai.empty[] is essentially the concise equivalent to !isset[$var] || $var == false.
Trả về giá trị
Trả về true
Nếu var
không tồn tại hoặc có giá trị trống hoặc bằng 0, hay còn gọi là Fisey, xem chuyển đổi sang Boolean. Nếu không thì trả về false
.true
if var
does not exist or has a value that is empty or
equal to zero, aka falsey, see conversion to boolean. Otherwise returns false
.
Ví dụ
Ví dụ #1 Một so sánh đơn giản trống [] / isset [].empty[] / isset[] comparison.
Ví dụ #2 trống [] trên chuỗi offsetsempty[] on String Offsets
Ví dụ trên sẽ xuất ra:
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]
Ghi chú
Lưu ý: Bởi vì đây là một cấu trúc ngôn ngữ và không phải là một hàm, nó không thể được gọi là sử dụng các hàm biến hoặc các đối số được đặt tên.: Because this is a language construct and not a function, it cannot be called using variable functions, or named arguments.
Ghi chú::
Khi sử dụng trống [] trên các thuộc tính đối tượng không thể truy cập, phương thức quá tải __isset [] sẽ được gọi, nếu được khai báo.empty[] on inaccessible object properties, the __isset[] overloading method will be called, if declared.
Xem thêm
- ISSET [] - Xác định xem một biến được khai báo và khác với NULL
- __isset[]
- unset [] - Und đặt một biến đã cho
- Array_Key_Exists [] - Kiểm tra xem khóa hoặc chỉ mục đã cho có tồn tại trong mảng
- Count [] - Đếm tất cả các phần tử trong một mảng hoặc trong một đối tượng có thể đếm được
- strlen [] - Nhận độ dài chuỗi
- Các bảng so sánh loại
Nanhe Kumar ¶ ¶
8 năm trước
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true0
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true1
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true2
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true3
Janci ¶ ¶
13 năm trước
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true4
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true5
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true6
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Steven tại Nevvix Dot Com ¶ ¶
11 năm trước
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true8
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true9
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Thông tin tại Ensostudio Dot Ru ¶ ¶
1 năm trước
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]1
Markmanning tại Gmail Dot Com ¶ ¶
3 năm trước
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]2
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]3
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]4
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]5
anh em chấm của bạn dot t tại hotmail dot com ¶
7 năm trước
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]6
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]7
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]8
Martin Dot Aarhof tại Gmail Dot Com ¶ ¶
10 năm trước
bool[true] bool[false] bool[false] bool[false] bool[true] bool[true]9
$var
0
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Ẩn danh ¶ ¶
14 năm trước
$var
2
$var
3
$var
4
$var
5
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Chrisdmiddleton tại Gmail Dot Com ¶ ¶
8 năm trước
$var
7
$var
8
$var
9Janci ¶ ¶
13 năm trước
var
0
Steven tại Nevvix Dot Com ¶ ¶
13 năm trước
var
1
Steven tại Nevvix Dot Com ¶ ¶
11 năm trước
var
2
Thông tin tại Ensostudio Dot Ru ¶ ¶
13 năm trước
var
3
var
4
var
5
var
6Steven tại Nevvix Dot Com ¶ ¶
14 năm trước
var
7
var
8
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Chrisdmiddleton tại Gmail Dot Com ¶ ¶
wranvaud tại gmail dot com ¶
true
0
5 năm trước ¶
11 năm trước
true
1
true
2
true
3
true
4
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Thông tin tại Ensostudio Dot Ru ¶ ¶
wranvaud tại gmail dot com ¶
true
6
true
7
true
8
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
5 năm trước ¶
13 năm trước
var
0
var
1
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Steven tại Nevvix Dot Com ¶ ¶
wranvaud tại gmail dot com ¶
var
3
var
4
var
5
var
6
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
5 năm trước ¶
Claudio Galdiolo ¶
var
8
var
9
false
0
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
phpsort ¶ ¶
Denobocation-bozic et yahoo.com
false
2
false
3
false
4
false
5
false
6
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Tom tại Tomwardrop Dot Com ¶ ¶
13 năm trước
false
8
false
9
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Steven tại Nevvix Dot Com ¶ ¶
wranvaud tại gmail dot com ¶
1
2
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
5 năm trước ¶
3 năm trước
4
anh em chấm của bạn dot t tại hotmail dot com ¶
14 năm trước
5
6
7
8
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Chrisdmiddleton tại Gmail Dot Com ¶ ¶
wranvaud tại gmail dot com ¶
0
5 năm trước ¶
Claudio Galdiolo ¶
1
2
3
4
5
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
phpsort ¶ ¶
wranvaud tại gmail dot com ¶
7
8
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
5 năm trước ¶
10 năm trước
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true00
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true01
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Ẩn danh ¶ ¶
wranvaud tại gmail dot com ¶
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true03
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true04
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true05
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true06
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true07
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true08
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
5 năm trước ¶
Claudio Galdiolo ¶
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true10
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true2
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true12
phpsort ¶ ¶
Denobocation-bozic et yahoo.com
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true13
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true14
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true15
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Tom tại Tomwardrop Dot Com ¶ ¶
Denobocation-bozic et yahoo.com
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true17
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true18
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true19
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true20
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true21
Tom tại Tomwardrop Dot Com ¶ ¶
13 năm trước
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true22
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true23
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true7
Steven tại Nevvix Dot Com ¶ ¶
Denobocation-bozic et yahoo.com
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true25
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true26
notempty[""] : false notempty[null] : false notempty[undefined]: false notempty[array[]] : false notempty[false] : false notempty[true] : true notempty[1] : true notempty[0] : false notempty[-1] : true notempty["1"] : true notempty["0"] : true notempty["php"] : true27