PHP empty() string function



PHP empty() function

The PHP empty() function used to check whether the string is empty or not.

In PHP the empty() function return true if the variable has empty or zero value and it return false if string has non-empty or non-zero value.

PHP empty() function syntax:

empty(“string”);

PHP example of empty() function :

Here, we take an example of empty() function. create a php form with one textbox with a submit button control. we do here when user click the submit button if the textbox value is blank so the result will be error message like “please enter username !!”, or if already entered some value then click submit button this time result will be “welcome user” on screen.

<html>
<head>
<title>PHP empty() Function</title>
</head>
<body>

<FORM method="POST" action="firstexample.php">

Name : <input type="text" name="name"> <br/>
<br/>
<input type="submit" name="Submit1" value="check empty()">

</FORM>

<?php
if(isset($_POST['Submit1']))
{
if(empty($_POST["name"]))
{ 
echo "Please enter username";
}
else
{
echo "welcome " . $_POST["name"];
}
}
?>

</body>
</html>

When we run above php example then the result will be like :

When we input “meera” string in textbox the result will be “welcome meera” but if we enter “0” digit value it will return error message “please enter username” because the for the empty() function digit zero(0) and empty string both are same.

PHP empty() function example
PHP empty() function example

In above php example we use empty() function to check whether textbox value empty of not. Here, we use isset() function to confirm the submit1 button is clicked or not. The php code execute if only the submit1 button is clicked.

Leave a Reply

Your email address will not be published. Required fields are marked *