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 strtok() Function

PHP strtok() Function The strtok() function in PHP splits a string into smaller strings or tokenize the specified string.  Syntax strtok ( string $str , string $token ) Parameter str(required)- This parameter represents the input string to split into smaller strings or tokens. token(required)- It signifies...

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 natsort() Function

PHP natsort() Function The natsort() function in PHP Sort an array using a case insensitive "natural order" algorithm while maintaining the keys. Syntax natsort ( array &$array ) Parameter array(required)- This parameter represents the input array. Return This parameter returns a Boolean value TRUE on success or FALSE on...

1 minute read.

PHP join() Function

PHP join() Function The join() function of PHP returns a string from the elements of an array. This function is an alias of the implode() function. Syntax join(separator,array) Parameter separator(Optional)- Specifies what to put between the array elements. The default...

1 minute read.

PHP stripcslashes() Function

PHP stripcslashes() Function The stripcslashes() function in PHP returns a string with backslashes(\n, \r ..., octal and hexadecimal representation) stripped off. Syntax stripcslashes ( string $str )  Parameter str(required)- This parameter signifies the string to be unescaped. Return This function returns the unescaped string with backslashes stripped off. Example 1   ...

1 minute read.

PHP wordwrap() Function

PHP wordwrap() Function The wordwrap() Function wraps a string to a given number of characters using a string break character. Syntax wordwrap ( string $str [, int $width [, string $break  [, bool $cut  ]]] ) Parameter str(required)- This parameter specifies the input string.       width(optional)- This parameter represents the number of...

1 minute read.

PHP is_real() Function

The PHP is_real () function in PHP is used to find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter...

1 minute read.

PHP array_flip() function

PHP array_flip() function The array_flip() function in PHP exchanges all keys with their associated values in an array and returns an array in flip order. If the specified value has several occurrences, the latest key will be...

1 minute read.

PHP array_reduce() Function

PHP array_reduce() Function The array_reduce() function in PHP reduces the array to a single value using by applying a callback function to the elements of the array. Syntax array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) Parameter array(required)- This parameter represents the input array. callback (required)- This...

1 minute read.

PHP Include Function

PHP Include( ) The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the "include" statement. With the help of include...

3 minutes read.

PHP strtr() Function

PHP strtr() Function The strtr() function in PHP translates or replaces certain characters in a string. Syntax strtr ( string $str , string $from , string $to ) or strtr ( string $str , array $replace_pairs ) Parameter str(required)- This parameter specifies the string to translate from(required)- This parameter represents the string being translated to the parameter ‘to’. to(required)- It specifies what...

1 minute read.

PHP gettype() Function

PHP gettype() Function The gettype () function in PHP gets the type of the specified variable. Syntax gettype ( mixed $var ) Parameter var(required)- This parameter represents the variable being type checked. Return This function returns the type of the PHP variable specified by “var”...

1 minute read.

How to use Date and Time in PHP

HOW TO USE DATE AND TIME IN PHP: PHP time and date function  informs us about current date and time. it can be manipulated also. How time() function works: The time() function...

2 minutes read.

PHP is_countable() Function

The PHP is_countable() function in PHP is used to  find whether a variable is countable or not. It returns a Boolean value true if the parameter var is countable else it returns false. Syntax is_countable ( mixed...

1 minute read.

PHP strtoupper() Function

The strtoupper() function in PHP is used to convert a string to uppercase. This function is binary-safe. The related functions are as follows: strtolower()lcfirst()ucfirst()ucwords() Syntax strtoupper ( string $string ) Parameter string(required)- This parameter represents the input string. Return This function returns a string with all alphabetic...

1 minute read.

PHP array() function

PHP array() function The array() function in PHP is used to create an array. Syntax array ([ mixed $... ] ) Parameter …(optional)- This parameter represents the elements’ values along with keys of the array. The syntax of this is as follows: "index => values"...

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 array_unshift() Function

PHP array_unshift() Function The array_unshift () function in PHP prepends one or more elements to the beginning or front of an array. It returns the new number of elements in the array. Syntax array_unshift ( array &$array [, mixed $... ] ) Parameter array(required)- This array represents...

1 minute read.

PHP ucfirst() Function

PHP ucfirst() Function The ucfirst() function in PHP converts the first character of the specified string to uppercase. Syntax ucfirst ( string $str ) Parameter str(required)- This parameter represents the input string. Return This function returns a string with the first character of the specified string capitalized if that...

1 minute read.

PHP crypt() Function

PHP crypt() Function The crypt () function in PHP returns a hashed string using DES, Blowfish, or MD5 algorithms. Some constants of crypt() function are as follows: [CRYPT_STD_DES][CRYPT_EXT_DES][CRYPT_MD5][CRYPT_BLOWFISH][CRYPT_SHA_256][CRYPT_SHA_512] etc. Syntax crypt ( string $str [, string $salt ] )  Parameter str(required)- This parameter represents the string to be hashed salt(optional)- This parameter...

2 minutes read.