• 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
  • VBA
  • About
You are here: Home / PHP / Using isset() Function in php to Check if Variable is Defined

Using isset() Function in php to Check if Variable is Defined

March 18, 2022 Leave a Comment

The isset() function in php allows us to check if one or more variables have been declared to and is not null.

$variable = "I'm a variable";

if (isset($variable)) {
    echo "variable has been declared!";
} else {
   echo "variable hasn't been declared!"
}

if (isset($variable, $another_variable)) {
    echo "all variables have been declared!";
} else {
   echo "all variables haven't been declared!"
}

//Output:
variable has been declared!
all variables haven't been declared!

When working with variables in our php programs, it is useful to be able to check if a variable has been declared or not. In php, the isset() method is used for checking if variables have been defined or not.

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

The isset() function in php takes one or more variables and checks to see if the variable is declared and is different than null.

isset() returns true if the variable exists and false if the variable hasn’t been declared or is null.

If multiple variables are provided, isset() returns true only when all variables passed have been declared.

Below are some examples of using isset() in php to check if one or more variables have been declared.

$variable = "I'm a variable";

if (isset($variable)) {
    echo "variable has been declared!";
} else {
   echo "variable hasn't been declared!"
}

if (isset($variable, $another_variable)) {
    echo "all variables have been declared!";
} else {
   echo "all variables haven't been declared!"
}

$another_variable = "Another variable";

if (isset($variable, $another_variable)) {
    echo "all variables have been declared!";
} else {
   echo "all variables haven't been declared!"
}

//Output:
variable has been declared!
all variables haven't been declared!
all variables have been declared!

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 about isset() in php and how to use the isset() function.

Other Articles You'll Also Like:

  • 1.  Add Months to Date in php
  • 2.  php in_array – Check If Value is in php Array
  • 3.  php array_slice() – Get Slice of Elements from Array
  • 4.  Remove First Character from String Using php
  • 5.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 6.  Replace Space with Dash in php String
  • 7.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 8.  php strlen() – Get the Length of String Variable in php
  • 9.  php array_walk() – Modify Elements in Array with Callback Function
  • 10.  php Random Number Generator with rand Function

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy