isset() and unset() Function in PHP with Examples

In this tutorial, we would love to share with you isset() and unset() functions in PHP

isset and unset Function in PHP

Here are isset() and unset() functions of PHP with its definition, syntax and examples:

PHP isset() Function

isset() function in PHP is used to check whether a variable is set or not, it means that it decides whether a variable is assigned a value and is not null.

Syntax

The basic Syntax of PHP isset() function is:

isset($variable);

Note:- If using isset() function in PHP, isset() function returns true if a variable is set and if it is not set it will be false.

Example – PHP isset Function

Let’s take an example, Here we will simply declare a string variable and assigned it value. and will use the isset function of PHP for checking whether it’s set or not.

<?php
	$issetVal = "";
	If (isset($issetVal)){
	   echo "The variable is set";
	}
	else {
	    echo "The variable is not set";
	}
?>
Source code of isset function In PHP
<!DOCTYPE html>
<html>
<title>How to check a variable set or not in php | PHP Tutorial</title>
<head></head>
<body>
<h4>How to check a variable set or not in php</h4>
<?php
	$issetVal = "";
	if(isset($issetVal)){
	   echo "Result:- The variable is set";
	}
	else {
	    echo "Result:- The variable is not set";
	}
?>
</body>
</html>

Second Example Of PHP isset() function

Let’s take a second example of PHP isset() function. In this example, we will not assign it any value or variable. We have just declared that string variable and see the output:

<?php
	$issetVal;
	if(isset($issetVal)){
	   echo "Result:- The variable is set";
	}
	else {
	    echo "Result:- The variable is not set";
	}
?>
Source code of Second Example – PHP isset function
<!DOCTYPE html>
<html>
<title>How to check a variable set or not in php | PHP Tutorial</title>
<head></head>
<body>
<h4>How to check a variable set or not in php</h4>
<?php
	$issetVal;
	if(isset($issetVal)){
	   echo "Result:- The variable is set";
	}
	else {
	    echo "Result:- The variable is not set";
	}
?>
</body>
</html>

PHP unset() Function

In PHP, the unset() function is used to destroy a defined variable, which means this function unsets only local variables.

Syntax

The basic Syntax of PHP unset() function is:

unset($variable_name);

Note: When you use the PHP unset function, You may specify one or more variable to be destroyed by using the unset in a single call.

Example – PHP unset Function

Let’s take an example, Here we will simply declare a string variable and assigned it value. and will use the unset function of php for destroy it.

<?php
	$str = "This is simple string";
	//Using unset function
	unset ($str);
	//
	If (isset($str)){
	   echo "The variable is set";
	}
	else {
	    echo "The variable is not set";
	}
?>
Source Code of Unset Function In PHP
<!DOCTYPE html>
<html>
<title>Unset Variable In PHP using PHP unset() Function | PHP Tutorial</title>
<head></head>
<body>
<h4>Unset Variable In PHP using PHP unset() Function</h4>
<?php
	$str = "This is simple string";
	//Using unset function
	unset ($str);
	//
	If (isset($str)){
	   echo "The variable is set";
	}
	else {
	    echo "The variable is not set";
	}
?>
</body>
</html>

Important Note of PHP unset() function

  • If you want to destroy a global variable is inside a function, it will only be destroyed in that function. This will maintain the value on the outside.
  • The unset function does not return any value.

Faqs of PHP isset() and unset() function

1. How to use isset() in PHP Form?

You can use the isset() function in PHP form like below:

<?php

if (isset($_POST['submit'])) {
    echo "Hello, you have successfully check a variable using isset() function in PHP";
}
?>
Source Code of check isset() for PHP Form
<?php

if (isset($_POST['submit'])) {
    echo "Hello, you have successfully check a variable using isset() function in PHP";
}
?>
<!DOCTYPE html>
<html>
	<head>
		<title>PHP Form with isset</title>
	</head>
	<body>
		 <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">

			Name : <input type = "text" name = "name"
			placeholder = "Enter Name" />

			<br><br>

			Email : <input type = "text"
			name = "email" placeholder = "Enter Email" />

			<br><br>

			<input type = "submit" name = "submit" value = "Submit">
		</form>
	</body>
</html>

2. Delete or Remove an element from array – PHP unset() function

if you have array and want to remove or delete an element of array. So you can use the php unset() function for removing and deleting an array item.

<?php
// You have array like this
$arr = array('A', 'C', 'X','G', 'E', 'E', 'K', 'S');
// Display the array element
print_r($arr);
// Use PHP unset() function delete or remove array elements
// elements
unset($arr[4]);
// Display the array element
print_r($arr);
?>
Source Code of Delete or Remove an element from array – PHP unset() function
<!DOCTYPE html>
<html>
<title>Delete or Remove an element from array - PHP unset() function | PHP Tutorial</title>
<head></head>
<body>
<h4>Delete or Remove an element from array - PHP unset() function</h4>
<?php
	// You have array like this
	$arr = array('A', 'C', 'X','G', 'E', 'E', 'K', 'S');
	// Display the array element
	print_r($arr);
	// Use PHP unset() function delete or remove array elements
	unset($arr[4]);

    echo "<br>";
	// Display the array element
	print_r($arr);
?>
</body>
</html>

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to isset() and unset() Function in PHP with Examples

  1. nice, great! Thanks 🙂

Leave a Reply

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