• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » PHP » PHP Operator Types
Next →
← Prev

PHP Operator Types

By Dinesh Thakur

There are 12 types of operators in PHP. We will review them using summary tables. Let’s start by arithmetic operators.

We’ll be covering the following topics in this tutorial:

  • Arithmetic operators
  • Increment operators / decrement
  • The assignment operator
  • The character string operators
  • Comparison operators
  • Logical operators
  • Binary operators (bitwise)
  • The Combined Operators
  • The error control operator
  • The execution operator
  • Operators on arrays
  • The object type operators (instance of)
  • Operator precedence

Arithmetic operators

Arithmetic operators are used to perform mathematical operations on variables. They are naturally all conventional operations such as addition, multiplication, subtraction or division. The following table summarizes all the possible mathematical operations.

OperatorOperationExampleResult
–Negation-$a$Has opposite
+Addition$a + $bSum of $a and $b
*Multiplication$a * $bProduct of $a and $b
–Subtraction$a – $bDifference of $a and $b
/Division$a / $bQuotient of $a and $b
%Modulus$a % $bRemaining $a /$b

Note: The modulus operator returns the remainder of the division between two numbers. Its use allows for example to determine the parity of a number or to achieve alternating colors of the lines of an HTML table.

Increment operators / decrement

The increment operator (respectively decrement) increases (respectively decrease) by one unit the value of the variable. Are used essentially in the loops to update the counter value in each iteration.

OperatorOperationExampleResult
++Pre-Increment++$aIncrements $a, then returns $a
++Post-Increment$a++Returns $a,then increments $a
—Pre-Decrement–$aDecrements $a, then returns $a
—Post-Decrement$a–Returns $a, then decrements $a

These operators can be placed either before or after the variable one. This nuance perhaps interesting when using loop.

Generally, the use cases are that one employs over $a++ ($a– respectively).

The assignment operator

This is probably the most basic and essential operator of the PHP language. It is through him that one assigns a value to a variable, which creates an array or that we instantiate a class.

OperatorOperationExampleResult
=Assignment$a = 3Sets the value a to $3

The character string operators

They are two in number. The first is the concatenation operator realized by the period (.) While the second is the concatenating assignment operator by. =

Operator Operation  ExampleResult 
 . Concatenation $a . $b Concatenates the values of$a and $b
 .= ConcatenatingAssignment  $a .= $b Adds the value of $b following that of $a

Comparison operators

They are mainly used in conditional structures (if, elseif, else, for, while, …) to compare values between them. Its tests will return TRUE (true) if the comparison is true or false (false) if the comparison is false.

OperatorOperationExampleResult
== Equalvalue $a == $b Verifies that the values $a and $b are identical
 === Equalin value and Type 
 $a === $b Check the values and types of $a and $b are identical
 != Differenceinvalue $a != $b Verifies that the values $a and $ b are different
 !== Differenceinvalue and type $a !== $b Check the values and types of $a and $b are different
 <> Differenceinvalue $a <> $b Alias! =
 < strictinferiority $a < $b Verifies that $a is strictly less than $b
 <= Less than or equal $a <= $b Verifies that $a is strictly less than or equal to $b
 > strictsuperiority $a > $b Verifies that $a is strictly greater than $b
 >= Greater than or equal $a >= $b Verifies that $a is strictly greater than or equal to $b

Note: Do not confuse the assignment operator that works with a single equal sign (=) and which fixes the value of a variable.

When it is desired to compare both the values and types of variables, the operator is used === (respectively! ==). This check is recommended for two reasons:

• This ensures that the two variables are the same type.
• This is slightly faster execution than its sister who does not check the type.

To illustrate the distinction between the two forms of comparison:

Comparison test on variables
<?php
    $a = '2'; // Type string
    $b = 2; // Integer type
    $c = 2; // Integer type

    // Compare the values
    if ($a == $b)
       {
           echo ‘$a and $b have the same value!’;
       }

    // Compare the values and types
   if ($a === $c)
      {
          echo ‘$a and $c are of the same value and type’;
      }
?>

These two examples introduce the concept of control structures provided by which we will study in the next tutorial. We have declared three variables the same value of 2. Only $ a is of type string (character string) while the other two are integer (int). When executing these two tests, we find that only the first displays. This is because double equals sign is not ensuring that the type of the two variables is the same contrast to the triple equal sign.

Logically, a string should not be “equal” to an integer. It would be like comparing apples and oranges !!! Here we see a weakness in terms of typing PHP variables, so this is why we speak of weak typing in the PHP language.

We advise you therefore to the extent possible to check your values ​​on both types and values.

Tip: PHP introduced intval () function which allows for the cast (or cast). It transforms an integer such as natural whole character string.

Logical operators

Logical operators are often used in control structures. They help to define more or less complex expressions that will return a boolean TRUE or FALSE. Below is a summary table of these operators.

OperatorOperationExampleResult
&&AND$a && $bTRUE if $a and $b are true
ANDAND$a AND $b&& Aliases
||OR$a || $bIf $a or $b is true
OROR$a OR $b|| Aliases
XORXOR$a XOR $bIf $a or $b is true, but not both
!NO!$aIf $a is false

The difference between AND and && (respectively OR and ||) is the priority of execution. The && and || operators have a higher priority compared to their respective literal similar.

Binary operators (bitwise)

These operators allow you to manipulate bits in an integer. If the left and right parameters are strings, the bitwise operator will act on the ASCII values of those characters.

