What Is Isset() Function in PHP and How to Use It

Sometimes you require to check wheater the variable is set with the value or not. And based on the result, some further cations are taken in PHP.

It is necessary to check this type of condition when you get all data from the input fields of the form and set all data to different variables of PHP.

PHP provides an inbuilt function isset() for checking whether the variable is set with value or not. It is used when you get values from the form, and before writing queries, it is essential to check if all variables are set with values or not.

Read Also: How to Convert Str to Int in PHP?

What is isset() function in PHP?

It is the function used for checking whether the variable is set with the value or not.
It returns true or 1 when the variable is set with the value.
It returns false or 0 when the variable is not set with the value.

Syntax:

bool isset(var_name,mixed);

Explanation:

  • Here, the bool shows that the return type of isset() function is boolean.
  • var_name is name of the variable.
  • Mixed is an optional field, and it is used for diverse content.

Example 1

<!DOCTYPE html>
<html>
<body>

<?php

echo "[before] The result is: ";
echo isset($name)."<br>";

$name = "Errorsea";

echo "[After] The result is: ";
echo isset($name);
?>
</body>
</html>

Output:

[before] The result is:
[After] The result is: 1

Explanation:

  • First, we check the $name variable has assigned the value or null using isset() function and print the result.
  • Next, we assign the “Errorsea” string to a variable $name.
  • Then, we again check the assigned value using the isset() function and print the result.
  • Here first, the $name didn’t assign the value, so it returns nothing as a result. Then we assign the value, and this time it returns one as a result.

You can use this scenario for with if statement and print some message according to the result. This is the popularly used method for isset() function.

Let’s take one example which checks for values in an array and display the message according to the result.

Example 2

Suppose you have an array and check this array for value using the isset() function and display an error message if it has a null value.

<!DOCTYPE html>
<html>
<body>
<?php

$fruits = array();

echo "[before] The result is:<br>";

if(!isset($fruits["Strawberry"])){
echo "Strawberry is not in an array.<br>";
};

array_push($fruits,"Strawberry");

echo "[After] The result is:<br>";

if(isset($fruits)){
echo "Strawberry is set in an array.<br>";
};
?>
</body>
</html>

Output:

[before] The result is:
Strawberry is not in an array.
[After] The result is:
Strawberry is set in an array.

Explanation:

  • First, the array is empty, and we check the array using the isset() function and display an error message.
  • Next, we push the value Strawberry in the array using array_push function.
  • Then we again check the array and show the success message using if statement.

Conclusion

This is all about the isset() function of PHP. We can use this function when we want to check the value is assigned or not and display a message according to the result.

I hope you found this post fully informative and helpful.

Thank you for reading 🙂

Leave a Reply

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

Read previous post:
str to int in php
How to Convert Str to Int in PHP

Sometimes you require to convert the string into the number like int, float, or any other type. It is needed...

Close