Custom Search

Thursday, February 21, 2013

PHP OPERATORS


PHP Operators

       An operator is a special symbol which indicates a certain process is carried out. Operators in programming languages are taken from mathematics. Programmers work with data. The operators are used to process data.

We have several types of operators:
  • Arithmetic operators
  • Boolean operators
  • Relational operators
  • Bitwise operators
An operator may have one or two operands. An operand is one of the inputs (arguments) of an operator. Those operators that work with only one operand are called unary operators. Those who work with two operands are called binary operators.
+ and - signs can be addition and subtraction operators as well as unary sign operators. It depends on the situation.

php > print +2;
2
php > print -2;
-2
php > print 2;
2
php > print 2+2;
4
php > print 2-2;
0

The plus sign can be used to indicate that we have a positive number. But it is mostly not used. The minus sign changes the sign of a value.

PHP Arithmetic Operators

Operator
Name
Description
Example
Result
x + y
Addition
Sum of x and y
2 + 2
4
x - y
Subtraction
Difference of x and y
5 - 2
3
x * y
Multiplication
Product of x and y
5 * 2
10
x / y
Division
Quotient of x and y
15 / 5
3
x % y
Modulus
Remainder of x divided by y
5 % 2
10 % 8
10 % 2
1
2
0
- x
Negation
Opposite of x
- 2

a . b
Concatenation
Concatenate two strings
"Hi" . "Ha"
HiHa

PHP Assignment Operators

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the expression on the right. That is, the value of "$x = 5" is 5.

Assignment
Same as...
Description
x = y
x = y
The left operand gets set to the value of the expression on the right
x += y
x = x + y
Addition
x -= y
x = x - y
Subtraction
x *= y
x = x * y
Multiplication
x /= y
x = x / y
Division
x %= y
x = x % y
Modulus
a .= b
a = a . b
Concatenate two strings
PHP Incrementing/Decrementing Operators

Operator
Name
Description
++ x
Pre-increment
Increments x by one, then returns x
x ++
Post-increment
Returns x, then increments x by one
-- x
Pre-decrement
Decrements x by one, then returns x
x --
Post-decrement
Returns x, then decrements x by one


PHP Comparison Operators

Comparison operators allows you to compare two values:

Operator
Name
Description
Example
x == y
Equal
True if x is equal to y
5==8 returns false
x === y
Identical
True if x is equal to y, and they are of same type
5==="5" returns false
x != y
Not equal
True if x is not equal to y
5!=8 returns true
x <> y
Not equal
True if x is not equal to y
5<>8 returns true
x !== y
Not identical
True if x is not equal to y, or they are not of same type
5!=="5" returns true
x > y
Greater than
True if x is greater than y
5>8 returns false
x < y
Less than
True if x is less than y
5<8 o:p="" returns="" true="">
x >= y
Greater than or equal to
True if x is greater than or equal to y
5>=8 returns false
x <= y
Less than or equal to
True if x is less than or equal to y
5<=8 returns true
PHP Logical Operators

Operator
Name
Description
Example
x and y
And
True if both x and y are true
x=6
y=3
(x < 10 and y > 1) returns true
x or y
Or
True if either or both x and y are true
x=6
y=3
(x==6 or y==5) returns true
x xor y
Xor
True if either x or y is true, but not both
x=6
y=3
(x==6 xor y==3) returns false
x && y
And
True if both x and y are true
x=6
y=3
(x < 10 && y > 1) returns true
x || y
Or
True if either or both x and y are true
x=6
y=3
(x==5 || y==5) returns false
! x
Not
True if x is not true
x=6
y=3
!(x==y) returns true





PHP Array Operators
Operator
Name
Description
x + y
Union
Union of x and y
x == y
Equality
True if x and y have the same key/value pairs
x === y
Identity
True if x and y have the same key/value pairs in the same order and are of the same type
x != y
Inequality
True if x is not equal to y
x <> y
Inequality
True if x is not equal to y
x !== y
Non-identity
True if x is not identical to y

Operator precedence

The operator precedence tells us which operators are evaluated first. The precedence level is necessary to avoid ambiguity in expressions.

What is the outcome of the following expression? 28 or 40?
 3 + 5 * 5

