Learn Php Operators Explain With Examples in 2023

Php Operators in Web Technology

Php Operators: Php operators can be used to perform operations on variables and values. There are several types of operators.
Learn Php Operators Explain With Examples in 2023
Learn Php Operators Explain With Examples in 2023

    What are the types of operators in PHP

    1. Arithmetic operators: These operators perform basic arithmetic operations, such as addition, subtraction, multiplication, and division.

    2. Relational operators: These operators compare two values and return a Boolean value (true or false) based on the relationship between the values.

    3. Logical operators: These operators perform logical operations, such as AND, OR, and NOT.

    4. Bitwise operators: These operators perform operations on individual bits of a binary number.

    5. Assignment operators: These operators assign a value to a variable.

    6. Increment and decrement operators: These operators increase or decrease the value of a variable by 1.

    7. Conditional operators: The conditional operator, also known as the ternary operator, is an operator in C that allows you to assign a value to a variable based on a certain condition.

    8. Special operators: Special operator is an address store(&) and sizeof() value store in special operator. 

    Arithmetic Operators in Php

    Php language, the following arithmetic operators are available:
    1. Addition operator.
    2. Subtraction operator.
    3. Multiplication operator.
    4. Division operator.
    5. Modulus operator.

    Arithmetic Operator Table in Php Programming
    SymbolsOperationsExamples
    +Additionx+y
    -Subtractionx-y
    *Multiplicationx*y
    /Divisionx/y
    %Modulusx%y

    Arithmetic Operators Program in Php

    <?php

    $x=13;

    $y=5;

    echo"Addition:".($x+$y)."br";

    echo"Subtraction:".($x-$y)."br";

    echo"Multiplication:".($x*$y)."br";

    echo"Division:".($x/$y)."br";

    echo"Modulus:".($x%$y)."br";

    ?>


    **********OUTPUT**********

    Addition:18

    Subtraction:8

    Multiplication:65

    Division:2.6

    Modulus:3


    Explain Relational Operators in Php

    Relational Operator: A computer programming, relational operators are operators that are used to compare values and determine the relationship between those values. There are several relational operators in PHP programming languages.
    1. Equal to.
    2. Not equal to.
    3. Greater than.
    4. Less than.
    5. Greater than or equal to.
    6. Less than or equal to.

    Relational Operator Table in php Programming
    SymbolsOperationsExamples
    =Equalx=y
    !=Not Equal Tox!=y
    >Greater Thanx>y
    <Less Thanx<y
    >=Greater Than or Equal Tox>=y
    <=Less Than or Equal Tox<=y

    What is Relational Operator with Example

    <?php

    $f=5;

    $s=3;

    echo"Equal To:".($f==$s)."br";

    echo"Greater Than:".($f>$s)."br";

    echo"Less Than:".($f<$s)."br";

    echo"Greater Than or Equal To:".($f>=$s)."br";

    echo"Less Than or Equal To:".($f<=$s)."br";

    echo"Not Equal To:".($f!=$s)."br";

    ?>


    **********OUTPUT**********

    Equal To:0

    Greater Than:1

    Less Than:0

    Greater Than or Equal To:1

    Less Than or Equal To:0

    Not Equal To:1


    Logical Operators in Php Programming Language

    Logical operators: A PHP programming language, there are several logical operators that you can use to perform logical operations on expressions.

    1. AND (&&) operator: This operator returns true if both the operands are true, otherwise it returns false.

    2. OR (||) operator: This operator returns true if either of the operands is true, otherwise it returns false.

    3. NOT (!) operator: This operator returns true if the operand is false, and returns false if the operand is true.

    Logical Operator Table in Php Programming
    SymbolsOperationsExamples
    &&Logical ANDx&&y
    ||Logical ORx||y
    !Logical NOTx!y

    Logical Operator in Php with Example Program

    <?php

    $marks=55;

    if($marks>=60)

    {

    echo "First Division<br>";

    }

    if($marks<60&&&marks>=45)

    {

    echo "Second Division<br>";

    }

    if($marks<45&&&marks>=33)

    {

    echo "Third Division<br>";

    }

    if($marks<33)

    {

    echo "Sorry! You are Fail<br>";

    }

    ?>


    **********OUTPUT**********

    Second Division


    Bitwise Operators in Php Definition

    Bitwise operators: A Php programming language, there are several bitwise operators that you can use to perform bitwise operations on integers.

    1. Bitwise AND (&) operator: This operator performs a bitwise AND operation on two operands. It returns 1 if both the operands are 1, and returns 0 otherwise.

    2. Bitwise OR (|) operator: This operator performs a bitwise OR operation on two operands. It returns 1 if either of the operands is 1, and returns 0 otherwise.

    3. Bitwise XOR (^) operator: This operator performs a bitwise exclusive OR operation on two operands. It returns 1 if either of the operands is 1, but not both, and returns 0 otherwise.

    4. Bitwise NOT (~) operator: This operator performs a bitwise NOT operation on an operand. It inverts all the bits of the operand.

    5. Bitwise left shift (<<) operator: This operator shifts the bits of an operand to the left by a specified number of positions.

    6. Bitwise right shift (>>) operator: This operator shifts the bits of an operand to the right by a specified number of positions.

    Bitwise Operator Table in Php Programming
    SymbolsOperationsExamples
    &Bitwise ANDx&y
    |Bitwise ORx|y
    ^Bitwise XORx^y
    ~Bitwise NOTx~y
    <<Bitwise LEFT SHIFTx<<2
    >>Bitwise RIGHT SHIFTx>>2

    Bitwise Operators in Php with Examples

    <?php

    $x=3;

    $y=5;


    echo"Bitwise AND Operator:".($f&$s)."<br>"

    echo"Bitwise OR Operator:".($f|$s)."<br>"

    echo"Bitwise  LEFT SHIFT Operator:".($f<<2)."<br>"

    echo"Bitwise  RIGHT SHIFT Operator:".($f>>1)."<br>"


    ?>


    **********OUTPUT**********

    Bitwise AND Operator:1

    Bitwise OR Operator:7

    Bitwise  LEFT SHIFT Operator:12

    Bitwise  RIGHT SHIFT Operator:1


    Assignment Operator Php Implementation

    Assignment operators: A Php programming language, there are several assignment operators that you can use to assign a value to a variable.

    Compound assignment operators: These operators are used to perform an operation and assign the result to a variable in a single statement. The compound assignment operators are:

    1. Addition assignment operator(+=): This operator adds the right operand to the left operand and assigns the result to the left operand.

    2. Subtraction assignment operator(-=): This operator subtracts the right operand from the left operand and assigns the result to the left operand.

    3. Multiplication assignment operator(*=): This operator multiplies the left operand by the right operand and assigns the result to the left operand.

    4. Division assignment operator(/=): This operator divides the left operand by the right operand and assigns the result to the left operand.

    5. Modulus assignment operator(%=): This operator calculates the remainder when the left operand is divided by the right operand and assigns the result to the left operand.

    Assignment Operator Table in Php Programming
    SymbolsOperationsExamples
    +=Additionx+=y or x=x+y
    -=Subtractionx-=y or x=x+y
    *=Multiplicationx*=y or x=x+y
    /=Divisionx/=y or x=x+y
    %=Modulusx%=y or x=x+y


    Assignment Operators in Php with Example Program

    <?php

    $x=13;

    $y=5;


    $x1+=$y;

    $x1-=$y;

    $x1*=$y;

    $x1/=$y;

    $x1%=$y;


    echo"Assignment Addition:".$x."<br>";

    echo"Assignment Subtraction:".$x."<br>";

    echo"Assignment Multiplication:".$x."<br>";

    echo"Assignment Division:".$x."<br>";

    echo"Assignment Modulus:".$x."<br>";

    ?>


    **********OUTPUT**********

    Assignment Addition:18

    Assignment Subtraction:8

    Assignment Multiplication:65

    Assignment Division:2.6

    Assignment Modulus:3


    Increment Decrement Operator Program in Php

    Increment and decrement operators: A Php, there are two increment operators: ++ and --. The ++ operator increases the value of a variable by 1, while the -- operator decreases the value of a variable by 1.

    Pre increment and Post increment Operator in Php examples
    SymbolsOperationsExamples
    ++Pre-Incrementx++
    --Pre-Decrementx--
    ++Post-Increment++x
    --Post-Decrement--x

    Increment Operator Php Example

    <?php

    $x=3;

    $y=5;

        echo"++x:".++$x."<br>";

        echo"--y:".--$y."<br>";

    ?>


    **********OUTPUT**********

    ++x:4

    --y:4


    Use of Conditional Operator in Php

    Conditional Operator: The conditional operator, also known as the ternary operator, is an operator in Php that allows you to assign a value to a variable based on a certain condition. It is written as "condition ? value1 : value2", and it works as follows:

    1. First, the condition is evaluated.
    2. If the condition is true, the operator returns value1.
    3. If the condition is false, the operator returns value2.

    Conditional Operator in Php Example Program

    <?php

    $x=3;

    $y=5;

       

    $str=$x>$y?"x is greater":"y is greater";

    echo $str;

    ?>


    **********OUTPUT**********

    y is greater




    Post a Comment

    0 Comments