Operators - LEARNING PHP AND MYSQL (2015)

LEARNING PHP AND MYSQL (2015)

Operators

Arithmetic operators

Example

Name

Result

-$a

Negation

Opposite of $a

$a + $b

Addition

Sum of $a and $b

$a - $b

Subtraction

Difference of $a and $b

$a*$b

Multiplication

Product of $a and $b

$a / $b

Division

Quotient of $a and $b

$a % $b

Modulus

Remainder of $a and $b

The division operator would return float value unless the two operands are integers.

Example

<? php

Echo (5 % 3).”\n”;

Echo (5 % -3).”\n”;

Echo (-5 %3).”\n”;

Echo (-5%-3).”\n”;

?>

Assignment operator

There are two types of assignment operators which are commonly used in PHP. These are represented as “=” and “=>”.

Example

<? php

$a= ($b = 4) + 5;

?>

Bitwise operator

It allows evaluation and manipulation of a specific bit.

Example

Name

Result

$a & $b

And

Are set in both $a and $b

$a | $b

Or

Are set in either $a or $b

$a ^ $b

Xor

Not in both

~ $a

Not

Are set in $a are not set

$a≪ $b

Shift left

Shifts bits of $a $b to left

$a≫$b

Shift right

Shift bits of $a $b to right

Comparison operator

It allows the comparison to be done by the use.

Example

Name

Result

$a==$b

Equal

True if $a=$b

$a===$b

identical

True if $a=$b

$a!=$b

Not equal

True if $a≠$b

$a<>$b

Not equal

True if $a≠$b

$a!==$b

Not identical

True if $a≠$b

$a<$b

Less than

True if $a strictly less than $b

$a>$b

Greater than

True if $a greater than $b

Error control operator

It is represented by ‘@ ‘. Using this it will ignore any error that is been intended by the expression.

Example

<? php

$my_file =@file (‘non_existent_file’)

$value = @$cache [$key];

?>

Execution operator

Supports execution operator: back ticks (“). Use of back tick is identical to the shell_exec ().

Example

<? php

$output = “ls – al”;

echo “<pre> $output</pre>;

?>

Increment decrement operator

They are pre and post increment decrement operator in c style.

Example

Name

Effect

++$a

Pre-increment

Increases by one

$a++

Post-increment

Returns ,then increases

--$a

Pre-decrement

Decreases then returns

$a--

Post-decrement

Returns then decreases

Logical operator

The use of two different variables is to get different precedence as result.

Example

Name

Result

$a and $b

And

True if both are true

$a or $b

Or

True if either is true

$a xor $b

Xor

True if either is true

!$a

Not

True if $a is not true

$a&&$b

And

True if both are true

$a || $b

Or

True if either is true