1. Comments in PHP

    We can write comments in php script like the following

    <?php

    // This type comment is used to write a single line comment

    echo “Hello php”;

    /*

    This is

    a PHP comment

    block

    */

    ?>

    Operators in php

    Php applications use operators to process the data that entered by a user or fetch from the database. Operators like + and – are used to process variables and return a value. It is used for computations or comparisons and it may contain one or more characters. Operators can transform one or more data values called operands, into a new data value.

    Consider the following example:

    $a+$b

    The preceding example uses two operands and an operator to add the values stored in the variables.

    Following operators used in php

    1.      Arithmetic operators

    2.      Assignment operators

    3.      Unary operators

    4.      Comparison operators

    5.      Logical operators

    6.      Array operators

     

    Arithmetic operators

    These operators are the symbols that are used to perform arithmetic operations on the php variables. The following table describes the commonly used arithmetic operators.

    Operators

    Description

    Example

    +

    Used to add to numbers

    $a = $b+$c;

    If  $b is equal to 10 and $c is equal to 5,Then $a will be 15

    -

    Used to subtract two numbers

    $a = $b-$c;

    If  $b is equal to 10 and $c is equal to 3,Then $a will be 12

    *

    Used to multiple two numbers

    $a = $b*$c;

    If  $b is equal to 8 and $c is equal to 5,Then $a will be 40

    /

    Used to divide one number by another

    $a = $b/$c;

    If  $b is equal to 8 and $c is equal to 2,Then $a will be 4

    %

    Used to divide two numbers and return the reminder

    $a = $b%$c;

    If  $b is equal to 21 and $c is equal to 2,Then $a will be 1

     

    Assignment operators

    These operators are used to perform arithmetic operations to assign a value to an operand. Left operand gets set to the value of the expression on the right.

    Example

    Usage

    Description

    =

    $a = 10;

    Stores the value 10 in the variable $a

     

    +=

    $a += $b;

    Same as

    $a = $a +$b;

    -=

    $a -= $b;

    Same as

    $a = $a - $b;

    *=

    $a *= $b;

    Same as

    $a = $a * $b;

    /=

    $a /= $b;

    Same as

    $a = $a / $b;

    %=

    $a %= $b;

    Same as

    $a = $a % $b;

    Unary operators

    These operators are used to increment or decrement the value of an operand by1. The following table explain the usage of the increment and decrement operators.

    operator

    Description

    Example

    ++

    Used to increment the value of an operand by 1

    $a = $b++;

     

    --

    Used to decrement the value of an operand by 1

    $a = $b--;

     

     

    Comparison operators

    These operators are used to compare two values and execute a code block or an action on the basis of the result of that comparison. The result of the comparison operator will be ‘true’ or ‘false’

    Operator

    Usage

    Description

    Example

    <

    expression1 < expression2

    It is used to check whether expression1 is less than expression2

    $x < $y

     

    True if $x is less than $y

    >

    expression1 > expression2

    It is used to check whether expression1 is greater expression2

    $x > $y

     

    True if $x is greater than $y

    <=

    expression1 <= expression2

    It is used to check whether expression1 is less than or equal to expression2

    $x <= $y

     

    True if $x is less than or equal to $y

    >=

    expression1 >= expression2

    It is used to check whether expression1 is greater than or equal to expression2

    $x >= $y

     

    True if $x is greater than or equal to $y

    ==

    expression1 == expression2

    It is used to check whether expression1 is equal to expression2

    $x == $y

     

    True if $x is equal to $y

    !=

    expression1 != expression2

    It is used to check whether expression1 is not equal to expression2

    $x != $y

     

    True if $x is not equal to $y

    ===

    expression1 === expression2

    It is used to check whether expression1 is identical to expression2

    $x === $y

    True if $x is equal to $y, and they are of the same type

     

    Logical operators

    These operators are used to evaluate expressions and return a boolean value.

    Operator

    Usage

    Description

    Example

    &&

    expression1 && expression2

    It returns true, if both expression1 and expression2 are true.

    $x && $y

     

    True if both $x and $y are true

    !

    ! expression

    It returns true, if expression is false.

    !$x

     

    True if $x is not true

    ||

    expression1 || expression2

    It returns true, if either expression1 or expression2 or both of them are true.

    $x || $y

     

    True if either $x or $y is true