Like in mathematics, the multiplication operator has a higher precedence than addition operator. So the outcome is 28.

(3 + 5) * 5
To change the order of evaluation, we can use square brackets. Expressions inside square brackets are always evaluated first.

The following list shows common PHP operators ordered by precedence (highest precedence first):
Operator(s)
Description
++ --
increment/decrement
(int) (float) (string) (array) (object) (bool)
casting
!
logical "not"
* / %
arithmetic
+ - .
arithmetic and string
<< >>
bitwise
< <= > >= <>
comparison
== != === !==
comparison
&&
logical "and"
||
logical "or"
? :
ternary operator
= += -= *= /= .= %=
assignment
and
logical "and"
xor
logical "xor"
or
logical "or"
,
comma operator

Operators on the same line in the list have the same precedence.


print 3 + 5 * 5;
print "\n";
print (3 + 5) * 5;
print "\n";

var_dump(! True or True);
var_dump(! (True or True));

?>

In this code example, we show some common expressions. The outcome of each expression is dependent on the precedence level.

var_dump(! True or True);

In this case, the negation operator has a higher precedence. 
First, the first True value is negated to False, than the or operator combines False and True, which gives True in the end.

$ php precedence.php
28
40
bool(true)
bool(false)

The relational operators have a higher precedence than logical operators.


$a = 1;
$b = 2;

if ($a > 0 and $b > 0) {

    echo "\$a and \$b are positive integers\n";
}

?>

The and operator awaits two boolean values.

 If one of the operands would not be a boolean value, we would get a syntax error.
 In PHP, the relational operators are evaluated first. 
The logical operator then.
$ php positive.php
$a and $b are positive integers

Associativity

Sometimes the precedence is not satisfactory to determine the outcome of an expression. 

There is another rule called associativity. 

The associativity of operators determines the order of evaluation of operators with the same precedence level.

9 / 3 * 3

What is the outcome of this expression? 9 or 1? The multiplication, deletion and the modulo operator are left to right associated. 

So the expression is evaluated this way: (9 / 3) * 3 and the result is 9.

Arithmetic, boolean, relational and bitwise operators are all left to right associated.
On the other hand, the assignment operator is right associated.

php > $a = $b = $c = $d = 0;
php > echo $a, $b, $c, $d;
0000

If the association was left to right, the previous expression would not be possible.
The compound assignment operators are right to left associated.

php > $j = 0;
php > $j *= 3 + 1;
php > print $j;
0

You might expect the result to be 1. 

But the actual result is 0.

 Because of the associativity. 

The expression on the right is evaluated first and than the compound assignment operator is applied.

Other operators

PHP has a silence ( @ ) operator. It is used to turn off error messaging. It is typically used with network or database connections. This operator should be used with caution, because it can lead to debugging issues.
php > echo 3 / 0;

Warning: Division by zero in php shell code on line 1

php > echo @ (3 / 0);
php >

In the first case, we receive a division by zero error message. In the second case, the @ operator turns off the error message.

The reference ( & ) operator. It creates a reference to an object.

php > $a = 12;
php > $b = &$a;
php > echo $b;
12
php > $b = 24;
php > echo $b;
24
php > echo $a;
24

In the above example, we pass a value to $a variable and pass a reference to the $a to the $b variable.

php > $b = &$a;

We create a new variable $b pointing to the $a variable. In other words, we create an alias for the $a variable.

php > $b = 24;
php > echo $b;
24
php > echo $a;
24

Assigning a new value to $b will also affect the $a.

The backtick ( ` ) operator

It is used to execute commands. 

It is identical to the shell_exec()function call.

php > $list = `ls -l`;
php > echo $list;
total 48

-rw-r--r-- 1 vronskij vronskij 127 2009-12-07 11:25 andop.php
-rw-r--r-- 1 vronskij vronskij 174 2009-12-07 10:48 arithmetic.php
-rw-r--r-- 1 vronskij vronskij  86 2010-01-16 15:11 atoperator.php
-rw-r--r-- 1 vronskij vronskij 106 2009-12-07 12:25 compare.php

Execute an ls command, which on Unix systems lists the contents of the current directory.

No comments:

Post a Comment