Isset in PHP Function With Examples

The isset in PHP is a built-in PHP function that checks if a variable is set or not. It simply means that it should be declared and not be NULL.

In this article, we will talk about PHP isset function in a detailed explanation as well as example programs that could be helpful to your future projects. This topic is a continuation of the previous topic, entitled PHP pathinfo Function.

What is Isset in PHP?

In PHP, isset is a function which use to determine once a variable is already declared and it is different than null. The isset function also checks if the declared array or array keys and variable have a null value. If it doesn’t declare, isset will return false otherwise false.

Syntax

isset(variable, ....);

Parameters

ParametersDescription
variableThis parameter is required and specifies a variable that needs to be checked.
This parameter is optional and specifies another variable that needs to be checked.

Example

<?php
      $var1 = 1;
      // True because $var1 is set
      if (isset($var1)) {
        echo "Variable 'First Variable' is set.";
      }
      
      $var2 = null;
      // False because $var2 is NULL
      if (isset($var2)) {
        echo "Variable 'Second Variable' is set.";
      }
?>

Output

Variable 'First Variable' is set.

What can I use instead of isset in PHP?

The ! empty() function can also e used instead of isset function they have the same functionalities in terms of checking a variable if they contain a value or null.

Example

<?php

      // PHP program to illustrate
      // empty() function
      
      $temporary = 0;
      
      // It returns true because
      // $temp is empty
      if (empty($temporary)) {
      	echo $temporary . ' is considered empty';
      }
      
      echo "\n";
      
      // It returns true since $new exist
      $new = 1;
      if (!empty($new)) {
      	echo $new . ' is considered set';
      }
      
?>

Output

0 is considered empty
1 is considered set

Why Isset is used?

The isset in PHP is always used for determining if the variable is set or not. Further, this function is also used to determine if you have used a variable in your entire code or not.

What is the difference between isset() and empty()?

The difference between isset and empty function is very simple, The isset is used to check if the variable is set and is not null. While the empty function is used to determine if the variable is not empty.

Summary

In summary, you have learned about isset in PHP function. This article also discussed what is Isset in PHP, what can we use instead of isset, why Isset is used, and what is the difference between isset() and empty().

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment