Jump to content

Question about the "Not" Operator


hostfreak

Recommended Posts

If you did

[code]if (isset($variable))[/code]

Then you would be checking if $variable is set.

[code]if (!isset($variable))[/code]

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

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

The second is the exact opposite of the first.
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:
[code]<?php
if (isset($_POST['thesubmitbutton'])) {
//
// process form
//
} else {
//
// display form
//
}?>[/code]
Another example would be storing a session variable, but only if it's not already set.
[code]<?php
session_start();
if (!isset($_SESSION['my_var'])) $_SESSION['my_var'] = 'some value here';
?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.