PHP Operators

What are operators? Things you can execute operations with. PHP has different kinds of operators:

Arithmetic operators

For Mathematical operations. Choose of the following:

  • + Addition
  • – Subtraction: Subtracts a value from a variable
  • * Multiplies
  • / Divides
  • % Modulus: Gives the remaining number when using division
  • ++ Increments
  • – – Decrements

Note: The increment/decrement operators only affect numbers and strings. Arrays, objects and resources are not affected. Decrementing NULL values has no effect too, but incrementing them results in 1.

– by php.net

Assignment Operators

Assigning Values to Variables.

  • = Variable equals number
  • += Adds value to variables (not replaces variable)
  • -= Decrease value from variable
  • *= Multiplies value to variable
  • /= Divides value from variable
  • .= used in strings to concatenate: $j.=$k is equivalent to $j . $k
  • %= Modulus

Comparison Operators

Often used in if statements when comparing 2 items

  • == is equal to // NOTE: Don’t mix up with assignment operator =
  • != is not equal to
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to

Logical Operators

Usually it is used to combine the results of other operators and returns true or false.

  • && and
  • and Low-precedence and
  • || or
  • or Low-precedence or
  • ! not
  • xor Exclusive or
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s