• 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 / 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.  What is the Difference Between unset() and unlink() in php?
  • 3.  php getimagesize – Get Height, Width and Image File Type
  • 4.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 5.  php array_merge() – Merge One or More Arrays Together by Appending
  • 6.  php array_fill() – Create Array of Length N with Same Value
  • 7.  php is_array() Function – Check if Variable is Array in php
  • 8.  Remove HTML Tags from String in php
  • 9.  php is_numeric – Check if Variable is a Number
  • 10.  php is_string() Function – Check if Variable is String 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 *

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