- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP isset() | Check if a Variable is Set
The PHP isset() function is used when we need to check whether a variable is set or not. For example:
<?php if(isset($x)) { echo "Variable \$x is set"; } else { echo "Variable \$x is not set"; } ?>
Since the variable $x is not set, therefore the output of above example is:
PHP isset() Syntax
The syntax of isset() function in PHP, is:
isset(variableOne, variableTwo, variableThree, ..., variableN);
At least one variable is required. Returns 1 if all given variables exists and are not NULL. Otherwise returns false or nothing. For example:
<?php $x = 10; $y = 20; $z = 30; echo "1: ", isset($x), "<BR>"; echo "2: ", isset($x, $y), "<BR>"; echo "3: ", isset($x, $y, $z), "<BR>"; echo "4: ", isset($a), "<BR>"; echo "5: ", isset($x, $a), "<BR>"; echo "6: ", isset($x, $y, $s), "<BR>"; ?>
The output of this PHP example on isset() function is shown in the snapshot given below:
In above example, the returned value 1 by isset() function, can also be considered as true.
PHP isset() Example
<?php $a = 10; $b = 0; $c = "jobails.com"; $d = "PHP is Fun!"; $e = 12.42; $f = true; $g = null; if(isset($a)) echo "<p>The variable \$a is set, with $a</p>"; if(isset($b)) echo "<p>The variable \$b is set, with $b</p>"; if(isset($c)) echo "<p>The variable \$c is set, with $c</p>"; if(isset($d)) echo "<p>The variable \$d is set, with $d</p>"; if(isset($e)) echo "<p>The variable \$e is set, with $e</p>"; if(isset($f)) echo "<p>The variable \$f is set, with $f</p>"; if(isset($g)) echo "<p>The variable \$g is set, with $g</p>"; if(isset($z)) echo "<p>The variable \$z is set, with $z</p>"; if(isset($myvar)) echo "<p>The variable \$myvar is set, with $myvar</p>"; ?>
The output should be:
The variable $a is set, with 10 The variable $b is set, with 0 The variable $c is set, with jobails.com The variable $d is set, with PHP is Fun! The variable $e is set, with 12.42 The variable $f is set, with 1
You can also print the counterpart, that is, the message when a variable is not set or defined, in this way:
<?php if(isset($myvar)) echo "<p>The variable \$myvar is set, with $myvar</p>"; else echo "<p>The variable \$myvar is not set</p>"; ?>
Or directly using:
<?php if(!isset($myvar)) echo "<p>The variable \$myvar is not set</p>"; ?>
Since, in both the above example, the variable $myvar is not set, therefore the output of these two codes should be:
The variable $myvar is not set
« Previous Tutorial Next Tutorial »