• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / PHP / Check if Variable Exists in php with isset() Function

Check if Variable Exists in php with isset() Function

March 17, 2022 Leave a Comment

In php, we can check if a variable exists with the php isset() function.

$variable = "I'm a variable";

if (isset($variable)) {
    echo "variable exists!";
} else {
   echo "variable doesn't exist!"
}

//Output:
variable exists!

When working with variables in our php programs, it is useful to be able to check if a variable exists or not.

We can check for the existence of a variable with the php isset() function.

If the variable exists, isset() return true. Otherwise, isset() returns false.

Below are some examples of using isset() in php to check if a variable exists.

$variable1 = "I'm a variable";

if (isset($variable1)) {
    echo "variable1 exists!";
} else {
   echo "variable1 doesn't exist!";
}

if (isset($variable2)) {
    echo "variable2 exists!";
} else {
   echo "variable2 doesn't exist!";
}

//Output:
variable1 exists!
variable2 doesn't exist!

How to Check if a Function Exists in php with function_exists()

Another useful function in php is the function_exists() function, which allows you to check the existence of a given function.

We can use function_exists() in a similar way to isset() to check if a function exists.

If the function exists, function_exists return true. Otherwise, function_exists returns false.

Below are some examples of testing to see if a function exists in php.

if (function_exists('max')) {
    echo "max() exists!";
} else {
   echo "max() doesn't exist!";
}

if (function_exists('other_function')) {
    echo "other_function() exists!";
} else {
   echo "other_function() doesn't exist!";
}

//Output:
max() exists!
other_function() doesn't exist!

Hopefully this article has been useful for you to learn how to check if a variable exists or not in php.

Other Articles You'll Also Like:

  • 1.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 2.  php range() – Create Array of Elements Within Given Range
  • 3.  Get Last Day of Month in php
  • 4.  Add Months to Date in php
  • 5.  php session_destroy() – Delete Session File Where Session Data is Stored
  • 6.  php Absolute Value with abs Function
  • 7.  Get Array Length with php count() Function
  • 8.  php property_exists() – Check if Property Exists in Class or Object
  • 9.  php array_merge() – Merge One or More Arrays Together by Appending
  • 10.  How to Check If String Contains Substring in PHP

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy