06 - operators

5
  © unettuts, 2011 http://unettuts.com  PHP Operator s An operator is a symbol which operates on operands to perform an action and produces new result. An operand is actual value on which operator operates. For example: 4+5 Here 4 and 5 are operands. + is a operator. The combination of operator and operand to produce result is called expression. PHP has rich set of operators. Some of the common PHP operators are : Assignment operator: Assignment operator (=) assigns value of the right hand operand to the left hand operand. For example: <?php $var = “Sourabh”; ?> Here variable $var is assigned string value “Sourabh”.  Arithmetic operator: As name suggests, Arithmetic operators performs arithmetic operations. For example: <?php $x = 4 + 5; ?> Here 4 and 5 add up to store result in variable $x.

Upload: sourabh-bhandari

Post on 07-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 06 - Operators

8/4/2019 06 - Operators

http://slidepdf.com/reader/full/06-operators 1/5

 

  © unettuts, 2011 http://une 

PHP Operators

An operator is a symbol which operates on operands to perform an action and

produces new result. An operand is actual value on which operator operates.

For example:

4+5

Here 4 and 5 are operands. + is a operator.

The combination of operator and operand to produce result is called expression.

PHP has rich set of operators. Some of the common PHP operators are :

Assignment operator:

Assignment operator (=) assigns value of the right hand operand to the left hand

operand. For example:

<?php

$var = “Sourabh”; 

?>

Here variable $var is assigned string value “Sourabh”.  

Arithmetic operator:

As name suggests, Arithmetic operators performs arithmetic operations. For

example:

<?php

$x = 4 + 5;

?>

Here 4 and 5 add up to store result in variable $x.

Page 2: 06 - Operators

8/4/2019 06 - Operators

http://slidepdf.com/reader/full/06-operators 2/5

 

  © unettuts, 2011 http://une 

List of Arithmetic operators are as under:

Operator Name Example

+ Addition 5 + 4 = 9

- Subtraction 5 – 4 = 9/ Division 5 / 4 = 1.25

* Multiplication 5 * 4 = 20

% Modulus 5 % 4 = 1

Concatenation operator:

Concatenate operator is used to concatenate operands. It appends right hand

operand to the left. It treats both operands as string regardless of data type of 

operand. It is denoted by period (.). For example:

<?php

echo “hello”.” World”; 

?>

It will concatenate “hello” and “ World” to output the string “hello World”.  

Combined assignment operator:

It is a shorthand to perform operation and assign result. It consist of standard

operator symbol followed by equals sign. For example:

$y = 5;

$y = $y + 5; 

is equivalent to

$y = 5;

$y += 5; 

Page 3: 06 - Operators

8/4/2019 06 - Operators

http://slidepdf.com/reader/full/06-operators 3/5

 

  © unettuts, 2011 http://une 

List of Combined assignment operators:

Operator Example

+= $x += 5

-= $x -= 5/= $x /= 5

*= $x *= 5

%= $x %= 5

.= $x .= “hello” 

Increment/Decrement operator:

It increments or decrements the value of integer variable by 1. For example:

$x = $x + 1; 

is equivalent to:

$x++; 

It is of two types:

1.  Pre – increment operator (Pre – decrement operator)

2.  Post – increment operator (Post – decrement operator)

1.  Pre – increment operator (Pre – decrement operator):

++$x; // pre – increment

--$x; // pre – decrement

If these expression are part of test expression then incrementation /

decrementation occurs before test is carried out. For example” 

<?php

$x = 3;

++$x < 4; // false

?> 

2.  Post – increment operator (Post – decrement operator):

$x++; // post – increment

Page 4: 06 - Operators

8/4/2019 06 - Operators

http://slidepdf.com/reader/full/06-operators 4/5

 

  © unettuts, 2011 http://une 

$x--; // post – decrement

If these expression are part of test expression then incrementation /

decrementation occurs after test is carried out. 

Comparision operator: 

It performs test on its operands and returns boolean value. If test is successful

then it returns true else it returns false. For example:

$x < 5; 

Here it will test whether value of x is less the 5 or not. If $x contains 5 then

expression has true value else false.

List of comparison operators

Operator Name Return true if 

== Equivalence Left is equivalent to right

!= Non – equivalence Left is not equivalent to

right

=== Identical Left is not equivalent to

right and they are of 

same type

> Greater than Left is greater than right>= Greater than or equal to Left is greater than or

equal to right

< Less then Left is less than right

<= Less than or equal to Left is less than or equal

to right

Page 5: 06 - Operators

8/4/2019 06 - Operators

http://slidepdf.com/reader/full/06-operators 5/5

 

  © unettuts, 2011 http://une 

Logical operator:

It tests the combinations of Booleans:

List of logical operators

Operator Name Returns true if 

|| OR Left or right is true

Or OR Left or right is true 

xor XOR Left or right is true but

both not

&& AND Left or right are true

and AND Left and right are true

! NOT Single operand is not true

Check our more tutorials at http://unettuts.com