PHP empty() Function
-- Learning more than just technology, but dreams!
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
PHP Tutorial
- PHP Tutorial
- PHP Introduction
- PHP Installation
- PHP Syntax
- PHP Variables
- PHP echo/print
- PHP EOF(heredoc)
- PHP Data Types
- PHP Type Comparisons
- PHP Constants
- PHP String
- PHP Operators
- PHP If...Else
- PHP Switch
- PHP Arrays
- PHP Array Sorting
- PHP Superglobals
- PHP While Loop
- PHP For Loop
- PHP Functions
- PHP Magic Constants
- PHP Namespaces
- PHP Object-Oriented
- PHP Quiz
PHP Forms
- PHP Forms
- PHP Form Validation
- PHP Form - Required Fields
- PHP Form - Validate Email and URL
- PHP Complete Form Example
- PHP $_GET Variable
- PHP $_POST Variable
PHP Advanced Tutorial
- PHP Multidimensional Arrays
- PHP Date
- PHP Include
- PHP Files
- PHP File Upload
- PHP Cookie
- PHP Session
- PHP E-mail
- PHP Secure E-mail
- PHP Error
- PHP Exception
- PHP Filters
- PHP Advanced Filters
- PHP JSON
PHP 7 New Features
PHP Database
- PHP MySQL Introduction
- PHP MySQL Connect
- PHP MySQL Create Database
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Insert Multiple Data
- PHP MySQL Prepared Statements
- PHP MySQL Read Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- PHP ODBC
PHP XML
PHP and AJAX
PHP Reference Manual
- PHP Array
- PHP Calendar
- PHP cURL
- PHP Date
- PHP Directory
- PHP Error
- PHP Filesystem
- PHP Filter
- PHP FTP
- PHP HTTP
- PHP Libxml
- PHP Mail
- PHP Math
- PHP Misc
- PHP MySQLi
- PHP PDO
- PHP SimpleXML
- PHP String
- PHP XML
- PHP Zip
- PHP Timezones
- PHP Image Processing
- PHP RESTful
- PHP PCRE
- PHP Available Functions
- PHP Composer
PHP Regular Expressions (PCRE)
PHP Dependency Management Tool
Deep Dive
- Web Design and Development
- Scripting Languages
- Development Tools
- Software
- Scripts
- Web Service
- Programming Languages
- Web Services
- Programming
- Computer Science
PHP empty() Function
The empty() function is used to check whether a variable is empty.
empty() determines whether a variable is considered empty. A variable is considered empty if it does not exist or its value is equivalent to FALSE. If the variable does not exist, empty() does not generate a warning.
Since PHP 5.5, empty() supports expressions, not just variables.
Version requirement: PHP 4, PHP 5, PHP 7
Syntax
bool empty ( mixed $var )
Parameter explanation:
- $var: The variable to be checked.
Note: Before PHP 5.5, empty() only supported variables; anything else would cause a parse error. In other words, the following code would not work:
empty(trim($name))
As an alternative, you should use:
trim($name) == false
empty() does not generate a warning, even if the variable does not exist. This means empty() is essentially equivalent to !isset($var) || $var == false.
Return Value
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise, returns TRUE.
The following variables are considered empty:
- "" (empty string)
- 0 (integer 0)
- 0.0 (float 0)
- "0" (string "0")
NULLFALSE- array() (an empty array)
- $var; (a variable that is declared but has no value)
Example
Example
<?php
$ivar1 = 0;
$istr1 = 'Tutorial';
if (empty($ivar1)) {
echo '$ivar1' . " is empty or 0." . PHP_EOL;
} else {
echo '$ivar1' . " is not empty or not 0." . PHP_EOL;
}
if (empty($istr1)) {
echo '$istr1' . " is empty or 0." . PHP_EOL;
} else {
echo '$istr1' . " string is not empty or not 0." . PHP_EOL;
}
?>
The execution result is as follows:
$ivar1 is empty or 0.
$istr1 string is not empty or not 0.
YouTip