Home » Blog » Uncategorized » What is isset in php

What is isset in php

isset is a built-in PHP function that checks if a variable is set and is not null. It is commonly used to verify the existence of variables before attempting to use them, thereby avoiding errors that may occur from trying to access undefined variables.

 

Syntax

bool isset(mixed $var, mixed …$vars)

 

  • $var: The variable to check.
  • $vars: Additional variables to check (optional).

Usage

  • Returns true if the variable exists and is not null.
  • Returns false if the variable does not exist or is null.

 

Example

<?php
$var1 = “Hello, World!”;
$var2 = null;

if (isset($var1)) {
echo “var1 is set and not null.”;
} else {
echo “var1 is either not set or is null.”;
}

if (isset($var2)) {
echo “var2 is set and not null.”;
} else {
echo “var2 is either not set or is null.”;
}

if (isset($var3)) {
echo “var3 is set and not null.”;
} else {
echo “var3 is either not set or is null.”;
}
?>

output

var1 and var2 are set and not null.
At least one of var1, var2, or var3 is either not set or is null.

 

Important Considerations

  • isset returns false for variables set to null.
  • It does not generate a warning if the variable does not exist, unlike some other PHP functions.

Using isset effectively helps in writing robust PHP code by ensuring that variables are checked for existence and value before they are used.

Leave a Reply