Login | Contact Us | Site Map | Resources

PHP Basics

Home > PHP Tutorial > PHP Operators

PHP Operators

Operators perform operations. They are symbols, or combinations of symbols, that assign value, combine values or compare values, to name just a few of their uses. Without operators, PHP is pretty much useless. In fact, you have already been introduced to several operators without even realizing it!

PHP operators can be broken down into several categories, which we will now review, beginning with the operators that we have already learned.

Assignment Operators

Very simply, assignment operators assign value. We have already used the assignment operator to assign values to variables.

Operator Description
= Assigns A Value

<?php
  $variable = "abcde";
  echo $variable;   // Result: abcde
?>

String Operators

String operators, as we have learned, can either link two strings together, or add one string to the end of another string.

Operator Description
. Concatenates (Links) Two Strings Together
.= Adds A String Onto the End of An Existing String

<?php
  $variable = "abcde" . "fghij";
  $variable .= "klmno";
  echo $variable;   // Result: abcdefghijklmno
?>

Arithmetic Operators

Math! Some people like it and some people don't. Why not have fun writing a PHP script that does your math for you?

Operator Description
+ Addition Operator (Adds Two Values Together)
- Subtraction Operator (Subtracts One Value From Another)
* Multiplication Operator (Multiplies Two Values)
/ Division Operator (Divides One Values From Another)
% Modulus Operator (Determines The Remainder of a Division)

Arithmetic operations can be performed on variables, within variables, echoed directly, etc.

<?php
  $addition = 5 + 5;
  echo $addition;   // Result: 10

  $subtraction = 5 - 5;
  echo $subtraction;   // Result: 0

  $multiplication = 5 * 5;
  echo $multiplication;   // Result: 25

  $division = 5 / 5;
  echo $division;   // Result: 1

  $modulus = 7 % 5;
  echo $modulus;   // Result: 2

  echo 3 + 5 / 2;   // Result: 5.5
?>

What?! It made sense until the last example, right? If you were expecting the results of the last example to be "4", then you might not have known about operator precedence, which states that multiplication and division come before addition and subtraction. To solve this problem you can use parenthesis to group the numbers that you want to be calculated first.

This rule is also known as PEMDAS (Parentheses, Exponents, Multiplication, Division, Addition, Subtraction), BEDMAS (Brackets, Exponents, Division, Multiplication, Addition, Subtraction), BIDMAS (Brackets, Indices, Division, Multiplication, Addition, Subtraction) or BODMAS (Brackets, Orders, Division, Multiplication, Addition, Subtraction).

<?php
  echo (3 + 5) / 2;   // Result: 4
?>

Problem solved; happy calculating!

Combined Assignment Operators

Combined assignment operators are shortcuts useful when performing arithmetic operations on an existing variable's numerical value. They are similar to the concatenation assignment operator.

Operator Description
+= Adds Value to An Existing Variable Value
-= Subtracts Value From Existing Variable Value
*= Multiplies An Existing Variable Value
/= Divides An Existing Variable Value
%= Modulo of An Existing Variable Value And New Value

Example before combined assignment operator is used:

<?php
  $variable = 8;
  $variable = $variable + 3;
  echo $variable;   // Result: 11
?>

Example of combined assignment operator in use:

<?php
  $variable = 8;
  $variable += 3;
  echo $variable;   // Result: 11
?>

Incrementing/Decrementing Operators

There are shortcuts for adding (incrementing) and subtracting (decrementing) a numerical value by one.

Operator Description
++ Incrementation Operator (Increases A Value By 1)
-- Decrementation Operator (Decreases A Value By 1)

<?php
  $variable = 15;
  $variable++;
  echo $variable;   // Result: 16
  $variable--;
  echo $variable;   // Result: 15
?>

Comparison Operators

Comparison operators compare two values and return either true or false, depending on the results of the comparison. They are used in conditions, which we will study in the next chapter.

Operator Description Example
== Is Equal To 3==2 (Will Return "False")
!= Is Not Equal To 3!=2 (Will Return "True")
<> Is Not Equal To 3<>2 (Will Return "True")
> Is Greater Than 3>2 (Will Return "True")
< Is Less Than 3<2 (Will Return "False")
>= Is Greater Than or Equal To 3>=2 (Will Return "True")
<= Is Less Than or Equal To 3<=2 (Will Return "False")

Logical Operators

Logical operators usually work with comparison operators to determine if more than one condition is true or false at the same time.

Operator Meaning Description
&& And True If Two Statements Are True, Otherwise False
|| Or True If One Statement or Another Is True, Otherwise False
! Not True If Statement Is False, False If Statement Is True

Logical operators make more sense when they are applied to practical examples, so let's keep moving. Next up we'll learn about conditions!