PHP how to check variable if not empty
To check if your PHP variable is not empty, you can negate the empty() function using the bang operator (!).
The PHP empty() function is used to check whether a variable is empty or NULL.
A variable is considered empty when it has not been declared or defined with an empty string:
// ๐ this is an empty string
$name = "";
Here are some examples of if not empty check using PHP:
<?php
$name = "Nathan";
// ๐ if name is not empty, echo some text
if (!empty($name)) {
echo "NAME is not empty";
}
// ๐ if age is not empty, echo some text
if (!empty($age)) {
echo "AGE is not empty";
}
To check if a PHP variable is not empty, you can call the negated empty function or !empty() in your if statement.
More PHP tutorials
See allPHP
PHP form validation basic example
Dec 22, 2022
PHP
PHP validate email address (with code examples)
Dec 22, 2022
PHP
How to redirect using PHP with code examples
Dec 22, 2022
PHP
Handling form data using PHP (GET and POST)
Dec 21, 2022
PHP
How to fix Wordpress PHP error missing MySQL extension
Dec 21, 2022