# PHP 5 Operator

## PHP operator

An operator is any symbol which used to perform operation on a value and produce new value. NEW TERM an operator is a symbol that manipulates one or more values, usually producing a new value in the process.

## Example of Operator

```  <?php

\$var1 = 38;
\$var2 = \$var1;//assignment operator
echo "Second Number is: ";
echo \$var2 . "";
\$var2*= \$var1;//Mathematical Operators
echo "Multiplication of two numbers is: ";
echo \$var2 . "";
\$var1++;//incremental operator
echo "Post increment of a number: ";
echo \$var1 . "";

?>
```

## PHP operators

Mathematical Operators

### Description

- Subtract numbers
* Multiplication of numbers
/ Divides the numbers
% Find the remainder

## Mathematical Operators example

```  <?php

\$var1  = 40;
\$var2  = 50;
\$sum = \$var1 +  \$var2 ;
\$diff =   \$var2 -\$var1 ;
echo "Difference: "."\$diff"."" ;
\$mul  =   \$var2  * \$var1 ;
echo "Multiplication: "."\$mul"."" ;
\$div = \$var1 /  \$var2 ;
echo "Division: "."\$div"."" ;
\$rem =   \$var2 % \$var1;
echo "Reminder: "."\$rem"."" ;

?>
```

## PHP operators

Incrementing /Decrementing Operators

### Description

++\$y; Add 1 to \$y and returns result
\$x++; Return \$x and then adds one to it
- - \$x; Subtract one from \$x and then returns the result
\$x --; Return \$x and then subtracts one from it

\$x++ and ++\$x are not same, both are totally different

## Increment / Decrement Operator Example

```  <?php

\$var1 = 50;
\$var2 = \$var1++;
echo "The first number is " . \$var1 . "";
echo "The second number is " . \$var2 . "";
\$num3 = 5;
\$num4 = --\$num3;
echo "The third number is " . \$num3 . "";
echo "The fourth number is " . \$num4;

?>
```

## PHP operators

PHP logical Operators

### Description

&& (and) Return true if both \$x and \$y are true ;otherwise false
|| (or) Return true either \$x or \$y is true ;otherwise false
! (not) true if \$x is false ; false if \$x is true
xor Return true if \$x or \$y (but not both) are true ; false otherwise

Generally logical operator used in decision making
Both are same either "and" or "& &".

## Logical Operator Example

```  <?php

\$var1 = 100;
\$var2 = 2;
\$var3 = 50;

If ((\$var1>=10) && (\$var2 <= 10))
{
echo "This is first condition"."";
echo "And all are false";
}
elseif ((\$var1>=10) or (\$var3 == 100))
{
echo "Second condition true";
}
else
{
echo"all conditions are false";
}

?>
```

## One more example of logical operator

```  <?php

\$var1 = 100;

If (\$var1!==10)
{
echo "First condition is true";
}

else
{
Echo "Its false condition";    }

?>
```

## PHP Comparison operators

PHP Comparison Operators

### Description

== Check two values equal?
=== Check two values equal in both value and data type?
> Check first value larger than the second value?
>= Check the first value larger than or equal to the second value?
< Check the first value smaller than the second value?
<= Check the first value smaller than or equal to the second value?
!=, <> Check the values not equal to each other in value?
!== Check the two values not equal to each other in either value or

## Comparison operator Example

```  <?php

\$var1 = 40;
\$var2 = 50;
\$var3 = '40';

If (\$var1 < \$var2)
{
echo \$var1 . " is less than " . \$var2 . "";
}

If (\$var1 == \$var3)
{
echo \$var1 . " is equal to " . \$var3 . "";
}

If (\$var1 === \$var3)
{
echo \$var1 . " is identical to " . \$var3 . "";
}

If (\$var2 !== \$var3)
{
echo \$var2 . " is not identical to " . \$var3;
}

?>
```

## PHP string operators

PHP string Operators

### Description

. concatenate two strings

## String operator example

```  <?php

\$var1="Hello";
\$var2=" Welcome to php!";
echo \$var1 . \$var2."";
echo "string"." operator";

?>
```

## Operators Precedence

If we use more than operation then first which operator resolve that’s called precedence. Consider the following example:

8 + 3 * 5

First add 8 to 3 to produce 11, then multiply the result by 2 to produce a final figure of 22? Or should it multiply 3 by 5 first to get 15, then add 8 to make 23?

Now operator precedence play important role. All PHP operators are ordered according to precedence. ## Operators Precedence example

```  <?php

\$a = 3 * 3 % 5 - 10 + 25; // 19
echo "Value of first exp:". \$a."";
\$b = 12 - 10 + 45 * 2 ;
echo"Value of first exp:". \$b;

?>
```

QUOTES OF THE DAY

Knowledge itself is a power.