Earning sources

Recent online moneymaking guides.

Unlimited Music Library, No Fees. Raydiyo (spotify Killer)

Monday, 1 July 2013

PHP Tutorial: PHP Operator Types

What is Operator? easy answer will be given mistreatment expression four + five is up to nine. Here four and five ar known as operands and + is termed operator. PHP language supports following sort of operators.

Arithmetic Operators

Comparison Operators

Logical (or Relational) Operators

Assignment Operators

Conditional (or ternary) Operators

Lets have a glance on all operators one by one.

Arithmetic Operators:

There ar following arithmetic operators supported by PHP language:

Assume variable A holds ten and variable B holds twenty then:

Show Examples
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
    $a = 42;
    $b = 20;
    
    $c = $a + $b;
    echo "Addtion Operation Result: $c <br/>";
    $c = $a - $b;
    echo "Substraction Operation Result: $c <br/>";
    $c = $a * $b;
    echo "Multiplication Operation Result: $c <br/>";
    $c = $a / $b;
    echo "Division Operation Result: $c <br/>";
    $c = $a % $b;
    echo "Modulus Operation Result: $c <br/>";
    $c = $a++; 
    echo "Increment Operation Result: $c <br/>";
    $c = $a--; 
    echo "Decrement Operation Result: $c <br/>";
?>
</body>
</html>

This will bring this result:
Addtion Operation Result: 62
Substraction Operation Result: 22
Multiplication Operation Result: 840
Division Operation Result: 2.1
Modulus Operation Result: 2
Increment Operation Result: 42
Decrement Operation Result: 43


OperatorDescriptionExample
+Adds two operandsA + B will give 30
-Subtracts second operand from the firstA - B will give -10
*Multiply both operandsA * B will give 200
/Divide numerator by denumeratorB / A will give 2
%Modulus Operator and remainder of after an integer divisionB % A will give 0
++Increment operator, increases integer value by oneA++ will give 11
--Decrement operator, decreases integer value by oneA-- will give 9

Comparison Operators:

There area unit following comparison operators supported by PHP language

Assume variable A holds ten and variable B holds twenty then:

Show Examples
<html>
<head><title>Comparision Operators</title><head>
<body>
<?php
    $a = 42;
    $b = 20;

    if( $a == $b ){
       echo "TEST1 : a is equal to b<br/>";
    }else{
       echo "TEST1 : a is not equal to b<br/>";
    }

    if( $a > $b ){
       echo "TEST2 : a is greater than  b<br/>";
    }else{
       echo "TEST2 : a is not greater than b<br/>";
    }
    if( $a < $b ){
       echo "TEST3 : a is less than  b<br/>";
    }else{
       echo "TEST3 : a is not less than b<br/>";
    }
    if( $a != $b ){
       echo "TEST4 : a is not equal to b<br/>";
    }else{
       echo "TEST4 : a is equal to b<br/>";
    }
    if( $a >= $b ){
       echo "TEST5 : a is either grater than or equal to b<br/>";
    }else{
       echo "TEST5 : a is nieghter greater than nor equal to b<br/>";
    }
    if( $a <= $b ){
       echo "TEST6 : a is either less than or equal to b<br/>";
    }else{
       echo "TEST6 : a is nieghter less than nor equal to b<br/>";
    }
?>
</body>
</html>

It will show you this result:
TEST1 : a is not equal to b
TEST2 : a is greater than b
TEST3 : a is not less than b
TEST4 : a is not equal to b
TEST5 : a is either grater than or equal to b
TEST6 : a is nieghter less than nor equal to b

