Hudzilla.org - the homepage of Paul Hudson
Contents > Simple variables and operators Wish List | Report Bug | About Me ]

3.2     Checking a variable is set: isset()

This is NOT the latest copy of this book; click here for the latest version.

Although the majority of functions are covered in in the Functions chapter, you really need to get to grips with the isset() function (literally "is a variable set?") in order to make the most of this chapter. To make it work, you pass a variable in as the only parameter to isset(), and it will return true or false depending on whether the variable is set. For example:

<?php
    $foo
= 1;
    if (isset(
$foo)) {
        echo
"Foo is set\n";
    } else {
        echo
"Foo is not set\n";
    }
    if (isset(
$bar)) {
        echo
"Bar is set\n";
    } else {
        echo
"Bar is not set\n";
    }
?>

That will output "Foo is set" and "Bar is not set". Usually if you try to access a variable that isn't set, like $bar above, PHP will issue a warning that you are trying to use an unknown variable. This does not happen with isset(), which makes it a safe function to use.

As I said, flip ahead to the Functions chapter to read more about this function; for now, you just need to know this one.

Note that throughout this book (and throughout the programming world), "nonsense" variable names are used. Names like $foo, $bar, and $baz are the most common, but I often add $wom and $bat. Some argue against the use of these names, but the reality is that their use is so widespread - and that they can help liven up otherwise dull texts - and so I've used them liberally. That said, avoid them in your own code unless its for short-lived variables.





<< 3.1 Types of Data   3.3 Automatic type conversion >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
bmuhmmad@yahoo.com - 13 Oct 2008

hi
Me Bilal from Pakistan i use this web its soo good
and it helps me a lot to understand the php.
thx the author.

Achoo - 13 Oct 2008

Translation of Tom Pester's post of - 05 Feb 2007.

Tom said.
isset() should only be used for testing if the variable exists and not if the variable contains an empty "" string. empty() is designed for that

Translation:
string. empty() is the proper test of an empty string.

However the author was teaching about isset() and all of it's uses in this lesson so mentioning string. empty() is not mentioned.

A PHP User - 13 Oct 2008

isset() i finally understand it after reading through the chapter not knowing what the heck it meant. If you are still confused about isset() then bookmark this page and return after you have read a few more pages :)

M. Kluwe - 13 Oct 2008

For history of $foo, $bar and $wombat, see

http://www.catb.org/~esr/jargon/html/M/metasyntactic-variable.html

Tom Pester - 13 Oct 2008

isset() should only be used for testing if the variable exists and not if the variable containes an empty "" string. empty() is designed for that

Qj - 13 Oct 2008

Translation??

A PHP User - 13 Oct 2008

Hello ppl! Whats goin on!!?L!`?L!`=O#`=^!I#K
'

u rulz



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow