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?
Index
Syntax
bool isset(var_name,mixed);
Explanation
<!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
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.
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
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 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…