Jump to content

Archived

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

hostfreak

Question about the "Not" Operator

Recommended Posts

I see it used often, e.g:

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

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.