Hướng dẫn php check array empty

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    An empty array can sometimes cause software crash or unexpected outputs. To avoid this, it is better to check whether an array is empty or not beforehand. There are various methods and functions available in PHP to check whether the defined or given array is an empty or not. Some of them are given below:

    1. Using empty[] Function: This function determines whether a given variable is empty. This function does not return a warning if a variable does not exist.

      Syntax:

      bool empty[ $var ]

      Example:

      Output:

      Given Array is not empty 
      Given Array is empty
    2. Using count Function: This function counts all the elements in an array. If number of elements in array is zero, then it will display empty array.

      Syntax:

      int count[ $array_or_countable ]

      Example:

    3. Using sizeof[] function: This method check the size of array. If the size of array is zero then array is empty otherwise array is not empty.

      Example:

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    View Discussion

    Improve Article

    Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    An empty array can sometimes cause software crash or unexpected outputs. To avoid this, it is better to check whether an array is empty or not beforehand. There are various methods and functions available in PHP to check whether the defined or given array is an empty or not. Some of them are given below:

    1. Using empty[] Function: This function determines whether a given variable is empty. This function does not return a warning if a variable does not exist.

      Syntax:

      bool empty[ $var ]

      Example:

      Output:

      Given Array is not empty 
      Given Array is empty

    2. Using count Function: This function counts all the elements in an array. If number of elements in array is zero, then it will display empty array.

      Syntax:

      int count[ $array_or_countable ]

      Example:

    3. Using sizeof[] function: This method check the size of array. If the size of array is zero then array is empty otherwise array is not empty.

      Example:

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    Chủ Đề