Kiểm tra null PHP

Toán tử an toàn null cho phép đọc giá trị của chuỗi giá trị trả về thuộc tính và phương thức, trong đó toán tử an toàn null ngắt mạch truy xuất nếu giá trị là

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4 mà không gây ra bất kỳ lỗi nào

Cú pháp tương tự như toán tử truy cập thuộc tính/phương thức [

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
5] và theo mẫu kiểu nullable, toán tử an toàn null là
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
6

$foo?->bar?->baz;

Toán tử an toàn null âm thầm trả về

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4 nếu biểu thức ở phía bên trái ước tính là
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];

Trong đoạn mã trên, giá trị trả về của phương thức

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
9 là nullable;

Chuỗi

Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
2 không phải là "null safe", bởi vì giá trị trả về của
Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
3 có thể là
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4 và PHP đưa ra lỗi khi cố gọi phương thức
Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
5

Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...

Để truy cập địa chỉ một cách an toàn, cần phải kiểm tra các giá trị trả về của

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4 trước khi tiếp tục truy cập vào giá trị trả về

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
6
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
7

Toán tử an toàn null giải quyết vấn đề này bằng cách rút ngắn quyền truy cập thuộc tính/phương thức và trả về

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4 ngay lập tức nếu phía bên trái của toán tử là
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4, mà không thực hiện phần còn lại của biểu thức

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
0

Toán tử an toàn null

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
6 có thể giúp giảm thiểu việc kiểm tra quá mức
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
60 và ternary

Toán tử an toàn không có giá trị đã được thêm vào PHP 8. 0 giữa bản phát hành alpha 3 và bản beta đầu tiên, ngay trong khoảng thời gian PHP 8. 0 đạt đến đóng băng tính năng của nó

Toán tử an toàn null là chỉ đọc. Nó không thể ghi/gán giá trị từ nó

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
3

Đoạn mã này cố ghi vào thuộc tính

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
61 của đối tượng
Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
1. Điều này không được phép với toán tử an toàn null

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
6

Toán tử an toàn null có thể được xâu chuỗi và toàn bộ biểu thức sẽ bị rút ngắn từ toán tử an toàn null đầu tiên gặp phải null

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
7

Chuỗi cuộc gọi này không an toàn. PHP sẽ dừng và ngay lập tức trả về

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4 nếu bất kỳ toán tử trả về an toàn null nào đánh giá là
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4

{code-ok} Người vận hành Null-safe phải có mặt ở mọi bước mà chuỗi có thể bị đoản mạch. Sử dụng toán tử

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
6 sẽ không làm cho toàn bộ chuỗi trở nên vô hiệu

Toán tử an toàn null được đánh giá từ trái sang phải. Quan trọng nhất, nó không ưu tiên các lệnh gọi hàm khác hoặc các mẫu truy cập khác, chẳng hạn như truy cập mảng

$foo?->bar?->baz;
1

Nếu phương thức

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
66 trả về một đối tượng
Fatal error: Uncaught Error: Call to a member function getCountry[] on null in ...:...
1 hợp lệ, thì
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
68 sẽ được thực thi và được chuyển đến lệnh gọi phương thức
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
69. Bởi vì các toán tử an toàn null được đánh giá từ trái sang phải, phương thức
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
70 sẽ không bao giờ được gọi nếu phương thức
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
9 trả về
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4

Lưu ý rằng việc sử dụng dấu ngoặc nhọn sẽ làm cho biểu thức bên trong dấu ngoặc nhọn thực thi hoàn toàn, ngược lại với mẫu viết từ trái sang phải

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
0

Nếu

class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
9 trả về một giá trị khác với
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
4, chuỗi
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
75 sẽ được thực thi và giá trị trả về sẽ được chuyển đến lệnh gọi
class Customer {
    public function getAddress[]: ?Address {}
}
class Address {
    public function getCountry[]: string {}
}

$country = $customer->getAddress[]->getCountry[];
69

Làm cách nào để kiểm tra xem một giá trị có phải là null trong PHP không?

Hàm is_null[] kiểm tra xem một biến có phải là NULL hay không. Hàm này trả về true [1] nếu biến là NULL, ngược lại trả về false/nothing.

Kiểm tra trống có NULL PHP không?

?> Đầu ra. 0 được đặt với mảng hàm isset không được đặt. 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 .

LÀ NULL hay 0 PHP?

Để xác định là null hoặc zero bởi. is_int[$var] nếu biến là số hoặc chuỗi số. Để xác định Zero, sử dụng is_numeric[$var] cũng là giải pháp hoặc sử dụng $var === 0. is_null[$var] nếu một biến là NULL

Giá trị null trong PHP là gì?

Trong PHP, một biến không có giá trị được gọi là kiểu dữ liệu null. Một biến như vậy có một giá trị được xác định là NULL. Một biến có thể được gán rõ ràng NULL hoặc giá trị của nó được đặt thành null bằng cách sử dụng hàm unset[].

Chủ Đề