PHP Operators Tutorial

By Tejal Patwardhan


There are many types of operators in PHP. These include arithmetic operators, assignment operators, incrementing operators, decrementing operators, comparison operators, logical operators, and array operators. We will be manipulating the distance formula as our example.

Arithmetic Operators

Most of these are quite simple. They are especially useful in if statements but are also helpful in calculations. Just a refresher, $x means variable x.

  • To add two values, just use a '+' sign ($a + $b).
  • For subtraction, use a '-" sign ($a - $b).
  • To multiply, use a '*' sign ($a * $b).
  • To divide, use a '/' sign ($a / $b).
  • To find the remainder after a division, use a '%' sign ($a % $b).This finds the remainder after dividing a by b.
  • To make a number negative, use the '-' sign (- $a).
  • To attach 2 strings together, use a concatenation with a "." sign ("a"."b"). This would turn "foot"."ball" into "football".

    Lets try this out for ourselves. Open up a new page in textwrangler and save it as "phparithmeticoperators.php" in your public_html folder. Then type the following starter code into your document.

    Using your new arithmetic operators knowledge, create the distance formula in php and type it next to the line that says '$d='.

    Use ( a , A ) as your initial point and ( b , B )as your final point.

    The distance formula is the square root of (a-b)^2 + (A-B)^2. Change this to a formula acceptable in php using the variables provided.

    Remember that in php, 'sqrt' is used find the square root of an expression.

    To square a value, use either pow(base,2) or simply use the multiplication operator to multiply the expression by itself. In this case, since we are trying to learn arithmetic operators, use the multiplication operator.

    After running the page, the answer ('15.811388300842') should be displayed on your screen.

    If you are getting a different answer or an error message, make sure you have a semicolon after each line. Then compare to my code.

    Feel free to mess around with the numbers!

    Assignment Operators

    We use assignment operators when we want to set one thing as something else, or give a variable a value. This is similar to how in math we say "x=5" to set the value of x at 5. These are also fairly straightforward.

  • To set $x to 5, simply type $x = 5. We used this operator fairly often in the last example..
  • To set $x to $x plus $y, use $x + = $y.
  • To set $x to $x minus $y, use $x - = $y.
  • To set $x to $x times $y, use $x * = $y.
  • To set $x to $x divided by $y, use $x / = $y.
  • To set $x to the remainder when $x is divided by $y, use $x % = $y.
  • To set "x" to the concatenation of "x" and "y", use "x" . = "y".
  • Incrementing and Decrementing Operators

    We use incrementing and decrementing operators to continuously add or subtract 1 from a variable. They are most commonly used in for loops.

  • Pre-Increments add 1 each time and then return the value. This can be written as ++x.
  • Post-Increments return the value and then add 1 each time. This can be written as x++.
  • Pre-Decrements subtract 1 each time and then return the value. This can be written as --x.
  • Post-Decrements return the value and then subtract 1 each time. This can be written as x--.
  • Open up phparithmeticoperators.php and alter the code as shown below.

    Now delete the '????' and replace it with any incrementing or decrementing operator you want. Then run the page. You can also mess around with the other points and see how the distance changes as the for loop adds or subtracts 1 from a each time.

    If you are having trouble, here is what the final code could look like.

    Comparison Operators

    These are used to compare 2 values to each other. We used these in the for loop from the last example. Comparison operators are frequently used in for loops.

  • The operator a==b becomes true if a and b are equal.
  • The operator a!=b becomes true if a and b are not equal. The operator a<>b also becomes true if a and b are not equal.
  • The operator use a===b becomes true if a and b are "identical" (if a and b are equal and they are the same type).
  • The operator use a!==b becomes true if a and b are not identical.
  • The operator a > b becomes true if a is greater than b.
  • The operator a < b becomes true if a is greater than b.
  • The operator a >= b becomes true if a is greater than or equal to b.
  • The operator a <= b becomes true if a is less than or equal to b.
  • Logical Operators

    These are the ands, ors, and xors. They are most commonly used in if statements.

  • The operator 'a and b' becomes true if both a and b are true.
  • The operator 'a or b' becomes true if either a or b or both are true. The operator 'a || b' also becomes true if either a or b or both are true.
  • The operator 'a xor b' becomes true if either a or b are true, but does not become true if both a and b are true.
  • The operator '!a' (not a) becomes true if a is not true.
  • Open up phparithmeticoperators.php and see if you can figure out a way to display $d only if it is between 16 and 30. Use a logical operator to do this. HINT: You need an if statement, which is in the format "if (condition) { }"

    Having trouble? Take a look at my code.

    Array Operators

    Use in arrays

  • The union operator is 'a + b' .
  • The equality operator 'a == b' becomes true if a and b have the same value.
  • The inequality operator a!=b becomes true if a and b are not equal. The operator a<>b also becomes true if a and b are not equal.
  • The identity operator 'a === b' becomes true if a and b have the same value, same order and same type.
  • The operator use a!==b becomes true if a and b are not identical.
  • Conclusion

    You are now finished with the tutorial! Bonus points if you noticed something special on each of the images of code. Watermark, anyone? #obnoxious

    Here is the finished product. Look at all the operators we used. These operators sure are operational! /p>