$Three3 Posted November 27, 2009 Share Posted November 27, 2009 Hi, I am having a little trouble understanding this piece of code. I have only started reading about PHP a week ago and I am reading a book right now and I have been stuck for a day now on this. I have seen it in a bunch of examples but I am just confused on the ! Operator. Here is the code from the book. <?php // if form not yet submitted // display form if (!isset($_POST['submit'])) { ?> <form method="post" action="primes.php"> Enter a list of numbers, separated by commas: <br /> <input type="text" name="num" /> <p> <input type="submit" name="submit" value="Submit" /> </form> I am having trouble with this line: if (!isset($_POST['submit'])) { Any help is greatly appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/183085-need-help-with-php-logical-operator-basic/ Share on other sites More sharing options...
premiso Posted November 27, 2009 Share Posted November 27, 2009 The ! signifies a "not" So if $_POST['submit'] is not set, then execute the code in the brackets. Or if something like this: if ($x != 1) { Will only execute if $x does not equal 1, hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/183085-need-help-with-php-logical-operator-basic/#findComment-966260 Share on other sites More sharing options...
$Three3 Posted December 1, 2009 Author Share Posted December 1, 2009 The ! signifies a "not" So if $_POST['submit'] is not set, then execute the code in the brackets. Or if something like this: if ($x != 1) { Will only execute if $x does not equal 1, hope that helps. Hey man thanks for the reply. That clarified it up quite a bit. That helped me get through that chapter finally lol. 2 chapters later though there is another example with the not ! operator. It was used in a different way this time. Here is the code: <?php if (isset($_POST['submitted'])) { $probelm = FALSE ; // No problems so far //start checking to make sure forms were filled out correclty if (empty($_POST['fname'])) { $problem = TRUE ; die('Please enter your first name.') ; } if (empty($_POST['lname'])) { $problem = TRUE ; die('Please enter your last name.') ; } if ((empty($_POST['email'])) { $problem = TRUE ; die('Please enter your email address.') ; } if (empty($_POST['password1'])) { $problem = TRUE ; die('Please enter a password.') ; } if ($_POST['password1'] != $_POST['password2']) { $problem = TRUE ; die('Your passwords do not match, please go back and correct the issue.') ; } if (!$problem) { echo "You have successfully registered for an account in North Shore. Your user information <br /> has been emailed to you at " . $_POST['email'] ; This line here if (!$problem) is the one giving me a hard time. Does this mean if there are no problems? Thanks again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/183085-need-help-with-php-logical-operator-basic/#findComment-968700 Share on other sites More sharing options...
mikesta707 Posted December 1, 2009 Share Posted December 1, 2009 $problem is a boolean value. When there is a problem, it is set to true. If not, then it will be false. Applying the not operator (!) to a boolean value basically reverses it (so true becomes false, false becomes true). What that would be checking is if $problem is false (IE there were no problems) The not operator turns it into true. so Does this mean if there are no problems?. yes Quote Link to comment https://forums.phpfreaks.com/topic/183085-need-help-with-php-logical-operator-basic/#findComment-968704 Share on other sites More sharing options...
$Three3 Posted December 1, 2009 Author Share Posted December 1, 2009 $problem is a boolean value. When there is a problem, it is set to true. If not, then it will be false. Applying the not operator (!) to a boolean value basically reverses it (so true becomes false, false becomes true). What that would be checking is if $problem is false (IE there were no problems) The not operator turns it into true. so Does this mean if there are no problems?. yes So the code: if (!$problem) turns the $problem to TRUE? Or does it check to see that $problem is NOT true and if it is not true echo the statement? Thanks for the helpful reply. I'm close to understanding this lol. I'm not sure why this is giving such a hard rme to grasp because it seems like it would b simple to understand. Thanks for the patience. Quote Link to comment https://forums.phpfreaks.com/topic/183085-need-help-with-php-logical-operator-basic/#findComment-968709 Share on other sites More sharing options...
mikesta707 Posted December 1, 2009 Share Posted December 1, 2009 Both actually. It does check if its not true, but in programming, the way you check if something is not true, is to add the not operator to the value. You want to pass a true value into the if statement if you want it to run, and the not operator will take a false value (if something is not true) and turn it into a true value Quote Link to comment https://forums.phpfreaks.com/topic/183085-need-help-with-php-logical-operator-basic/#findComment-968721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.