Hướng dẫn php check empty string - kiểm tra php chuỗi trống

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọcempty() function to check whether a string is empty or not.

    Bàn luận

    Syntax:

    bool empty(string)

    Trong bài viết này, chúng ta sẽ xem cách kiểm tra chuỗi trống trong PHP. Chuỗi là một tập hợp các ký tự. Một chuỗi được cho là trống, nếu nó không chứa ký tự. Chúng ta có thể sử dụng hàm trống () để kiểm tra xem chuỗi có trống hay không.Variable to check whether it is empty or not.

    Hàm được sử dụng để kiểm tra xem chuỗi có trống hay không. Nó sẽ trả về true nếu chuỗi trống. If string is empty, it returns true and false otherwise.

    Tham số: Biến để kiểm tra xem nó có trống hay không.PHP program to check whether the string is empty or not.

    Giá trị trả về: Nếu chuỗi trống, nó sẽ trả về đúng và sai nếu không.

    Ví dụ 1: Chương trình PHP để kiểm tra xem chuỗi có trống hay không.

    PHP

    $s = ""

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    1

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    1
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    3
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    7
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    8
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    9
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    1

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    9

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    2
    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    3

    Giá trị trả về: Nếu chuỗi trống, nó sẽ trả về đúng và sai nếu không.

    Ví dụ 1: Chương trình PHP để kiểm tra xem chuỗi có trống hay không.

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    1
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    3
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2$s
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    6

    $s = ""

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    1

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    1
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    3
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    7
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    8
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    9
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    1

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    9

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    2
    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    3

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    7
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    8
    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    6
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0


    Ví dụ 2:

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    

    $s =

    if (isset($str) && (string) $str !== '') {
        // variable set, not empty string, not falsy
    }
    
    3
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0

    Bạn cần

    bool empty(string)
    4 trong trường hợp
    bool empty(string)
    5 có thể không xác định được:

    if (isset($str) && (string) $str !== '') {
        // variable set, not empty string, not falsy
    }
    


    Sử dụng

    bool empty(string)
    6 sẽ có một cảnh báo quan trọng: chuỗi
    bool empty(string)
    7 đánh giá là sai.


    Ngoài ra, đôi khi người ta muốn kiểm tra, ngoài ra,

    bool empty(string)
    5 không phải là thứ gì đó giả, như
    bool empty(string)
    9 hoặc ________ 60 [1]. Mã trước không xử lý việc này. Đó là một trong những tình huống hiếm hoi mà so sánh lỏng lẻo có thể hữu ích:


    Phương pháp trên rất thú vị vì nó vẫn ngắn gọn và không lọc ra

    bool empty(string)
    7. Nhưng hãy chắc chắn ghi lại mã của bạn nếu bạn sử dụng nó.

    Nếu không, bạn có thể sử dụng phiên bản tương đương nhưng nhiều hơn:

    Tất nhiên, nếu bạn chắc chắn

    bool empty(string)
    5 được xác định, bạn có thể bỏ qua 3 từ các mã trên.

    class Request
    {
    
        // This is the spirit but you may want to make that cleaner :-)
        function get($key, $default=null, $from=null)
        {
             if ($from) :
                 if (isset(${'_'.$from}[$key]));
                    return sanitize(${'_'.strtoupper($from)}[$key]); // didn't test that but it should work
             else
                 if isset($_REQUEST[$key])
                    return sanitize($_REQUEST[$key]);
    
             return $default;
        }
    
        // basics. Enforce it with filters according to your needs
        function sanitize($data)
        {
              return addslashes(trim($data));
        }
    
        // your rules here
        function isEmptyString($data)
        {
            return (trim($data) === "" or $data === null);
        }
    
    
        function exists($key) {}
    
        function setFlash($name, $value) {}
    
        [...]
    
    }
    
    $request = new Request();
    $question= $request->get('question', '', 'post');
    print $request->isEmptyString($question);
    

    Cuối cùng, xem xét rằng 4, 5, nhưng 6, bạn có thể đã đoán nó: so sánh PHP không chuyển tiếp (bao gồm các biểu đồ thú vị).

    [1] Lưu ý rằng

    bool empty(string)
    4 đã lọc ra 0.

    Không có cách nào tốt hơn nhưng vì đó là một hoạt động bạn thường thực hiện khá thường xuyên, nên bạn nên tự động hóa quá trình.

    Hầu hết các khung cung cấp một cách để làm cho các đối số phân tích một nhiệm vụ dễ dàng. Bạn có thể xây dựng đối tượng riêng của mình cho điều đó. Ví dụ nhanh chóng và bẩn thỉu:

    Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọcempty() function to check whether a string is empty or not.

    Bàn luận

    Syntax:

    bool empty(string)

    Trong bài viết này, chúng ta sẽ xem cách kiểm tra chuỗi trống trong PHP. Chuỗi là một tập hợp các ký tự. Một chuỗi được cho là trống, nếu nó không chứa ký tự. Chúng ta có thể sử dụng hàm trống () để kiểm tra xem chuỗi có trống hay không.Variable to check whether it is empty or not.

    Hàm được sử dụng để kiểm tra xem chuỗi có trống hay không. Nó sẽ trả về true nếu chuỗi trống. If string is empty, it returns true and false otherwise.

    Tham số: Biến để kiểm tra xem nó có trống hay không.PHP program to check whether the string is empty or not.

    Giá trị trả về: Nếu chuỗi trống, nó sẽ trả về đúng và sai nếu không.

    Ví dụ 1: Chương trình PHP để kiểm tra xem chuỗi có trống hay không.

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    1
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    3
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2$s
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    6

    $s = ""

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    1

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    1
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    3
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    7
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    8
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    9
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    1

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    9

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    2
    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    3

    Giá trị trả về: Nếu chuỗi trống, nó sẽ trả về đúng và sai nếu không.

    Ví dụ 1: Chương trình PHP để kiểm tra xem chuỗi có trống hay không.

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    1
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    3
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2$s
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    6

    $s = ""

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    1

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    1
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    3
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    2

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    7
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    8
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    9
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    1

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    9

    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    2
    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    30 value represents a variable with no value. 0 is the only possible value of type null.

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    7
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    8
    if (isset($str) && $str != '') {
        // variable set, not empty string, not falsy
    }
    
    6
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    0null if:

    • Ví dụ 2:0.

    • $s =

      if (isset($str) && (string) $str !== '') {
          // variable set, not empty string, not falsy
      }
      
      3
      if (isset($str) && $str !== '') {
          // variable set, not empty string
      }
      
      0

    • Bạn cần

      bool empty(string)
      4 trong trường hợp
      bool empty(string)
      5 có thể không xác định được:unset().

    Sử dụng bool empty(string)6 sẽ có một cảnh báo quan trọng: chuỗi bool empty(string)7 đánh giá là sai.

    Ngoài ra, đôi khi người ta muốn kiểm tra, ngoài ra,

    bool empty(string)
    5 không phải là thứ gì đó giả, như
    bool empty(string)
    9 hoặc ________ 60 [1]. Mã trước không xử lý việc này. Đó là một trong những tình huống hiếm hoi mà so sánh lỏng lẻo có thể hữu ích:null, and that is the case-insensitive constant 0.

    Phương pháp trên rất thú vị vì nó vẫn ngắn gọn và không lọc ra

    bool empty(string)
    7. Nhưng hãy chắc chắn ghi lại mã của bạn nếu bạn sử dụng nó.is_null() and unset().

    Nếu không, bạn có thể sử dụng phiên bản tương đương nhưng nhiều hơn:

    Tất nhiên, nếu bạn chắc chắn

    bool empty(string)
    5 được xác định, bạn có thể bỏ qua 3 từ các mã trên.

    Tính năng này đã được không dùng nữa kể từ Php 7.2.0 và được loại bỏ kể từ Php 8.0.0.Dựa vào tính năng này là rất nản lòng.

    Đúc một biến thành NULL bằng cách sử dụng

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    21 sẽ không loại bỏ biến hoặc giải phóng giá trị của nó.Nó sẽ chỉ trả về giá trị 0.null using
    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    21 will not remove the variable or unset its value. It will only return a 0 value.

    chọn nhanh ¶

    11 năm trước

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    23

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    24

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    25

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    26

    Mattias tại Kregert dot se ¶

    1 năm trước

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    27

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    28

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    29

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    30

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    31

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    32

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    33

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    34

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    26

    Hayley Watson ¶

    4 năm trước

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    36

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    37

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    38

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    26

    Ẩn danh ¶

    4 năm trước

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    40

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    41

    Ẩn danh ¶

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    43

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    26

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    42

    1 năm trước

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    45

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    46

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    47

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    48

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    49

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    26

    Hayley Watson ¶

    1 năm trước

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    51

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    52

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    53

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    54

    if (isset($str) && $str !== '') {
        // variable set, not empty string
    }
    
    26