PHP Tutorial – Operators

In this PHP programming tutorial we will look at PHP operators. An operator is something that can be used to do operations on values to get a new value. We will look at: Arithmetic, Assignment, Logical, Comparison and Concatenation (string) Operators.

Assignment Operators

Assignment operators are used to set a variable equal to a value or set a variable by doing an arithmetic operation on itself and another value.

Operator Examples Large notation
+= x+=y x=x+y
-= x-=y x=x-y
= x=y x=y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

Arithmetic Operators

Operator Description Example Result
+ Addition x=5
x+2
7
- Subtraction x=6
10-x
4
/ Division 20/5 4
* Multiplication x=4
x*5
20
++ Increment x=5
x++
x=6
Decrement x=5
x–
x=4
% Modulus (division remainder) 10%8 2

Logical Operators

Operator Description Example
&& and x=5
y=5
(x < 10 && y > 1) returns true
|| or x=5
y=5
(x==5 || y==5) returns true
! not x=5
y=5
!(x==y) returns false

Comparison Operators

Operator Description Example
== is equal to 1==2 returns false
!= is not equal 1!=2 returns true
<> is not equal 1<>2 returns true
< is less than 1<2 returns true
> is greater than 1>2 returns false
<= is less than or equal to 1<=2 returns true
>= is greater than or equal to 1>=2 returns false

Concatenation Operator or String Operator

The PHP language has only one string operator. If you want to put two string values together you can use the concatenation operator (.) Take a look at the PHP String Variables tutorial for a concatenation (string) operator example.

This entry was posted in PHP Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

Leave a Reply: