Jump to content

Recommended Posts

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

 

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.

 

 

 

$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

$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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.