Rỗng và rỗng trong php

Bạn có thể sử dụng hàm PHP empty() để tìm hiểu xem một biến có trống hay không. Một biến được coi là rỗng nếu nó không tồn tại hoặc nếu giá trị của nó bằng FALSE

Hãy thử ví dụ sau để hiểu cơ bản chức năng này hoạt động như thế nào

";
 
if(empty($var2)){
    echo 'This line is printed, because the $var2 is empty.';
}
echo "
"; if(empty($var3)){ echo 'This line is printed, because the $var3 is empty.'; } echo "
"; if(empty($var4)){ echo 'This line is printed, because the $var4 is empty.'; } echo "
"; if(empty($var5)){ echo 'This line is printed, because the $var5 is empty.'; } ?>

Ghi chú. Hàm empty() không tạo cảnh báo nếu biến không tồn tại. Điều đó có nghĩa là empty() tương đương với !isset($var) || $var == false

PHP có nhiều chức năng để kiểm tra các biến PHP đối với các giá trị khởi tạo của chúng. Các hàm này là isset, empty và is_null

  • isset() là để kiểm tra xem một biến có được đặt với một giá trị hay không và giá trị đó không được rỗng
  • trống () là để kiểm tra xem một biến đã cho có trống không. Sự khác biệt với isset() là, isset có null check
  • is_null() là để kiểm tra xem một biến có được định nghĩa là null hay không

Tôi cảm thấy cách hiệu quả để truyền đạt sự khác biệt là sử dụng BẢNG SỰ THẬT,

“”“apple”NULLFALSE0undefinedTRUEarray()123issetTRUETRUEFALSETRUETRUEFALSETRUETRUETRUEemptyTRUEFALSETRUETRUETRUETRUEFALSETRUEFALSEis_nullFALSEFALSETRUEFALSEFALSEWarning / TRUEFALSEFALSEFALSE

Vâng. Bây giờ hãy tìm sự khác biệt

Rỗng và rỗng trong php

Mã để hiểu sự khác biệt

sự khác biệt. php

ISSET:
"; $var = ""; print 'isset(""): ' . isset($var) . "
"; $var = 'apple'; print "isset('apple'): " . isset($var) . "
"; $var = null; print "isset(null): " . isset($var) . "
"; $var = FALSE; print "isset(FALSE): " . isset($var) . "
"; $var = 0; print "isset(0): " . isset($var) . "
"; print "isset(undefined): " . isset($var3) . "
"; $var = TRUE; print "isset(TRUE): " . isset($var) . "
"; $var = array(); print "isset(array()): " . isset($var) . "
"; $var = 123; print "isset(123): " . isset($var) . "
"; print "
EMPTY:
"; $var = ""; print 'empty(""): ' . empty($var) . "
"; $var = 'apple'; print "empty('apple'): " . empty($var) . "
"; $var = null; print "empty(null): " . empty($var) . "
"; $var = FALSE; print "empty(FALSE): " . empty($var) . "
"; $var = 0; print "empty(0): " . empty($var) . "
"; print "empty(undefined): " . empty($var1) . "
"; $var = TRUE; print "empty(TRUE): " . empty($var) . "
"; $var = array(); print "empty(array()): " . empty($var) . "
"; $var = 123; print "empty(123): " . empty($var) . "
"; print "
IS_NULL:
"; $var = ""; print 'is_null(""): ' . is_null($var) . "
"; $var = 'apple'; print "is_null('apple'): " . is_null($var) . "
"; $var = null; print "is_null(null): " . is_null($var) . "
"; $var = FALSE; print "is_null(FALSE): " . is_null($var) . "
"; $var = 0; print "is_null('0'): " . is_null($var) . "
"; print "is_null(undefined):" . is_null($var2) . "
"; $var = TRUE; print "is_null(TRUE): " . is_null($var) . "
"; $var = array(); print "is_null(array()): " . is_null($var) . "
"; $var = 123; print "is_null(123): " . is_null($var) . "
"; ?>

đầu ra

ISSET:
isset(""): 1
isset('apple'): 1
isset(null):
isset(FALSE): 1
isset(0): 1
isset(undefined):
isset(TRUE): 1
isset(array()): 1
isset(123): 1

EMPTY:
empty(""): 1
empty('apple'):
empty(null): 1
empty(FALSE): 1
empty(0): 1
empty(undefined): 1
empty(TRUE):
empty(array()): 1
empty(123):

IS_NULL:
is_null(""):
is_null('apple'):
is_null(null): 1
is_null(FALSE):
is_null('0'):
Notice: Undefined variable: var2 in .../index.php on line 51
is_null(undefined):1
is_null(TRUE):
is_null(array()):
is_null(123):

sự khác biệt

ngay lập tức

Trả về true cho chuỗi rỗng, False, 0 hoặc một biến không xác định. Trả về false cho null

trống rỗng

Trả về true cho null, chuỗi trống, Sai, 0 hoặc một biến không xác định. Trả về true nếu có bất kỳ giá trị nào

is_null

Chỉ trả về true cho null. Trả về false trong tất cả các trường hợp khác. Ném cảnh báo nếu biến không được xác định. Nếu bạn chặn cảnh báo, bạn sẽ nhận được sự thật

Hàm isset() là một hàm sẵn có trong PHP để kiểm tra xem một biến có được đặt và không phải là NULL hay không. Hàm này cũng kiểm tra xem một biến, mảng hoặc khóa mảng đã khai báo có giá trị null hay không, nếu có, isset() trả về false, nó trả về true trong tất cả các trường hợp có thể khác

cú pháp.  

bool isset( $var, mixed )

Thông số. Chức năng này chấp nhận nhiều hơn một tham số. Tham số đầu tiên của hàm này là $var. Tham số này dùng để lưu trữ giá trị của biến

