• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  Reversing an Array in php with array_reverse() Function
  • 2.  Add Months to Date in php
  • 3.  Remove Last Character from String Using php
  • 4.  php preg_match_all – Get All Matches of Pattern in String
  • 5.  php pi – Get Value of pi Using php pi() Function
  • 6.  Remove Specific Character from String Variable in php
  • 7.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 8.  php switch case – How to Use Switch…Case Statements in php
  • 9.  Replace Underscore with Space in php String
  • 10.  Remove String from String in php with str_replace()

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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

x