Jump to content


Photo

Question about the "Not" Operator


  • Please log in to reply
3 replies to this topic

#1 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 10 September 2006 - 05:55 AM

I see it used often, e.g:

if (!isset($variable) { ...

But, I am not sure of a practical use for it? I have been wondering for awhile now, it is really starting to bug me. Any answers are appreciated, thanks.

#2 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 10 September 2006 - 06:04 AM

If you did

if (isset($variable))

Then you would be checking if $variable is set.

if (!isset($variable))

checks if $variable is NOT set.  Basically, it gives you the opposite result to what you would normally get.  Another example:

if ($var > 5) { print "$var is greater than 5\n";}
if (!($var > 5)) { print "$var is NOT greater than 5\n";}

The second is the exact opposite of the first.

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 10 September 2006 - 06:05 AM

Most of the time I use this when I use the same script to process a form and to display it. In the form my submit button is usually named "thesubmitbutton" or something like that. This comes to my script in the $_POST array and is only defined when then form is submitted, so I can do something like:
<?php
if (isset($_POST['thesubmitbutton'])) {
//
// process form
//
} else {
//
// display form
//
}?>
Another example would be storing a session variable, but only if it's not already set.
<?php
session_start();
if (!isset($_SESSION['my_var'])) $_SESSION['my_var'] = 'some value here';
?>

Ken

#4 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 10 September 2006 - 06:09 AM

Ah, makes perfect sense now. Thanks guys.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users