In this section we’ll look at the operators
in PHP, that are used to operate on values. There are basically five types: arithmetic,
assignment, comparison, logical, and bitwise operators. First, we have the four basic arithmetic operations,
as well as modulus to get the division remainder. The equal sign itself is an assignment operator
to assign a value to a variable. A common use of the assignment and arithmetic operators
is to operate on a variable and then to save the result back into that same variable. These
operations can be shortened with the combined assignment operators. Another common operation is to increment or
decrement a variable by one. This can be simplified with one of the increment (++) and decrement
(–) operators. The effect on the variable is identical whether we use the post or pre
operator, but a post-increment returns the original value before it increments, while
the pre-operator increments first and then returns the value. Note that expressions are normally evaluated
from left to right. However different operators also have different precedents, For example,
the multiplication sign binds harder then addition and is therefore evaluated first.
To avoid having to learn the precedents of all operators we can instead use parenthesis
to decide what part of the expression will be evaluated first. Next, there’s the comparison operators that
compares two values and returns either true or false. Notice the three equal signs (===)
for comparing both the value and data type. Used together with these we have the logical
operators. And/or (&&/||) can combine several comparison operators. And the not operator
(!) can invert the result. Finally, The bitwise operators work on the
binary representation of integers. For example, the xor operator (^) turn on the bits that
are set on one side but not on both.

PHP Tutorial – 04 – Operators
Tagged on:                                                                                                                                                                 

4 thoughts on “PHP Tutorial – 04 – Operators

  • July 2, 2012 at 10:58 pm
    Permalink

    hi, thanks for the reply. Can you please explain more? i really cant understand the bitwise operator. please make it detailed!

    Reply
  • July 2, 2012 at 11:07 pm
    Permalink

    hi, thanks for the kind reply. does this mean 4 << 1 means shift 1 to the left with 4? and how does it get 8? i really dont understand, hope you can please explain in details.

    Reply
  • July 3, 2012 at 9:17 pm
    Permalink

    Left shift is a bitwise operator, and so it acts on the binary representation of the specified value. 4 << 1 means to shift 4 left 1 step. The binary representation of 4 is 1 0 0, so shifting it left one step gives 1 0 0 0, which is 8 in decimal notation.

    Reply
  • July 3, 2012 at 9:30 pm
    Permalink

    how do i know the binary representations of the number?

    Reply
  • July 3, 2012 at 10:06 pm
    Permalink

    Google for "A tutorial on binary numbers" and you'll find a good tutorial on binary arithmetics on math.grin.edu.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *