-->

  • PHP all types of operator


    PHP all types of operator



    Operators are used to perform operations on some values. In other words, we can describe operators as something that takes some values, performs some operation on them and gives a result. From example, “1 + 2 = 3” in this expression ‘+’ is an operator. It takes two values 1 and 2, performs addition operation on them to give 3.

    Just like any other programming language, PHP also supports various types of operations like the arithmetic operations(addition, subtraction, etc), logical operations(AND, OR etc), Increment/Decrement Operations etc.  PHP all types of operator





    PHP divides the operators in the following groups:


    • Arithmetic operators
    • Assignment operators
    • Comparison operators
    • Increment/Decrement operators
    • Logical operators
    • String operators
    • Array operators
    • Conditional assignment operators




    Arithmetic operator



    The arithmetic operators are use to perform simple mathematical operations like addition, subtraction, multiplication etc. Below is the list of arithmetic operators along with there syntax and operations, that PHP provides us:



    PHP all types of operator



    Example 


    $x = 18;  
    $y = 6;

    echo $x + $y;
    echo "<br>";

    echo $x - $y;
    echo "<br>";

    echo $x * $y;
    echo "<br>";

    echo $x / $y;
    echo "<br>";

    echo $x % $y;
    echo "<br>";

    echo $x ** $y;
    echo "<br>";

    ?>



    </body>
    </html>





    24
    12
    108
    3
    0
    34012224





    Assignment operators


    These operators are used to assign values to different variable, with or without mid-operations. Here are the assignment operators along with there syntax and operations, that PHP provides us:



    PHP all types of operator



    Example 




    $a = 20;  
    $a += 100;

    echo $a;
    echo "<br>";


    $b = 10;  
    echo $b;
    echo "<br>";


    $c = 200;  
    $c -= 100;

    echo $c;
    echo "<br>";


    $d = 8;  
    $e = 6;

    echo $d * $e;
    echo "<br>";


    $f = 60;
    $f /= 6;

    echo $f;
    echo "<br>";


    $g = 15;
    $g %= 4;

    echo $g;
    echo "<br>";

    ?>



    </body>
    </html>





    120
    10
    100
    48
    10
    3

     

    Comparison Operators


    These operators are used to compare two elements and outputs the result in boolean form. Here are the comparison operators along with there syntax and operations, that PHP provides us:



    PHP all types of operator


    Example 


    $x = 100;  
    $y = "100";

    var_dump($x == $y); // returns true because values are equal

    echo "<br>";

    $a = 200;  
    $b = "200";

    var_dump($a === $b); // returns false because types are not equal


    echo "<br>";

    $c = 100;  
    $d = "100";

    var_dump($c != $d); // returns false because values are equal

    echo "<br>";


    $e = 100;  
    $f = "100";

    var_dump($e <> $f); // returns false because values are equal
    echo "<br>";


    $g = 100;  
    $h = "100";

    var_dump($g !== $h); // returns true because types are not equal
    echo "<br>";


    $i = 100;
    $j = 50;

    var_dump($i > $j); // returns true because $x is greater than $y
    echo "<br>";


    $x = 10;
    $y = 50;

    var_dump($x < $y); // returns true because $x is less than $y
    echo "<br>";


    $k = 50;
    $l = 50;

    var_dump($k >= $l); // returns true because $x is greater than or equal to $y
    echo "<br>";



    $m = 50;
    $n = 50;

    var_dump($m <= $n); // returns true because $x is less than or equal to $y
    echo "<br>";



    $o = 5;  
    $p = 10;

    echo ($o <=> $p); // returns -1 because $x is less than $y
    echo "<br>";


    $q = 10;  
    $r = 10;

    echo ($q <=> $r); // returns 0 because values are equal
    echo "<br>";


    $s = 15;  
    $t = 10;


    echo ($s <=> $t); // returns +1 because $x is greater than $y
    echo "<br>";
    echo "<br>";



    ?>



    </body>
    </html>





    bool(true)
    bool(false)
    bool(false)
    bool(false)
    bool(true)
    bool(true)
    bool(true)
    bool(true)
    bool(true)
    -1
    0
    1

     




    Logical or Relational Operators



    These are basically used to operate with conditional statements and expressions. Conditional statements are based on conditions. Also, a condition can either be met or cannot be met so the result of a conditional statement can either be true or false. Here are the logical operators along with there syntax and operations, that PHP provides us:



    PHP all types of operator



    Example 


    $x = 100;  
    $y = 50;

    if ($x == 100 and $y == 50) {
        echo "Hello world!";
    }

    echo "<br>";

    $a = 100;  
    $b = 50;

    if ($a == 100 or $b == 80) {
        echo "Hello kumarAtulJaiswal!";
    }

    echo "<br>";

    $d = 100;  
    $e = 50;

    if ($d == 100 xor $e == 80) {
        echo "welcome to our YT channel!";
    }

    echo "<br>";

    $f = 100;  
    $g = 50;

    if ($f == 100 && $g == 50) {
        echo "Hello world!";
    }

    echo "<br>";
    $h = 100;  
    $i = 50;

    if ($h == 100 || $i == 80) {
        echo "Dont forget to subscribe!";
    }

    echo "<br>";

    $j = 100;  

    if ($j !== 90) {
        echo "Hello viewers!";
    }

    ?>



    </body>
    </html>




    Hello world!
    Hello kumarAtulJaiswal!
    welcome to our YT channel!
    Hello world!
    Dont forget to subscribe!
    Hello viewers!



    ----------------



    Increment operators


    The PHP increment operators are used to increment a variable's value. ++a is pre increment in this case first value will be increase then assign in variable. Both will increase the value by 1. ++a: do the increment first then return the value of a. a++: return the value of a first, then increment after.



    PHP all types of operator




    Example 



    $a = 10;
    echo ++$a;

    echo "<br>";

    $b = 10;
    echo $b++;

    echo "<br>";

    $c = 10;
    echo --$c;

    echo "<br>";

    $d = 10;
    echo $d--;




    ?>



    </body>
    </html>



    11
    10
    9
    10

    ----------------

    PHP String Operators


    PHP has two operators that are specially designed for strings.



    PHP all types of operator



    Example 



    $txta = "Hello";
    $txtb = " world!";
    echo $txta . $txtb;

    echo "<br>";


    $txtc = "Hello";
    $txtd = " world!";
    $txtc .= $txtd;
    echo $txtc;



    ?>



    </body>
    </html>




    Hello world!
    Hello world!



     



    Array Operators


    The PHP array operators are used to compare arrays.






    Example 



    </head>
    <body>


    $xa = array("a" => "red", "b" => "green");  
    $yb = array("c" => "blue", "d" => "yellow");  

    print_r($xa + $yb); // union of $x and $y

    echo "<br>";

    $a = array("a" => "red", "b" => "green");  
    $b = array("c" => "blue", "d" => "yellow");  

    var_dump($a == $b);

    echo "<br>";

    $c = array("a" => "red", "b" => "green");  
    $d = array("c" => "blue", "d" => "yellow");  

    var_dump($c === $d);

    echo "<br>";

    $e= array("a" => "red", "b" => "green");  
    $f= array("c" => "blue", "d" => "yellow");  

    var_dump($e!= $f);

    echo "<br>";

    $g= array("a" => "red", "b" => "green");  
    $h= array("c" => "blue", "d" => "yellow");  

    var_dump($g <> $h);

    echo "<br>";

    $i = array("a" => "red", "b" => "green");  
    $j = array("c" => "blue", "d" => "yellow");  

    var_dump($i !== $j);

    echo "<br>";


    ?>



    </body>
    </html>




    Array ( [a] => red [b] => green [c] => blue [d] => yellow )
    bool(false)
    bool(false)
    bool(true)
    bool(true)
    bool(true)


    ----------------



    PHP Conditional Assignment Operators


    The PHP conditional assignment operators are used to set a value depending on conditions:



    PHP all types of operator




    Example 




    </head>
    <body>


    // if empty($user) = TRUE, set $status = "anonymous"

    echo $status = (empty($user)) ? "anonymous" : "logged in";
      
    echo "<br>";

    $user = "John Doe";

    // if empty($user) = FALSE, set $status = "logged in"
    echo $status = (empty($user)) ? "anonymous" : "logged in";
       
    echo "<br>";
       
       
    // variable $user is the value of $_GET['user']
    // and 'anonymous' if it does not exist
      
    echo $user = $_GET["user"] ?? "anonymous";

    echo "<br>";
      
    // variable $color is "red" if $color does not exist or is null
    echo $color = $color ?? "red";
     

      echo "<br>";


    ?>



    </body>
    </html>




    anonymous
    logged in
    anonymous
    red






    Video on This Topic :- Soon


    I hope you liked this post, then you should not forget to share this post at all.
    Thank you so much :-)



  • 0 comments:

    Post a Comment

    For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.