Tuesday, April 7, 2009

Operators in PHP

Just like any other programing language , PHP comes with its own set of operators. they might look similar to behave in same way as any java or c# operator. But we will look into the operators as they might be some variations. There are different types of operators in PHP : 
  1. Arithematic operators
  2. Assignment operators
  3. comparison operators 
  4. Logical operators 
Arithermatic operators : Throughout the history , the first things we learned we learned were alphabets and numbers. Then we have added ages to the life, subtracted the shortcomings ,and multipliedz our experience in life. All these operations in PHP are carried with a rich set of arithematic tools it has to offer. Below is a table which lists the various PHP operators : 

Operator

Description

Example

Result

+

Addition

$iknowjavaAdd=2
$iknowjavaAdd+20

22

-

Subtraction

$iknowjavaSub=29
$50-
iknowjavaAdd

21

*

Multiplication

$iknowjavaMultiply=54

$iknowjavaMultiply*2

108

/

Division

15/5
5/2

3
2.5

%

Modulus (division remainder)

5%2
10%8
10%2

1
2
0

++

Increment

$iknowjavaInc=59

$iknowjavaMultiply++

x=60

--

Decrement

$iknowjavaDec=90

$iknowjavaDec--

x=89


----------------------------------------------------------------------------------------------------------------------------------------
Assignment operators 
----------------------------------------------------------------------------------------------------------------------------------------
Assignment operators : Many times we have looked into assignement operators. This is a new beginning for many. If i want to put a value of 5 in a variable $iknowmyjava, then I will have to find a way to do it. I will use "=" sign . 
Such type of operations are handled by assignment operators. A few PHP operators are listed below : 

Operator

Example

Is The Same As

=

$x=$y

$x=$y

+=

$x+=$y

$x=$x+$y

-=

$x-=$y

$x=$x-$y

*=

$x*=$y

$x=$x*$y

/=

$x/=$y

$x=$x/$y

%=

$x%=$y

$x=$x%$y


----------------------------------------------------------------------------------------------------------------------------------------
Comparison operators 
----------------------------------------------------------------------------------------------------------------------------------------

Operator

Description

Example

==

is equal to

5==8 returns false

!=

is not equal

15!=18 returns true

> 

is greater than

15>18 returns false

< 

is less than

51<81>

>=

is greater than or equal to

51>=81 returns false

<=

is less than or equal to

51<=81 returns true


----------------------------------------------------------------------------------------------------------------------------------------
Logical operators 
----------------------------------------------------------------------------------------------------------------------------------------

Operator

Description

E$xample

&&

and

$x=6
$y=3

($x <> 1) returns true

||

or

$x=6
$y=3

($x==5 || $y==5) returns false

!

not

$x=6
$y=3

!($x==$y) returns true

No comments:

Post a Comment