Jump to content


Photo

isset


  • Please log in to reply
4 replies to this topic

#1 mnvdk

mnvdk
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 October 2006 - 01:46 PM

I was just wondering (while wandering)...

I've always used
if (!isset($variable))
to determine whether $variable is set or not.

I could also check this just by using
if (!$variable)
but I've always thought this was less "beautiful", or whatever...

But what is the major difference between the two ways?
I'm fairly new to PHP, so I'm not that into the technological specifics of every function, so if there is some major problem by just using
!$variable, I wouldn't know it.
Sarcasm: It looks like it works, therefore it's good! The IE way!

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 03 October 2006 - 01:48 PM

basically, if you simply use if (!$variable), it will work, but if you have error reporting turned on, you are actually accessing the variable before it is instantiated, and you'll produce warnings. it's much cleaner and more appropriate code to catch that error yourself with isset().
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 03 October 2006 - 01:49 PM

The first (isset) checks to see if the variable actually exists. The second, checks to see if the variables value is true or false.

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 03 October 2006 - 02:26 PM

if you are using this to check values from a form becareful. the variable will be set if it was present in a form - even if the user didn't put any data in there.

I always use isset to check a whole list of vars are present then !empty for those that are required (or !is_null)
follow me on twitter @PHPsycho

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 03 October 2006 - 02:29 PM

if you are using this to check values from a form becareful. the variable will be set if it was present in a form - even if the user didn't put any data in there.

I always use isset to check a whole list of vars are present then !empty for those that are required (or !is_null)


actually, not all variables in a form are set automatically. text fields are, checkboxes and radio buttons are not by default (unless, of course, you have a radio button checked by default).
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users