October 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using PHP Operators

  • December 12, 2001
  • By Elizabeth Fulghum
  • Send Email »
  • More Articles »

Operators are one of the most essential components of PHP; in fact, there is little you can do without them. They are used to assign variables, work with strings, and work with numbers. They are also integral in controlling program flow, as you will later learn.

In this article, we will be exploring some of the most common operators used in PHP, and how to use them.

So what exactly are operators? Well, to briefly define them, an operator is any symbol used to perform an operation on a value. The actual symbol used to perform the operation is known as an operand. In PHP, operators can easily be grouped by function.

The Assignment Operator

This is the most common operator you will run across in the course of programming PHP. As introduced in the last article, the assignment operator allows you to assign a value to a variable. You can assign strings, integers, even the results of functions:

$var = "Hi there";
$var = 1;
$var = myfunction();



The Concatenation Operator

This operator allows you to append one value to another. You can append strings, other variables or even the results of functions:

$var = "test";
$var2 = "This is a ".$var;
print $var2; -- This would output "This is a test"


You can also use what's called the Assignment Concatenation Operator to append a new value to the value an existing variable:

$var = "this is a test";
$var .=" of the emergency broadcast system";
print $var; -- This would output "This is a test of the emergency broadcast system"


This ability can be especially useful, as we will explore a little later on.


Arithmetic Operators

Just like in math, Arithmetic Operators in PHP are used to work with numbers. Addition subtraction, division and multiplication are all supported:

Operand

Name

Sample

Description

+

Addition

$var + $var2

Adds the two values together

-

Subtraction

$var - $var2

Subtracts the second value from the first.

*

Multiplication

$var * $var2

Multiplies the two values by each other.

/

Division

$var/$var2

Divides the first value by the second.

%

Modulus

$var%$var2

Returns the remainder of the first value divided by the second.


The first four will seem very familiar to you. They are the same operators you have been using since elementary math, and they work the same way. The Modulus operator is a little more obscure, but can be invaluable. We will explore how it works in the context of an actual script later on.


Incrementing/Decrementing Operators

PHP supports specific operators for incrementing and decrementing numbers. These are a short hand way of expressing the increase or decrease of a value by one:

Operand

Name

Sample

Description

++

Post-increment

$var++;

Returns $var, then increases it by 1.

++

Pre-increment

++$var;

Increases $var by 1, then returns it.

--

Post-decrement

$var--;

Returns $var, then decreases it by 1.

--

Pre-decrement

--$var;

Decreases $var by 1, the returns it.


Samples:

$x = 1;

$x++; $x would have the value of 2
$x = 4;
print --$x; -- 4 would be outputted, and then $x would be decremented.



Comparison Operators

Frequently in PHP you will find the need to evaluate the relationship between one value and another. Comparison Operators allow you to test things like whether one number value is larger, smaller or equal to another. Unlike expressions with Arithmetic Operators (which return a value), those with Comparison Operators evaluate to either TRUE or FALSE and are frequently used in conditional statements:

Operand

Name

Sample

Description

==

Equal to

$var == $var2

Is true if the first value is equivalent to the second.

!=

Not equal to

$var != $var2

Is true if the first value is not equal to the second.

<

Less than

$var < $var2

Is true if the first value is less than the second.

>

Greater than

$var > $var2

Is true if the first value is greater than the second.

<=

Less than or equal

$var <= $var2

Is true if the first value is less than or equal to the second.

>=

Greater than or equal

$var >= $var2

Is true if the first value is greater than or equal to the second.


The Equal and Not-Equal To operators are also used to evaluate whether non-numerical values are equal to each other. As an example, imagine a situation where you were developing a user login system and needed to see if the password the user entered was the same as what you had stored.

Try this script on your server to see how you can use a conditional statement to test whether or not the entered password is the same as the stored one:


<?php

$storedpassword = "mypassword";
$enteredpassword = "mypassword";

if ($storedpassword == $enteredpassword) {
   print "The entered password was correct!";
}else {
   print "The entered password was wrong!";
}

?>


To express it in pseudo-code: "If the stored password is equal to the entered password, the passwords match and "The entered password was correct!" is printed. Otherwise, the two passwords do not match and "the entered password was wrong!" is printed."

You could also reverse the logic for the same effect:


<?php

$storedpassword = "mypassword";
$enteredpassword = "mypassword";

if ($storedpassword != $enteredpassword) {
   print "The entered password was wrong!";
}else {
   print "The entered password was correct!";
}

?>


As you might have guessed, the match is case sensitive; "mypassword" is not the same as "Mypassword". Try substituting different values for $enteredpassword to see how the script behaves when the two passwords are not the same.

In addition to the comparision operators listed above, PHP4 has added two new ones that not only allow you see if two values are equal, but also check to see if they are of the same type. These are identical to their cousins, except they use an additional equal sign:

Operand

Name

Sample

Description

===

Identical

$var === $var2

Is true if the first value is equivalent to the second and both are the same type.

!==

Not identical to

$var !== $var2

Is true if the first value is not equal to the second or they are not the same type



Logical Operators

So what happens if you want to evaluate whether more than one condition is true or false? Going back to the example of the user login, pretend you wanted to check to make sure the user's username and password matched the stored ones. You could easily do something like this:


<?php

$storedusername = "user";
$enteredusername = "user";

$storedpassword = "mypassword";
$enteredpassword = "mypassword";

if ($storedusername == $enteredusername AND $storedpassword == $enteredpassword) {
   print "The entered username and password were correct!";
} else {
   print "The entered username and password were wrong!";
}

?>


The "AND" between the two separate conditions tells the script that both must be true for the entire statement to be considered true. If both are true, then "The entered username and password were correct!" would be printed.

PHP supports several different Logical Operators:

Operand

Name

Sample

Description

AND

And

$var AND $var2

Is true if both values are true.

OR

Or

$var OR $var2

Is true if either value is true.

XOR

Xor

$var XOR $var2

Is true if either value is true, but not both.

&&

And

$var && $var2

Is true if both values are true.

||

Or

$var || $var2

Is true if either value is true.

!

Not

!$var

Is true if the value is not true (ex. if the variable doesnt exist).

After looking at this chart, you are probably wonder why there are two different operators for AND and OR. Part of the answer lies with Operator Precedence.


Operator Precedence

Take a look at the following:

$x = 9 + 1 * 10

Because we read left to right, you might assume that $x equals 100 (Nine plus one, times 10, equals 100); however, because of operator precedence, you would be wrong. The multiplication operator has higher precedence than the addition operator, so that calculation would be performed first: One times 10, plus nine, equals 19.

The same rules of precedence apply to Operators in PHP. Take a look at the below chart to get an idea of how various operators rank. Operators listed in the same row have equal precedence.

Highest Precedence

* / %

+ - .

< <= > >=

&&

||

And

Xor

Lowest precedence

or


You can also force precedence by using parentheses:

$x = (9 + 1) * 10


Since the first part of the above equation is surrounded, it would be calculated first, and $x would equal 100.


Finally...

In this article, I have touched on some of those most common operators used in PHP. There are others that you will discover as you progress in learning the language, including some that will be introduced later in this series.

In the next article, we will take a look at conditional loops and control structures, leaning heavily on the concepts introduced here. Stay tuned!

 






Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel