Web20 University

The PHP isset() Function (practical examples)

Get up to 65% Off Hosting with FREE SSL & FREE Domains!

* Web 2.0 University is supported by it's audience. If you purchase through links on our site, we may earn an affiliate commision.

TL;DR

The build in isset function in PHP determines whether a particular variable has been set and is not NULL. You can use it to check if a variable exists or properties on an object exist before trying to access them.

As the result of the PHP isset function returns a boolean (true/false) it is really handy to use it in combination with an if statement.

This article will go in to more detail about how to use the PHP isset function.

What is PHP isset()?

The isset() function is a built-in PHP function used to determine if a variable is set and is not NULL. It’s particularly useful for checking the existence of variables, array elements, or object properties before using them, thus helping to prevent errors in your code.

Key points about isset():

  • It returns true if the variable exists and has any value other than NULL.
  • It returns false if the variable does not exist or has a NULL value.
  • It can check multiple variables at once.

An Overview of PHP isset() for Beginners

For newcomers to PHP, isset() is one of the first functions you’ll likely encounter when dealing with form submissions or handling potentially undefined variables. Here’s a basic syntax:

isset($variable);

This function is often used in conditional statements:

if (isset($variable)) {
    // Do something with $variable
} else {
    // Handle the case where $variable is not set
}

Practical Examples of Using PHP isset()

Let’s look at some common scenarios where isset() proves useful:

1. Checking Form Submissions

if (isset($_POST['submit'])) {
    // Process the form
    $username = $_POST['username'];
    // ... more processing
} else {
    echo "Please submit the form.";
}

2. Validating Array Keys

$user = ['name' => 'John', 'email' => '[email protected]'];

if (isset($user['age'])) {
    echo "User's age: " . $user['age'];
} else {
    echo "Age not provided";
}

3. Checking Multiple Variables

if (isset($firstname, $lastname, $email)) {
    // All three variables are set
    registerUser($firstname, $lastname, $email);
} else {
    echo "Please provide all required information.";
}

4. Ternary Operator with isset()

$username = isset($_GET['user']) ? $_GET['user'] : 'Guest';
echo "Welcome, $username!";

Alternatives to Using PHP isset()

While isset() is versatile, there are situations where other methods might be more appropriate:

1. empty()

The empty() function checks if a variable is empty (including NULL, false, 0, “”, array(), etc.).

if (empty($var)) {
    echo "Variable is empty or not set";
}

2. array_key_exists()

For checking if an array key exists, regardless of its value:

$array = ['key' => null];

if (array_key_exists('key', $array)) {
    echo "Key exists, even though its value is null";
}

3. Null Coalescing Operator (??)

Introduced in PHP 7, this operator provides a concise way to check and assign default values:

$username = $_GET['user'] ?? 'Guest';

4. property_exists()

For checking if a class property exists:

if (property_exists($object, 'property')) {
    echo "The property exists in this object";
}

Conclusion

The isset() function is a powerful tool in a PHP developer’s toolkit, essential for preventing errors and ensuring smooth code execution. By understanding when and how to use isset(), as well as knowing its alternatives, you can write more robust and error-resistant PHP code.

Remember, while isset() is incredibly useful, it’s important to choose the right tool for each specific scenario. Sometimes, alternatives like empty(), array_key_exists(), or the null coalescing operator might be more appropriate. As you gain experience, you’ll develop a sense for which method to use in different situations.

As always, you can find more information in the PHP official documentation for isset.

Get up to 65% Off Hosting with FREE SSL & FREE Domains!