PHP nếu không phải là biến rỗng

Hàm empty() chấp nhận một tham số duy nhất, như thể hiện trong cú pháp trên và được mô tả bên dưới. Nó ngược lại với hàm isset() trong PHP mà chúng ta đã thấy trước đó. Hàm empty() kiểm tra xem biến có trống không

cú pháp

Xem cú pháp sau

bool empty ($var)

Thông số

$var là biến đang được kiểm tra và là tham số bắt buộc

Dưới đây PHP 5. 5, trống() chỉ hỗ trợ các biến; . Câu lệnh sau sẽ không hoạt động trống(trim($var)). Thay vào đó, hãy sử dụng trim($name) == false

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

Ví 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_______2_______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_______20_______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_______20_______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()

Ví dụ.   

PHP




 

// PHP program to illustrate

0

 

1 2

 

3

4

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

0 is set with isset function 
array is not set.
5_______11_______7 1 // PHP program to illustrate4_______64_______5
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_______65_______2// PHP program to illustrate0

0 is set with isset function 
array is not set.
5_______11_______7 // isset() function2 // PHP program to illustrate4_______66_______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

Ví 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_______2_______6$num // PHP program to illustrate4_______68_______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.
077 6$num
0 is set with isset function 
array is not set.
4

0 is set with isset function 
array is not set.
5_______2_______6$num // PHP program to illustrate4_______2_______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

Nếu một biến không trống trong PHP thì sao?

Hàm PHP empty() . 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 .

Làm cách nào để kiểm tra nếu không NULL hoặc trống trong PHP?

empty() trong PHP ? . 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. isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.

Làm cách nào để sử dụng chức năng không trống trong PHP?

Để kiểm tra xem biến PHP của bạn có trống không, bạn có thể phủ định hàm empty() bằng cách sử dụng toán tử bang (. ) . Hàm PHP empty() được sử dụng để kiểm tra xem một biến rỗng hay NULL. Để kiểm tra xem một biến PHP có rỗng không, bạn có thể gọi hàm rỗng phủ định hoặc. trống() trong câu lệnh if của bạn.

Làm cách nào để kiểm tra xem chuỗi có trống trong PHP không?

Chúng ta có thể sử dụng hàm rỗng() để kiểm tra xem một chuỗi có rỗng hay không. Hàm dùng để kiểm tra chuỗi có rỗng hay không. Nó sẽ trả về true nếu chuỗi rỗng.