Alternative Home ButtonCIT 235 logo
School
  • CIT 230
  • CIT 235
Bio
  • Mike and Brit's Blog
  • Our Family
Current Projects
  • Hoggan's Rock Solid Gear
    Test Site
  • PHP Motors
Portfolio
  • BurgWear
  • Nature's Nook
  • The Auto Spa Plus

You Are Here: Home > School's Main Page > CIT 235 Main Page > WDS Main Page> WDS 8

WDS 8

Variable Comparison Methods

Now we get to the hairy little calculations that PHP does in order for your input into a database to spit out something logical. What I am talking about are comparison operators for variables, values, numbers, and/or strings. Remember variables hold and store values that you assign it, which can be strings, values, function results or numbers. So comparison operators are the little tools that help you compare variables and values against each other or with each other. We have used this very thing over and over again setting up a forms and a contact page.
  For example, in a contact page we want to make sure that we can respond to an inquiry but it would be very difficult for us if the person filling out the form leaves off vital information that we may need, namely their email, message itself, and their name. Whether they forgot the information or purposely inputted invalid values, it is up to us to control the output with PHP.

PHP Motors contact code

If you go the Contact page in PHP Motors you'll find three fields. If you were to solely hit the send button without filling out any of the fields, you would get errors saying - which has been paraphrased for humor sake - "Hold on there, speedy! Fill out the form correctly." Here is the code we used in the first name field to control what is outputted:

if($_POST['txtfName']=="")
{ $errors['fname']='The first name field is empty, please enter your name'; }

You see the two yellow equal signs? Those are comparison operators we used to make sure that if someone left the field blank, which means the empty space between the parenthesis, then an error would return saying "The first name field is empty, please enter your name" In essence what == means is equal to. What we are basically saying here in this code is if the first name field is equal to nothing, then send an error saying you have to fix it.

We used something similar in effect with the email input field. We wanted to make sure that the email they entered was not less than 6 characters. This is what we wrote in order to control the outcome somewhat:

if(strlen($tempEmail) < 6)
{ $errors['emaillength']='The email provided is too short, please enter a valid email address'; }

Now these are just some simple examples of comparison operators and they can do whole lot more than what I have explained here. You can compare integers with strings, for example, and output something different. This is more advanced and I will not get into it for this summary
  W3 Schools and PHP.net both describe various handfuls of comparison operators and what they mean. Here is a table courtesy of PHP.net site that I thought would be helpful.

Comparison Operators
Example Name Result
$a == $b Equal TRUE if $a is equal to $b.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type. (introduced in PHP 4)
$a != $b Not equal TRUE if $a is not equal to $b.
$a <> $b Not equal TRUE if $a is not equal to $b.
$a !== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type. (introduced in PHP 4)
$a < $b Less than TRUE if $a is strictly less than $b.
$a > $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b Greater than or equal to TRUE if $a is greater than or equal to $b.

In summary, comparison operators help us control the output of any form or search that we may build. They are essential for logical results. You will see that on any page that uses PHP.

Back

Last Updated on April 4, 2009 3:57 PM

© 2008, Mike Hoggan, All rights reserved
Contact Me | Terms | Privacy | Colophon | CIT 230 | CIT 235 | Mike and Brit's Blog
Our Family | Hoggan's Rugged Gear Test Site | BurgWear | Nature's Nook | The Auto Spa Plus

Valid CSS! Valid XHTML 1.0 Strict