OperatorDescriptionExample
==Checks if the value of two operands are equal or not, if yes then condition becomes true.(A == B) is not true.
!=Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.(A != B) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(A <= B

Logical Operators:

There area unit following logical operators supported by PHP language

Assume variable A holds ten and variable B holds twenty then:

Show Examples
<html>
<head><title>Logical Operators</title><head>
<body>
<?php
    $a = 42;
    $b = 0;
    
    if( $a && $b ){
       echo "TEST1 : Both a and b are true<br/>";
    }else{
       echo "TEST1 : Either a or b is false<br/>";
    }
    if( $a and $b ){
       echo "TEST2 : Both a and b are true<br/>";
    }else{
       echo "TEST2 : Either a or b is false<br/>";
    }
    if( $a || $b ){
       echo "TEST3 : Either a or b is true<br/>";
    }else{
       echo "TEST3 : Both a and b are false<br/>";
    }
    if( $a or $b ){
       echo "TEST4 : Either a or b is true<br/>";
    }else{
       echo "TEST4 : Both a and b are false<br/>";
    }
    $a = 10;
    $b = 20;
    if( $a ){
       echo "TEST5 : a is true <br/>";
    }else{
       echo "TEST5 : a  is false<br/>";
    }
    if( $b ){
       echo "TEST6 : b is true <br/>";
    }else{
       echo "TEST6 : b  is false<br/>";
    }
    if( !$a ){
       echo "TEST7 : a is true <br/>";
    }else{
       echo "TEST7 : a  is false<br/>";
    }
    if( !$b ){
       echo "TEST8 : b is true <br/>";
    }else{
       echo "TEST8 : b  is false<br/>";
    }
?>
</body>
</html>

It will show you this result:
TEST1 : Either a or b is false
TEST2 : Either a or b is false
TEST3 : Either a or b is true
TEST4 : Either a or b is true
TEST5 : a is true
TEST6 : b is true
TEST7 : a is false
TEST8 : b is false

OperatorDescriptionExample
andCalled Logical AND operator. If both the operands are true then then condition becomes true.(A and B) is true.
orCalled Logical OR Operator. If any of the two operands are non zero then then condition becomes true.(A or B) is true.
&&Called Logical AND operator. If both the operands are non zero then then condition becomes true.(A && B) is true.
||Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true.(A || B) is true.
!Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.!(A && B) i
Assignment Operators:

There area unit following assignment operators supported by PHP language:

Show Examples
<html>
<head><title>Assignment Operators</title><head>
<body>
<?php
    $a = 42;
    $b = 20;
    
    $c = $a + $b;   /* Assignment operator */
    echo "Addtion Operation Result: $c <br/>";
    $c += $a;  /* c value was 42 + 20 = 62 */
    echo "Add AND Assigment Operation Result: $c <br/>";
    $c -= $a; /* c value was 42 + 20 + 42 = 104 */
    echo "Subtract AND Assignment Operation Result: $c <br/>";
    $c *= $a; /* c value was 104 - 42 = 62 */
    echo "Multiply AND Assignment Operation Result: $c <br/>";
    $c /= $a;  /* c value was 62 * 42 = 2604 */
    echo "Division AND Assignment Operation Result: $c <br/>";
    $c %= $a; /* c value was 2604/42 = 62*/
    echo "Modulus AND Assignment Operation Result: $c <br/>";
?>
</body>
</html>

It will show you this result:
Addtion Operation Result: 62
Add AND Assigment Operation Result: 104
Subtract AND Assignment Operation Result: 62
Multiply AND Assignment Operation Result: 2604
Division AND Assignment Operation Result: 62
Modulus AND Assignment Operation Result: 20 

OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operandC = A + B will assigne value of A + B into C
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operandC += A is equivalent to C = C + A
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandC -= A is equivalent to C = C - A
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operandC *= A is equivalent to C = C * A
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operandC /= A is equivalent to C = C / A
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operandC %= A

Conditional Operator

There is an added operator known as conditional operator. This initial evaluates Associate in Nursing expression for a real or false worth so execute one amongst the 2 given statements relying upon the results of the analysis. The conditional operator has this syntax:

Show Examples
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
    $a = 10;
    $b = 20;
    
    /* If condition is true then assign a to result otheriwse b */
    $result = ($a > $b ) ? $a :$b;
    echo "TEST1 : Value of result is $result<br/>";
    /* If condition is true then assign a to result otheriwse b */
    $result = ($a < $b ) ? $a :$b;
    echo "TEST2 : Value of result is $result<br/>";
?>
</body>
</html>

It will show you this result:
TEST1 : Value of result is 20
TEST2 : Value of result is 10

OperatorDescriptionExample
? :Conditional ExpressionIf Condition is true ? Then value X : Otherwise value Y
Operators Categories:

All the operators we've got mentioned higher than will be categorized into following categories:

Unary prefix operators, that precede one quantity.

Binary operators, that take 2 operands and perform a range of arithmetic and logical operations.

The conditional operator (a ternary operator), that takes 3 operands and evaluates either the second or third expression, counting on the analysis of the primary expression.

Assignment operators, that assign a worth to a variable.

Precedence of PHP Operators:

Operator precedence determines the grouping of terms in associate degree expression. This affects however associate degree expression is evaluated. bound operators have higher precedence than others; for instance, the multiplication operator has higher precedence than the addition operator:

For example x = seven + three * 2; Here x is allotted thirteen, not twenty as a result of operator * has higher precedence than + therefore it 1st get increased with 3*2 and so adds into seven.

Here operators with the very best precedence seem at the highest of the table, those with all-time low seem at very cheap. inside associate degree expression, higher precedence operators are going to be evaluated 1st.
Category Operator Associativity 
Unary ! ++ -- Right to left 
Multiplicative  * / % Left to right 
Additive  + - Left to right 
Relational  < <= > >= Left to right 
Equality  == != Left to right 
Logical AND && Left to right 
Logical OR || Left to right 
Conditional ?: Right to left 
Assignment = += -= *= /= %=Right to left 

No comments:

Post a Comment

Adbox