What are operators in PHP? PHP – Operator Types

Operators are used to developing data. In Programming Languages, Operators are taken from Mathematics and Operator Programmers work with Data. An operator is a chain of symbols that is used with some values. We use Operators to perform operations on Variables and Values.

Operators help us to perform any variable or any value operation. This means that operators will give a new result by doing some operation inside any operand.

Types of PHP Operators

1. Arithmetic Operators

Arithmetic Operators are used to performing Arithmetic Operations with variables. These + , – , * , / , % , ++, — Arithmetic Operators are used in PHP language.

<?php
    echo $a + $b . "<br>"; 
    echo "For a + b, the result is " . ($a + $b) . "<br>"; 
    echo "For a - b, the result is " . ($a - $b) . "<br>";   
    echo "For a * b, the result is " . ($a * $b) . "<br>";   
    echo "For a / b, the result is " . ($a / $b) . "<br>";  
    echo "For a % b, the result is " . ($a % $b) . "<br>";
    echo "For a ** b, the result is " . ($a ** $b) . "<br>"; 
?>

Output:-

2. Assignment Operators

Assignment operators are used with numeric values to assign a value to a variable. In PHP language these = , += , -= , *= , /= , %= Assignment Operators are used.

<?php
    $a = 20;
    $b = 3;
    
    echo "<b> Assignment Operators </b>";
    echo "<br>";
    echo "<br>";

    $x  = $a;
    echo "For x, the value is ". $a . "<br>";
    echo "<br>";

    $a += 5;
    echo "For x, the value is ". $a . "<br>";
    echo "<br>";
    $a -= 5;
    echo "For x, the value is ". $a . "<br>";
    $a *= 5;
    echo "For x, the value is ". $a . "<br>";
    $a /= 5;
    echo "For x, the value is ". $a . "<br>"; 
?>

Output:-

3. Comparison Operators

Comparison operators are used to comparing the values of two variables. These == , != , > , < , >= , <= Comparison Operators are used in the PHP language.

<?php
    echo "<b> Comparison Operators </b>";
    echo "<br>";
    echo "<br>";

    $x = 5;
    $y = 7;

    echo "For x == y, the result is ". ($x == $y) . "<br>";

    echo "For x == y, the result is "; 
    echo var_dump($x == $y); 
    echo "<br>";
?>

Output:-

4. Logical Operators

Logical operators are used to performing logic and these operators are also used in control statements. In PHP language these and, or, &&, ||, Logical Operators are used.

<?php
      echo "<b> Logical Operators </b>";
      echo "<br>";
      echo "<br>";
  
      $m = true;
      $n = false;
  
      echo "For m and n, the result is";
      echo var_dump($m and $n);
      echo "<br>"; 

      echo "For m or n, the result is";
      echo var_dump($m or $n);
      echo "<br>";

      echo "For m && n, the result is";
      echo var_dump($m && $n);
      echo "<br>";

      echo "For m || n, the result is";
      echo var_dump($m || $n);
      echo "<br>";

      echo "For !m, the result is";
      echo var_dump(!$m);
      echo "<br>";
?>

Output:-

Related Posts

PHP explode() Function Example Tutorial

A PHP explode function example is given in this article. I will demonstrate how to use PHP’s explode string to array function. This is a basic example…

How to Convert Array to String in PHP?

We will cover the example of converting an array to a string in PHP in this lesson. We’ll examine an example of converting an array to a…

How Can I Convert a String in PHP Into an Array?

We will go over how to transform a string into an array in PHP in this little tutorial. Describe how to convert a string value into an…

How to Use the array_merge() Function in PHP?

We will go over the simple and quick method to use the PHP array_merge() function example in this brief tutorial. Let’s talk about the PHP example of…

How to Merge Two Arrays in PHP?

I explained simply how to merge two arrays in PHP Laravel. you will learn how to append two arrays in PHP. you will learn how to merge…

How to Get the First 5 Elements of Array in PHP?

Here, I will show you the php array to get the first 5 elements. let’s discuss about php array getting 5 first element values. you can see…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x