PHP isset() function

PHP isset is used to check whether the variable is set or not.The PHP isset() function returns false if variable contains a NULL value. Syntax:
isset(variable1, variable2….)
Presentation of PHP isset: Presentation of PHP isset
  • In the above diagram, the isset variable is null so it will return false.
Isset Variable
  • In the above diagram, the isset variable is 10, and it will return true. It means if the variable is set and not null, then the isset() function will return true.
Example (Null Variable):
<?php
$var=null;
if(isset($var))
{
echo "True";
}
else{
echo "False";
}
?>
Output:
False
  • In the above example, the variable is null, and it will return the false result.
Example (Not NullVariable):
<?php
$var=2;
if(isset($var))
{
echo "True";
}
else{
echo "False";
}
?>
Output:
True
  • In the above example, the variable has 2 value, and it will return true
Example:
<!DOCTYPE html>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
//define variables and set to empty values
$genderEr = "";
$gender = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["gender"]))
   {
    $genderEr = "Gender is required";
  }
else
   {
    $gender = test_input($_POST["gender"]);
  }
}
functiontest_input($data)
{
$data = trim($data);
$data = stripslashes($data);
return $data;
}
?>
<form method="post" action="<?php ($_SERVER["PHP_SELF"]);?>">
 Gender:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female");?> value="female">Female
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") ;?> value="male">Male
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="trans");?> value="trans">TransGender
<span class="error">* <?php echo $genderEr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

  • In the above example, the PHP isset() function is to check the variable ($gender) is set or not with a value( “female, male and trans”). 

PHP isset Examples

Example 1

Output

10 is set with isset function

Example 2

Output

Specified array is not set.

Example 3

Output

bool(true)
bool(true)

Example 4

Output

bool(false)
bool(true)

isset vs. empty vs. is_null :

  • isset() is to check if a variable is set with a value.
  • empty() is to check if a given variable is empty.
  • is_null() – It is to check whether a variable is defined as NULL.
Functions “” Null False Undefined 0
isset() True False True False True
empty() True True True True True
is_null() False True False Error False

Related Topics

PHP is_array() Function

The is_array() Function in PHP is used to  find whether a variable is an array or not. It returns a Boolean value true if the parameter var is an array else it returns...

1 minute read.

PHP in_array() Function

PHP in_array() Function The in_array() function in PHP checks if the value exists in the specified array or not. Syntax in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )  Parameter needle(required)- This parameter represents the searched value. haystack(required)- This parameter represents the input array. strict(optional)- This parameter...

1 minute read.

PHP Multidimensional Array

An array of arrays is called multidimensional array. It is used to store the data in a tabular form. It is represented in the form of matrix (row*column). Example: We can store...

2 minutes read.

PHP money_format() Function

PHP money_format() Function The money_format() function in PHP formats a number as a currency string and returns a formatted version of number. Syntax money_format ( string $format , float $number )  Parameter format(required)- This parameter specifies the string to be formatted and how to format the...

1 minute read.

PHP uasort() Function

PHP uasort() Function The uasort() function in PHP sorts an array with a user-defined comparison function and maintains the index association. Syntax uasort ( array &$array , callable $value_compare_func ) Parameter array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a...

1 minute read.

PHP lcfirst() Function

PHP lcfirst() Function The lcfirst() function in PHP converts the first character of a string to lowercase if that character is alphabetic. Syntax lcfirst ( string $str ) Parameter str(required)- This parameter signifies the input string. Return This function returns a string with the...

1 minute read.

PHP str_repeat() Function

PHP str_repeat() Function The str_repeat() function in PHP repeats a string a specified number of times. Syntax str_repeat ( string $input , int $multiplier ) Parameter string(required)- This parameter signifies the string to be repeated. multiplier(required)- This parameter represents the number of times the string will be...

1 minute read.

PHP addslashes() Function

PHP addslashes() Function The addslashed() function in PHP is used to return a string with backslashes in front of predefined characters. This function is only used with some characters which are...

1 minute read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.

PHP array_change_key_case() Function

PHP array_change_key_case() Function The array_change_key_case() function in PHP is used to change the case of all keys present in the specified array. It returns an array with all keys in the specified array to lowercase or...

1 minute read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

PHP array_shift() Function

PHP array_shift() Function The array_shift() in PHP shifts the first value of the array off. This function returns the same value after shortening the array by one element and moving everything. Syntax array_shift ( array &$array )  Parameter array(required)- This parameter represents the input array Return This function returns...

1 minute read.

PHP array_replace() Function

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP array_slice() Function

PHP array_slice() Function The array_slice() function in PHP is used to extract a slice of the array. It returns the sequence of elements from the array as specified by the offset and length parameters. Syntax array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) Parameter array(required)- This array represents the...

2 minutes read.

PHP For loop with Example

For loop in PHP : The for loop is the most potent loop, it combines the abilities to set up variables as we enter the loop, test for conditions while iterating...

3 minutes read.

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP implode() Function

PHP implode() Function The implode() function in PHP joins the specified array elements with a string. Syntax implode ( string $glue, array $pieces ) Parameter glue(optional)- It specifies what to put between the array elements. pieces(required)- This parameter represents...

1 minute read.

PHP array_unique() Function

PHP array_unique() Function The array_unique() function in PHP removes the duplicate values from the specified array. Syntax array_unique (array $array [, int $sort_flags = SORT_STRING]) Parameter array(required)- This array represents the first input array. sort_flags (optional)- This parameter is used to modify the sorting behavior. The...

1 minute read.

PHP array_search() Function

PHP array_search() Function The array_search() function in PHP searches the array for the specified value. This function returns the first corresponding key if the value is found. Syntax array_search ( mixed $needle , array $haystack [, bool $strict ])  Parameter needle(required)- This parameter represents the value to...

1 minute read.

PHP array_count_values() Function

PHP array_count_values() Function The array_count_values() function in PHP counts all the values present in the specified array and returns an array using the values of the array as keys and their frequency in the array as values. Syntax array_count_values ( array $array ) Parameter array(required)- This parameter represents the...

1 minute read.