PHP and MySQL (54 Blogs) Become a Certified Professional

Everything you Need to Know About Empty PHP

Published on Aug 19,2019 6 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

In a form, if someone puts something blank and we want to check if that was left blank using PHP script as we don’t want people leaving required information blank. Within an HTML form, you have the boxes for people to input data like username, password, and email. We want to create what is known for the required field which means they have to put that information without leaving the field blank. The following pointers will be covered in this Empty PHP article,

For example, if you are logging into a website, required fields are username and password because if you are going to leave any of them blank, you are not going to get granted access to the account. So using PHP, we want to check to make sure that they put in a value. In order to do this, we have 3 functions, isset(), empty() and is_null(). All these three functions return a boolean value. In this article, we are going to discuss about empty in PHP

 

What is empty() in PHP?

It is a built-in function in PHP which is used to check whether a variable is empty or not. You need to determine whether a variable is considered to be empty or not. If it does not exist or if its value equals FALSE, a variable is considered empty. If the variable does not exist, empty() does not generate a warning.

Empty PHP

You need to make sure that prior to PHP 5.5, empty() does not support anything other than variables. Anything else will result in a parse error.

 

Syntax for Empty

bool empty (var)

If var exists and has a non-empty or non-zero value, it returns FALSE

These are the following values which are considered to be empty()

  • “” (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • “0” (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)

<html>
<body>
<?php
if(isset($_POST['submit']))
{
if(empty ($_POST['fname']))
{
echo "first name is required"."
";
}
if(empty($_POST['lname']))
{
echo "Last Name is Required";
}
}
?>

<form action="empty.php" method="post">

<form>
First name:
<input type="text" name="fname">
Last name:
<input type="text" name="lname">
<input type="submit" name="submit" value="submit">
<input type="reset" name="reset" >
</form>

<body>
</html>

If we leave both the boxes empty and click on submit, it results in this way

Output-empty-php- Edureka

With this we come to an end of this article, I hope you understood the inbuilt function empty() in PHP.

If you found this Tutorial blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of thisarticle and I will get back to you.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.