Javatpoint Logo
Javatpoint Logo

PHP empty ( ) Function

The empty() function is an inbuilt function in PHP, which is utilized to check regardless of whether a variable is unfilled or not.

Syntax

Parameters: This function acknowledges a solitary parameter as displayed in above syntax and portrayed beneath.

  • $variable name: Variable to check regardless of whether it is unfilled or not.

Note: Below version of PHP 5.5, empty() just backings factors, anything other will result in a parse mistake. The accompanying assertion won't function empty(trim($var)). All things considered, use trim($name) = = false.

Return type: It returns FALSE when $var exists and has a non-empty, non-zero worth. In any case it brings TRUE back.

Rundown of empty data types:

  • " 0 " (0 as a string)
  • 0 (0 as a number)
  • " " (as an empty string)
  • Invalid or NULL case
  • False
  • ' ' (as an unfilled string)
  • Array () (as an empty array)
  • $variable _ name; (a variable pronounced yet without a worth in a class)

Example 1:

Output:

PHP empty ( ) Function

As you can clearly see in the above example, we have used different variables, each with different empty data types, to show the case when an empty function will display true in each case of an empty variable in different data types. Also, we have used a ternary operator to make our program easy to understand.

Note: You can utilize the PHP empty() function to see if a variable is unfilled or not. A variable is viewed as empty in the event that it doesn't exist or on the other hand assuming its worth equivalents FALSE.

Example 2:

Output:

PHP empty ( ) Function

As you can clearly see in the above example, we have used different variables, each with different empty data types, to show the case when an empty function will display true in each case of an empty variable in different data types. Also, we have used a ternary operator to make our program easy to understand.

Example 3:

Output:

PHP empty ( ) Function

As you can clearly see in the above example, we have used 2 different variables, each with different data types, one with an integer with 0 as an initial value and the other string with some text to show the case when an empty function will display true in the case of an integer as 0 is considered empty whereas string will give false because it contains text inside it.

Example 4:

Output:

PHP empty ( ) Function

As you can clearly see in the above example, we have used multiple arrays nested inside each other to check how a nested array works with an empty function, and we can see that till array 8, it returns true, or 1 as all the arrays are empty or null, whereas after that it returns false.


Next TopicWhat is PHP-FPM?





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA