Home arrow PHP arrow Page 5 - PHP Operators

Better by Comparison - PHP

What can you do without operators? Not much, if you're trying to do without them in a programming language, and PHP is no exception. On Monday, we barely had time to show you a long list of the operators in PHP. Today, we're going to show you what they do.

TABLE OF CONTENTS:
  1. PHP Operators
  2. Assignment Operators
  3. Arithmetic Operators
  4. Incremental Operators
  5. Better by Comparison
  6. Logical Operators
By: James Payne
Rating: starstarstarstarstar / 20
October 31, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

People love to compare things. Is this better than that? Are you fatter than me? Am I just as ugly as you are? Is Picard Greater than or Equal to Kirk? I'll tell you right now, Cisco was greater than Kirk and Picard combined. Deep Space 9 FTW.

Well, in PHP you can compare all day long with the help of Comparison Operators. They allow you to compare two or more values and then have the program respond appropriately. We will cover these more during the upcoming tutorial on Statements, but here is how they look in code:


<html>

<body>


<?php


$my_name = “Mr. T”;


if ( $my_name == “Mr. T”) {

echo “Your name is really Mr. T? I pity you fool!”;

}


?>


</body>

</html>

In the above example, we assigned the value “Mr. T” to the variable $my_name. We then made a program that stated if the value of $my_name was indeed “Mr. T” then print “Your name is really Mr. T? I pity you fool!”. If the value of $my_name had been anything else, the line would not have printed.

Note that we used the == to compare the value of $my_name to our criteria (Mr. T).

In the following code we will use the not equal to operator (!=).


<html>

<body>


<?php


$my_name = “Charlie Brown”;


if ( $my_name != “Mr. T”) {

echo “Your name is not Mr. T? Then quit that jibba-jabba!”;

}


?>


</body>

</html>

The code above checks to see if the value of $my_name is “Mr. T”. Since it isn't, it prints out the line:

 

  You name is not Mr. T? Then quit that jibba-jabba!

If the value had been “Mr. T”, then nothing would have occurred.

If it is still a little confusing, have no fear. It'll be clear as mud after the next few tutorials.



 
 
>>> More PHP Articles          >>> More By James Payne
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: