The Not Operator ! Used to Check Form Input

The not logical operator ! seems to have a broader interpretation in PHP than in other languages. This makes it possible to use ! to check form input to determine if a value was supplied.

According to documentation !$a is TRUE if $a is not TRUE.

So, what would the condition of $a have to be for it to be not TRUE?

According to this Wiki:

  • is not set
  • 0
  • FALSE
  • an empty string
  • 0.0
  • "0" A string with a value of zero
  • an array with zero elements
  • new StdClass() An object with zero member variables
  • NULL

So, to determine whether the user supplied a field value:

if (!$_POST['user_name'] || !$_POST['password']) {
  $feedback = 'ERROR -- Missing username or password';
  return $feedback;
}

Note that a $_POST value is set to "" when a form is submitted and the field is blank.

Advertisements

About samehramzylabib

See About on https://samehramzylabib.wordpress.com
This entry was posted in PHP Code Snippets, PHP Data Value Types, PHP Form Processing, PHP Language Constructs and tagged , , . Bookmark the permalink.

One Response to The Not Operator ! Used to Check Form Input

  1. Pingback: empty() is similar !$a | Sam's PHP How-To

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s