Hướng dẫn php isset vs null

Hiểu về ISSET - EMPTY - IS_NULL trong PHP

PHP có các hàm khác nhau có thể được sử dụng để kiểm tra giá trị của một biến. Ba hàm hữu ích cho hàm này là isset (), empty () và is_null (). Tất cả các hàm này trả về một giá trị boolean. Nếu các chức năng này không được sử dụng đúng cách, chúng có thể gây ra các kết quả không mong muốn.

isset () và empty () thường được xem như các hàm ngược lại, tuy nhiên điều này không phải lúc nào cũng đúng. Trong bài này tôi sẽ giải thích sự khác biệt giữa các chức năng này.

Value of variable ($var)isset($var)empty($var)is_null($var)
“” (an empty string) bool(true) bool(true)  
” ” (space) bool(true)    
FALSE bool(true) bool(true)  
TRUE bool(true)    
array() (an empty array) bool(true) bool(true)  
NULL   bool(true) bool(true)
“0″ (0 as a string) bool(true) bool(true)  
0 (0 as an integer) bool(true) bool(true)  
0.0 (0 as a float) bool(true) bool(true)  
var $var; (a variable declared, but without a value)   bool(true) bool(true)
NULL byte (“\ 0″) bool(true)    

isset() là gì ?

isset - Xác định nếu một biến được đặt và không phải là NULL. Nói cách khác, nó trả về true chỉ khi biến không phải là null.

Hướng dẫn php isset vs null

Xem Thêm: PHP manual – isset()

empty() là gì?

- empty() Xác định xem biến có trống không

Nói cách khác, nó sẽ trả về true nếu biến là một chuỗi rỗng, false, array (), NULL, “0 ?, 0 và một biến chưa được đặt.

Xem Thêm: PHP Manual – empty()

is_null() là gì ?

is_null - Tìm một biến là NULL

Nói cách khác, nó trả về true chỉ khi biến là null. is_null () đối diện với isset (), ngoại trừ một sự khác biệt mà isset () có thể được áp dụng cho các biến không xác định, nhưng is_null () chỉ với các biến được khai báo.

Bảng dưới đây là một tham chiếu dễ dàng cho những chức năng này sẽ trả lại cho các giá trị khác nhau. Các khoảng trắng có nghĩa là hàm trả về bool (false).

Xem thêm: PHP Manual – is_null()

 Đó là lý do tôi viết bài này, nhiều bạn bây giờ không phân biệt được đâu là isset, đâu là is_null luôn . hy vọng bài viết này sẽ giúp các bạn bổ trợ được thêm một tí kiến thức cũ nhé.

BIÊN SOẠN:

CÔNG TY TNHH THƯƠNG MẠI ĐIỆN TỬ CÔNG NGHỆ LP

Giấy phép cung cấp dịch vụ Viễn thông số:0315561312/GP – BTTTT cấp ngày 13 tháng 03 năm 2019

Địa chỉ: 96/76 Đường Trục, Phường 13, Quận Bình Thạnh, HCMC

Hotline: 0963 400 885

Mail: 

Hướng dẫn php isset vs null

Thông tin tác giả

Hướng dẫn php isset vs null

LÊ VĂN PHÚ

LPTech được thành lập với mong muốn đem những công nghệ mới nhất cho các doanh nghiệp trong nước. Công ty chúng tôi chuyên Thiết kế website, Dịch vụ SEO và Chỉnh sửa website. Các vấn đề đến hệ thống mạng, lập trình ứng dụng. Uy tín là danh dự của LPTech. Chúng tôi không chấp nhận một sản phẩm kém chất lượng đến tay khách hàng.

You will use variables in almost every program that you write using PHP. Most of the time these variables have a value, and we usually create them with an initial value. However, there is always a possibility that some of the variables you are using are not initialized. This can result in a warning from PHP about using an undefined variable.

There can be many reasons for undefined variables. The most common ones are that you either actually did not define the variable or you made a spelling mistake when reusing it somewhere else. This is just a programming bug. However, another possibility that can result in an undefined variable is that it has been defined conditionally.

You also might find that a variable has the value NULL. This also can happen for a number of reasons. For example, the variable might just have not been initialized with a value. Or the null value might be returned from a function to signal some sort of error.

In any case, using a variable before it has been defined or when it has a null value can have unintended consequences. In this tutorial, I'll show you how to check if an element has been defined and see if it is empty or null.

You can use isset(), empty(), or is_null() to check if either one or all of those conditions are true or false.

Definitions

Let's get started with some definitions.

  1. isset(): You can use isset() to determine if a variable is declared and is different than null.
  2. empty(): It is used to determine if the variable exists and the variable's value does not evaluate to false.
  3. is_null(): This function is used to check if a variable is null.

PHP isset() vs. empty()

As we saw from the definitions, isset() will return true if we have defined the variable before and set its value to something other than NULL. This can include 0, an empty string, or false. On the other hand, empty() will return true whenever the variable value is set to something that evaluates to false—we call these "falsey" values. Examples of falsey values include 0, the empty string "" and the string "0", an empty array, NULL, or of course the boolean false.

One similarity between isset() and empty() is that they are both language constructs and therefore cannot be called using variable functions.

The following code snippet should explain the difference between these two.

Note that empty() can be written using the isset() function:

Of course, it's usually just easier to use the built-in empty() function.

PHP isset() vs. is_null()

The is_null() function returns true if the value of a variable has been explicitly set to NULL. Otherwise, it simply returns false. On the other hand, isset() will return true as long as a variable is defined and its value is not NULL.

Here is a basic example to show the difference between them.

PHP empty() vs. is_null()

The empty() function returns true if the value of a variable evaluates to false. This could mean the empty string, NULL, the integer 0, or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL.

Here is a basic example to show the difference between them.

 'Monty', 'last_name' => '', 'age' => '83', 'fav_movie' => NULL];

if(empty($person['last_name'])) {
    if(is_null($person['last_name'])) {
        echo 'The last name is set to NULL.';
    } else {
        echo 'The last name is probably an empty string.';
    }
}
// Output: The last name is probably an empty string.

if(is_null($person['fav_movie'])) {
    echo $person['first_name'].' did not specify a favorite movie.';
}
// Output: Monty did not specify a favorite movie.

Important Points to Remember

There are two tips that you can use to write more concise code and avoid errors in future.

1. Unlike empty() and is_null(), you can pass multiple values to isset() at once to simultaneously check if any of them is undefined or set to NULL. In that case, isset() will only return true if none of the passed values are NULL.

2. Don't use == to check if a value is NULL. This will give a false positive for falsey values like an empty string that evaluate to false.

Final Thoughts

This tutorial quickly explained the difference between isset(), empty(), and is_null(). Hopefully, you will now be able to determine which of them is right for you to use in your code.

Did you find this post useful?

Hướng dẫn php isset vs null

Freelancer, Instructor

I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around.