“Say Goodbye to Friday Bugs: Making isset() Work for You!"

jochelle mendonca
3 min readJul 14, 2024
Photo by Flipsnack on Unsplash

It’s Friday, and yet another bug has surfaced… My excitement for the upcoming weekend quickly fades as I realize I need to fix it by today.

Production issues on a Friday are just a party pooper, and I think we’ve all been there at some point.

Let me tell you, even though I was debugging on a Friday, I think I learned something new (well, kind of).

So, I was working with two amount fields and needed to use isset() to ensure both variables were set.

So, I wrote my code like this:

$amount1 = 20;
$amount2 = 40;
if (isset($amount1) && isset($amount2)) {
// some operations performed here
echo 'Both variables are set.';
}

In this example, isset($amount1) and isset($amount2) both return true because the variables are defined and not null.

In PHP, the isset() function is a fundamental tool for determining whether a variable is set and not null. Understanding its nuances and how to use it effectively can streamline your code and help prevent errors. (Read more about it here)

Then, something in me just wanted to check up on the isset() documentation, and along with the empty() function,

I discovered something surprising. I wondered why I didn't know this sooner.

You can use isset() to check multiple variables at once.

This is particularly useful when you need to ensure that several variables are set before proceeding with an operation.

$var1 = 'PHP';
$var2 = 'is awesome';
if (isset($var1, $var2)) {
echo 'Both variables are set.';
}

This usage returns true only if both $var1 and $var2 are set and not null.

Tips and Tricks for Using isset()

  1. Avoid Null Values: Remember that isset() returns false for variables set to null. Ensure your variables are not intentionally set to null if you plan to use isset() to check them.
$var = null; 
if (isset($var)) {
// This block will not execute because $var is null.
}

2. Combining with Other Functions: Combine isset() with other functions for more robust checks. For example, checking if a variable is set and not empty.

if (isset($var) && !empty($var)) {
echo 'The variable is set and not empty.';
}

3. Use with Arrays: isset() is often used to check if an array key exists and is not null.

$array = ['key1' => 'value1', 'key2' => null];  
if (isset($array['key1']))
{
echo 'key1 is set.';
}
if (!isset($array['key2'])) {
echo 'key2 is not set or is null.';
}

4. Short-Circuit Evaluation: When checking multiple variables, isset() uses short-circuit evaluation, meaning it stops checking as soon as it finds a variable that is not set or is null. This can improve performance slightly.

if (isset($var1, $var2, $var3)) {
echo 'All variables are set.';
}

5. Error Suppression: Using the @ operator with isset() can suppress errors for undefined variables. However, use this cautiously as it can hide potential issues in your code.

if (@isset($undefinedVar)) {
echo 'No error, but use with caution.';
}

Common Pitfalls

  • Misinterpreting false as Unset: Remember, isset() will return true for variables that are set to false. It only returns false for null or undefined variables.
$var = false; 
if (isset($var)) {
echo 'This will print because $var is set to false.';
}
  • Not Checking Array Keys Properly: When working with arrays, always ensure you are checking the correct keys.
array = ['key1' => 'value1'];  
if (isset($array['key2'])) {
// This will not execute because key2 does not exist in the array.
}

By leveraging this feature, you can write cleaner and more efficient code, making your debugging sessions a bit less stressful.

So, the next time you’re facing an end-of-the-week bug, remember these tricks with isset(). They might just save your weekend plans!

Happy coding!

If you liked this article and would like to support me, make sure to:

  • 👏Clap for this story
  • 🔔Follow me on Medium
  • ☕️ If you feel my work is worth an appreciation, you can buy me a coffee!

--

--

jochelle mendonca

Passionate PHP developer. Enthusiastic about the power of words, equally adept at reading and writing