Thí dụ.   

PHP




 

// PHP program to illustrate

// isset() function

$num ='0'

0 is set with isset function 
array is not set.
0

 

0 is set with isset function 
array is not set.
1
0 is set with isset function 
array is not set.
2$num
0 is set with isset function 
array is not set.
4

0 is set with isset function 
array is not set.
5
0 is set with isset function 
array is not set.
6
0 is set with isset function 
array is not set.
7
0 is set with isset function 
array is not set.
8

0 is set with isset function 
array is not set.
9

 

0 is considered empty
1 is considered set
0

0 is considered empty
1 is considered set
1 =
0 is considered empty
1 is considered set
3
0 is considered empty
1 is considered set
4

0 is considered empty
1 is considered set
5

0 is considered empty
1 is considered set
6

0 is considered empty
1 is considered set
7
0 is considered empty
1 is considered set
8
0 is considered empty
1 is considered set
1
0 is set with isset function
1 is set with !empty function
0_______22_______1
0 is set with isset function
1 is set with !empty function
2

0 is set with isset function
1 is set with !empty function
3
0 is set with isset function
1 is set with !empty function
4_______22_______5
0 is set with isset function 
array is not set.
0

0 is set with isset function
1 is set with !empty function
7

đầu ra.

0 is set with isset function 
array is not set.

 

chức năng rỗng ()

Hàm empty() là một cấu trúc ngôn ngữ để xác định xem biến đã cho là rỗng hay NULL. Các. Hàm trống () là phủ định hoặc bổ sung của hàm trống (). Hàm trống () đáng kể bằng. hàm isset() và. Hàm empty() bằng với hàm isset()

Thí dụ.   

PHP




 

// PHP program to illustrate

0

 

1 2

 

3

4

0 is set with isset function 
array is not set.
1 6_______70_______76_______70_______1// PHP program to illustrate0

0 is set with isset function 
array is not set.
5
0 is considered empty
1 is considered set
7 1 // PHP program to illustrate4// PHP program to illustrate5
0 is set with isset function 
array is not set.
0

0 is set with isset function 
array is not set.
9

 

0 is considered empty
1 is considered set
7 // PHP program to illustrate9
0 is set with isset function 
array is not set.
0

 

// isset() function1

// isset() function2 // isset() function3

0 is set with isset function 
array is not set.
1 // isset() function576// isset() function2// PHP program to illustrate0

0 is set with isset function 
array is not set.
5
0 is considered empty
1 is considered set
7 // isset() function2 // PHP program to illustrate4_______73_______4
0 is set with isset function 
array is not set.
0

0 is set with isset function 
array is not set.
9

0 is set with isset function
1 is set with !empty function
7

đầu ra.

0 is considered empty
1 is considered set

 

Lý do để kiểm tra cả hai chức năng.  
Các isset() và. Các hàm trống () tương tự nhau và cả hai sẽ trả về cùng một kết quả. Nhưng sự khác biệt duy nhất là. Hàm empty() sẽ không tạo bất kỳ cảnh báo hoặc thông báo điện tử nào khi biến không tồn tại. Nó là đủ để sử dụng một trong hai chức năng. Bằng cách kết hợp cả hai chức năng trong một chương trình gây ra thời gian trôi đi và sử dụng bộ nhớ không cần thiết

Thí dụ.   

PHP




 

$num9

=0

 

=1

$num ='0'

0 is set with isset function 
array is not set.
0

 

=6

0 is set with isset function 
array is not set.
1=8$num
0 is set with isset function 
array is not set.
4

0 is set with isset function 
array is not set.
5
0 is set with isset function 
array is not set.
6$num // PHP program to illustrate4'0'5
0 is set with isset function 
array is not set.
8

0 is set with isset function 
array is not set.
9

 

'0'8

0 is considered empty
1 is considered set
7 // PHP program to illustrate9
0 is set with isset function 
array is not set.
0

 

0 is set with isset function 
array is not set.
02

$num // isset() function3

 

0 is set with isset function 
array is not set.
05

0 is set with isset function 
array is not set.
1
0 is set with isset function 
array is not set.
07_______70_______7 6$num
0 is set with isset function 
array is not set.
4

0 is set with isset function 
array is not set.
5
0 is set with isset function 
array is not set.
6_______73_______ // PHP program to illustrate4
0 is set with isset function 
array is not set.
16
0 is set with isset function 
array is not set.
8

0 is set with isset function 
array is not set.
9

đầu ra.

0 is set with isset function
1 is set with !empty function

 

PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này

Trống và rỗng có giống nhau trong PHP không?

Giá trị null thể hiện sự vắng mặt của bất kỳ đối tượng nào, trong khi chuỗi trống là đối tượng kiểu Chuỗi không có ký tự nào. Nếu bạn thử so sánh cả hai, chúng không giống nhau .

Sự khác biệt giữa rỗng và rỗng là gì?

Chuỗi rỗng là một thể hiện của chuỗi có độ dài bằng 0, trong khi chuỗi rỗng không có giá trị gì cả . Một chuỗi rỗng được biểu thị là "". Đó là một chuỗi ký tự gồm 0 ký tự. Một chuỗi null được đại diện bởi null.

Có trống == null không?

Sự khác biệt chính giữa null và trống là null được dùng để chỉ không có gì trong khi trống được dùng để chỉ một chuỗi duy nhất có độ dài bằng 0. A String refers to a sequence of characters.

Làm cách nào để kiểm tra xem một giá trị trống hay rỗng trong PHP?

Hàm empty() kiểm tra biến có rỗng hay không. Hàm này trả về false nếu biến tồn tại và không trống, ngược lại trả về true. .
SAI
mảng()