OperatorOperationExampleResult
&AND$a & $bThe bits set to 1 in $a and $b are set to 1
|OR$a | $bThe bits set to 1 in$a or $b are set to 1
^XOR$a ^ $bThe bits set to 1 in $a or $b but not both are set to 1
~NO$a ~ $bThe bits that are set to 1 in $a is set to 0, and vice versa.
<<Left shift$a << $bShifts the bits of $a $b steps to the left(each step means a multiplication by 2)
>>Right shift$a >> $bShifting the bits of $a $b steps to the right(each step means division by 2)

The Combined Operators

The combined operators are operators combining arithmetic or binary operation with a summons. Hence the concept of combination. The following table summarizes these operators.

OperatorOperationExampleResult
+=Addition$a += 4Adds 4 to the value of $a and store the result in $a
-=Subtraction$a -= 4Subtract 4 tothe value of $a and store the result in $a
*=Multiplication$a *= 4Multiplied by the $4 of value and stores the result in $a
/=Division$a /= 4
Divided by the $4 of value and stores the result in $a
%=Modulus$a %= 4
Calculates the remainder of the division has $4 and stores the result in$a
&=ANDbinary$a &= $bEqual to $a =$a & $b
|=OR binary$a |= $bEqual to $a =$a |$b
^=XOR binary$a ^= $bEqual to $a = $a ^ $b
<<=Left shift$a <<= $bEqual to $a = $a << $b
>>=Right shift$a >>= $bEqual to $a = $a >> $b

Generalizing all, it has obtained $ (operator) = $b which is also equivalent to $a = $a (operator) $b.

The error control operator

It is the operator at sign (@) that removes all the errors generated by an expression (function, variables, constants, …). It is just up to the term for which you wish to hide the error that is thrown.

OperatorOperationExampleResult
 @ error @include(‘file.php’) Maskthe errorgenerated by theinclude ()

However this practice is strongly discouraged because we should not hide an error generated but rather it should be treated. However there are special cases where the use of this technique is useful. For example, when using fsockopen ().

The execution operator

This operator, delimited by apostrophes or backtick, allows you to run shell commands. It behaves the same way as the shell_exec () function. If it is turned off or if the safe mode then the operator will not work for safety reasons (on shared accommodation for example).

OperatorOperationExampleResult
` `Shell`ifconfig`Executes theshell commandand returns the result

 The result of an executed shell commands can be restored as well to standard output in a variable. Illustration by an example from the official documentation of the executing operator.

Using the runtime operator

<?php
    $output = 'ls -at';
    echo "<pre> $output </ pre>";
?>

Operators on arrays

As for the strings, PHP offers a series of operators to manipulate the tables. These operators are the same as the comparison operators do not necessarily have the same function and the same meaning.

OperatorOperationExampleResult
+union$a + $bUnion of $a and $b
==Equality$a == $bTRUE if $a and $b contain the same key / value pairs
===identical$a === $bTRUE if $a and $b contain the same key / value pairs in the same order and type
!=inequality$a != $bTRUE if $a and $b are not equal
<>inequality$a <> $bAlias! =
!==not identical$a !== $bTRUE if $a and $b are not identical

The object type operators (instance of)

The object type operator (= instanceof “Instance”) used in object-oriented programming to determine if an object, its parent and its derived classes are the same type or not.

OperatorOperationExampleResult
instanceofInstance$a instance of bIf $a is an instance of class b

Take a simple example to illustrate this concept.

Using the instance of operator

<?php

    // Class declaration
   class Student {}
    class Professor {}

    // Instantiating an object of type Student
    $Karan = new Student ();

    // Testing the type of the object
    if ($Karan instanceof Student) {

         echo ‘Karan is a student!’;
     }
     else
     {
        echo ‘Karan is a teacher!’;
     }

?>

In our example, you create an object $Karan type of Student (= instance of a Student class). Then we test whether that object is of type Student with the instanceof operator. If the test returns TRUE then $Karan is of type Student, otherwise it is of type Professor.

Operator precedence

As in mathematics, operators prioritize them when executed. The following table summarizes each of the priorities of these operators.

PriorityOperator
1new
2() And []
3– ++ And!
4~ – (Int) (float) (string) (array) (object) @
5instanceof
6* / And%
7+ – And.
8<< And >>
9<<= And> =>
10==! = === And
11&
12^
13|
14&&
15||
16? and:
17Combined assignment operators
18AND
19XOR
20OR

You’ll also like:

  1. How many Types of Comments are there in PHP
  2. PHP Constants Types
  3. PHP Variable Types
  4. Java Operator | Types of Operator in Java
  5. PHP Cookies
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

PHP Tutorials

PHP Tutorials

  • PHP - Home
  • PHP - Features
  • PHP - Magic Methods
  • PHP - Imagefilter
  • PHP - Arrays Numeric
  • PHP - Sessions
  • PHP - Forms Processing
  • PHP - clone()
  • PHP - Cookies
  • PHP - Variable Types
  • PHP - First program
  • PHP - call()
  • PHP - Iterator interface
  • PHP - Imports files
  • PHP - Exception Handling
  • PHP - set() and get()
  • PHP - Install MAMP
  • PHP - Functions
  • PHP - Constants Types
  • PHP - Comments Types
  • PHP - OOP's
  • PHP - OOps Use
  • PHP - PHP Code & Redirect 301
  • PHP - Control structures
  • PHP - Abstract Classes
  • PHP - Control structures
  • PHP - Classes
  • PHP - MySQL NULL values
  • PHP - Methods Visibility
  • PHP - Operator Types
  • PHP - Short tags Not use
  • PHP - Object and class
  • PHP - Secure Passwords

Other Links

  • PHP - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2023. All Rights Reserved.