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
Subtraction x=6
/ Division 20/5 4
* Multiplication x=4
++ Increment x=5
Decrement x=5
% Modulus (division remainder) 10%8 2

Logical Operators

Operator Description Example
&& and x=5
(x < 10 && y > 1) returns true
|| or x=5
(x==5 || y==5) returns true
! not x=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. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.