SlideShare une entreprise Scribd logo
1  sur  8
Télécharger pour lire hors ligne
Operators in PHP

Introduction

Operators are a special type of symbols, which are used for calculation and comparison in a
programming language. Operators are also used to operate on values. There are six types
operators in PHP, which are as follows:

      Arithmetic Operators
      Assignment Operators
      Comparison Operators
      Logical Operators
      Increment/Decrement Operators
      Conditional Operators

Arithmetic Operator

These operators are used for calculation like add, subtraction, multiplication, division and
modulation.

      Plus Operator (+) :- This operator is used for add two or more numbers. This is
       known as addition operator.
      Minus Operator (-) :- This operator is used for subtract two more numbers. This
       operator is known as subtraction operator.
      Multiplication Operator (*) :- This operator is used for find the product of two or
       more numbers.
      Division Operator (/) :- This operator is used for find the quotient of two numbers.
      Modulus Operator (%) :- This operator is used to find the quotient and remainder of
       two numbers.

Examples of arithmetic operators are as follows:

<?php
$x=5;
$y=2;
$addition=$x+$y; //add two number
$subtraction=$x-$y; //subtract two number
$multiplication=$x*$y; //multiply two number
$division=$x/$y; //divide two numbers
$modulus=$x%$y; //modulus two numbers
echo "The addition is :" .$addition.'<br>';
echo "The subtraction is :" .$subtraction.'<br>';
echo "The multiplication is :".$multiplication.'<br>';
echo "The division is :".$division.'<br>';
echo "The modulus is :".$modulus.'<br>';
?>

Output



                       www.vineetsaini.wordpress.com
Assignment Operators

The assignment operators are used to set a variable equal to a value or set a variable to
another variable's value. This operator is denoted by the (equal) "=" sign. There are many
types of assignment operators in PHP, which are as follows.

      = :- This operator is used for assign the value to the variable. e.g. x=5.
      += :- This operator is used for assign the value and add the value to the variable.
       e.g. x+=5 i.e. x=x+5.
      -= :- This operator is used for assign the value and subtract the value to the
       variable. e.g. x-=5 i.e. x=x-5.
      *= :- This operator is used for assign the value and multiply the value to the
       variable. e.g. x*=5 i.e. x=x*5.
      /= :- This operator is used for assign the value to the variable and find the quotient.
       e.g. x/=5 i.e. x=x/5.
      %= :- This operator is used for assign the value to the variable and find the
       modulation i.e. remainder and quotient. e.g. x%=5 i.e. x=x%5.
      .= :- This operator is used for assign the value to the variable and add the new value
       in the variable.

Examples of assignment operators are as follows:

<?php
$x=5;
$x+=5; //x=x+5 i.e. x=5+5=10
echo "The value of x is :".$x.'<br>';
$y=8;
$y-=5; //y=y-5 i.e. y=8-5=3
echo "The value of y is :".$y.'<br>';
$z=2;
$z*=3; //z=z*3 i.e. z=2*3=6
echo "The value of z is :".$z.'<br>';
$p=7;
$p/=2; // p=p/2 i.e. p=7/2=3.5



                      www.vineetsaini.wordpress.com
echo "The value of p is :".$p.'<br>';
$q=8;
$q%=3; //q=q%3 i.e. q=8%3=2 (remainder)
echo "The value of q is :".$q.'<br>';
?>

Output




Decision Statement (if...else)

Before discussing comparison operators I will discuss the decision statement. A decision
statement is represented by if...else. A decision statement has two parts i.e. one is if and
second is else.
If is the part of a decision statement where we supply a condition and it has a block of
commands that execute when the condition is true.
The second part of the decision statement is else which has commands and executes when
the condition is false.

Syntax of if...else

 if (expression)
  {
      true;
  }
 else
  {
     false;
   }

Comparison Operators

Comparison operators are used to check the relationship between variables and values.
There are many types of comparison operators in PHP, which are as follows:



                      www.vineetsaini.wordpress.com
   == :- This operator is known as equal to operator. e.g. 5==5 i.e. return true.
      != :- This operator is known as not equal to operator. e.g. 5!=5 return false.
      < :- This operator is known as less than operator. e.g. 5<5 return false.
      > :- This operator is known as greater than operator. e.g. 5>5 return false.
      <= :- This operator is known as less than equal to operator. e.g. 5<=2 return
       false.
      >= :- This operator is known as greater than equal to operator. e.g. 5>=2 return
       true.

Examples of comparison operators are as follows:

<?php
$x=5;
$y=2;
if($x==$y)
{
echo "x is equal to y i.e. True".'<br>';
}
else
{
echo "x is not equal to y i.e. False".'<br>';
}
if($x<$y)
{
echo "x is less than y i.e. True".'<br>';
}
else
{
echo "x is not less than y i.e. False".'<br>';
}
echo "Same as you can use other comparison operator ";
?>

Output




                     www.vineetsaini.wordpress.com
Logical Operators

Logical operators are used to convert the operands to Boolean values and then perform a
respective comparison. There are many types of logical operators in PHP, which are as
follows:

      && :- This operator is known as logical and operator.
      || : This operator is known as logical or operator.
      ! :- This operator is known as logical not operator.

Examples of logical operators are as follows:


<?php
$x=5;
$y=5;
if($x&&$y) //logical and operator
{
echo "True".'<br>';
}
else
{
echo "False".'<br>';
}
if($x||$y) //logical or operator
{
echo "True".'<br>';
}
else
{
echo "False".'<br>';
}
if($x!=$y) //logical not operator
{
echo "True".'<br>';
}
else
{
echo "False".'<br>';
}
?>

Output




                      www.vineetsaini.wordpress.com
Increment/Decrement Operators

Increment/Decrement is a special type of operator. The Increment operator increases the
value of an operand by 1. The Decrement operator decreases the value of an operand by 1.
There are two types of increment/decrement operators which are as follows:

      Prefix Increment/Decrement operator
      Postfix Increment/Decrement operator

Examples of Increment/Decrement operators are as follows:

<?php
$x=5;
$y=$x++; // postfix increment operator
$z=++$x; // prefix increment operator
$a=$x--; // postfix decrement operator
$b=--$x; // prefix decrement operator
echo "The value of x in postfix increment operator is : " .$y.'<br>';
echo "The value of x in prefix increment operator is :".$z.'<br>';
echo "The value of x in postfix decrement operator is :".$a.'<br>';
echo "The value of x in prefix decrement operator is :" .$b.'<br>';
?>

Output




                      www.vineetsaini.wordpress.com
Conditional Operators

The Conditional operator is a special type of operator which is conditional. There are two
expressions in a conditional operator. If condition one is true then return expression 1
otherwise return expression 2.

Syntax of Conditional Operator

condition ? exp1 : exp 2 ;

Examples of the Conditional operator are as follows:

<?php
$x=5;
$y=6;
$z=($x>$y) ? 'True' : 'False' ;
echo $z;
?>

Output




                       www.vineetsaini.wordpress.com
Conclusion

So in this article you saw the types of operators and how to use these operators in PHP.




                     www.vineetsaini.wordpress.com

Contenu connexe

Tendances (20)

Data types in php
Data types in phpData types in php
Data types in php
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Php operators
Php operatorsPhp operators
Php operators
 
Uploading a file with php
Uploading a file with phpUploading a file with php
Uploading a file with php
 
Php
PhpPhp
Php
 
Php string function
Php string function Php string function
Php string function
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
Php array
Php arrayPhp array
Php array
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Php basics
Php basicsPhp basics
Php basics
 
php
phpphp
php
 

Similaire à Operators in PHP

Similaire à Operators in PHP (20)

PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
Php
PhpPhp
Php
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHP
 
Php-Continuation
Php-ContinuationPhp-Continuation
Php-Continuation
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
Programming with php
Programming with phpProgramming with php
Programming with php
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
 
Basic PHP
Basic PHPBasic PHP
Basic PHP
 
Operators in java script
Operators   in  java scriptOperators   in  java script
Operators in java script
 
Web app development_php_05
Web app development_php_05Web app development_php_05
Web app development_php_05
 
Php Basic
Php BasicPhp Basic
Php Basic
 
Php + my sql
Php + my sqlPhp + my sql
Php + my sql
 
Operator & Expression in c++
Operator & Expression in c++Operator & Expression in c++
Operator & Expression in c++
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 

Plus de Vineet Kumar Saini (20)

Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
 
Update statement in PHP
Update statement in PHPUpdate statement in PHP
Update statement in PHP
 
Delete statement in PHP
Delete statement in PHPDelete statement in PHP
Delete statement in PHP
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Database connectivity in PHP
Database connectivity in PHPDatabase connectivity in PHP
Database connectivity in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 

Dernier

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 

Dernier (20)

Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

Operators in PHP

  • 1. Operators in PHP Introduction Operators are a special type of symbols, which are used for calculation and comparison in a programming language. Operators are also used to operate on values. There are six types operators in PHP, which are as follows:  Arithmetic Operators  Assignment Operators  Comparison Operators  Logical Operators  Increment/Decrement Operators  Conditional Operators Arithmetic Operator These operators are used for calculation like add, subtraction, multiplication, division and modulation.  Plus Operator (+) :- This operator is used for add two or more numbers. This is known as addition operator.  Minus Operator (-) :- This operator is used for subtract two more numbers. This operator is known as subtraction operator.  Multiplication Operator (*) :- This operator is used for find the product of two or more numbers.  Division Operator (/) :- This operator is used for find the quotient of two numbers.  Modulus Operator (%) :- This operator is used to find the quotient and remainder of two numbers. Examples of arithmetic operators are as follows: <?php $x=5; $y=2; $addition=$x+$y; //add two number $subtraction=$x-$y; //subtract two number $multiplication=$x*$y; //multiply two number $division=$x/$y; //divide two numbers $modulus=$x%$y; //modulus two numbers echo "The addition is :" .$addition.'<br>'; echo "The subtraction is :" .$subtraction.'<br>'; echo "The multiplication is :".$multiplication.'<br>'; echo "The division is :".$division.'<br>'; echo "The modulus is :".$modulus.'<br>'; ?> Output www.vineetsaini.wordpress.com
  • 2. Assignment Operators The assignment operators are used to set a variable equal to a value or set a variable to another variable's value. This operator is denoted by the (equal) "=" sign. There are many types of assignment operators in PHP, which are as follows.  = :- This operator is used for assign the value to the variable. e.g. x=5.  += :- This operator is used for assign the value and add the value to the variable. e.g. x+=5 i.e. x=x+5.  -= :- This operator is used for assign the value and subtract the value to the variable. e.g. x-=5 i.e. x=x-5.  *= :- This operator is used for assign the value and multiply the value to the variable. e.g. x*=5 i.e. x=x*5.  /= :- This operator is used for assign the value to the variable and find the quotient. e.g. x/=5 i.e. x=x/5.  %= :- This operator is used for assign the value to the variable and find the modulation i.e. remainder and quotient. e.g. x%=5 i.e. x=x%5.  .= :- This operator is used for assign the value to the variable and add the new value in the variable. Examples of assignment operators are as follows: <?php $x=5; $x+=5; //x=x+5 i.e. x=5+5=10 echo "The value of x is :".$x.'<br>'; $y=8; $y-=5; //y=y-5 i.e. y=8-5=3 echo "The value of y is :".$y.'<br>'; $z=2; $z*=3; //z=z*3 i.e. z=2*3=6 echo "The value of z is :".$z.'<br>'; $p=7; $p/=2; // p=p/2 i.e. p=7/2=3.5 www.vineetsaini.wordpress.com
  • 3. echo "The value of p is :".$p.'<br>'; $q=8; $q%=3; //q=q%3 i.e. q=8%3=2 (remainder) echo "The value of q is :".$q.'<br>'; ?> Output Decision Statement (if...else) Before discussing comparison operators I will discuss the decision statement. A decision statement is represented by if...else. A decision statement has two parts i.e. one is if and second is else. If is the part of a decision statement where we supply a condition and it has a block of commands that execute when the condition is true. The second part of the decision statement is else which has commands and executes when the condition is false. Syntax of if...else if (expression) { true; } else { false; } Comparison Operators Comparison operators are used to check the relationship between variables and values. There are many types of comparison operators in PHP, which are as follows: www.vineetsaini.wordpress.com
  • 4. == :- This operator is known as equal to operator. e.g. 5==5 i.e. return true.  != :- This operator is known as not equal to operator. e.g. 5!=5 return false.  < :- This operator is known as less than operator. e.g. 5<5 return false.  > :- This operator is known as greater than operator. e.g. 5>5 return false.  <= :- This operator is known as less than equal to operator. e.g. 5<=2 return false.  >= :- This operator is known as greater than equal to operator. e.g. 5>=2 return true. Examples of comparison operators are as follows: <?php $x=5; $y=2; if($x==$y) { echo "x is equal to y i.e. True".'<br>'; } else { echo "x is not equal to y i.e. False".'<br>'; } if($x<$y) { echo "x is less than y i.e. True".'<br>'; } else { echo "x is not less than y i.e. False".'<br>'; } echo "Same as you can use other comparison operator "; ?> Output www.vineetsaini.wordpress.com
  • 5. Logical Operators Logical operators are used to convert the operands to Boolean values and then perform a respective comparison. There are many types of logical operators in PHP, which are as follows:  && :- This operator is known as logical and operator.  || : This operator is known as logical or operator.  ! :- This operator is known as logical not operator. Examples of logical operators are as follows: <?php $x=5; $y=5; if($x&&$y) //logical and operator { echo "True".'<br>'; } else { echo "False".'<br>'; } if($x||$y) //logical or operator { echo "True".'<br>'; } else { echo "False".'<br>'; } if($x!=$y) //logical not operator { echo "True".'<br>'; } else { echo "False".'<br>'; } ?> Output www.vineetsaini.wordpress.com
  • 6. Increment/Decrement Operators Increment/Decrement is a special type of operator. The Increment operator increases the value of an operand by 1. The Decrement operator decreases the value of an operand by 1. There are two types of increment/decrement operators which are as follows:  Prefix Increment/Decrement operator  Postfix Increment/Decrement operator Examples of Increment/Decrement operators are as follows: <?php $x=5; $y=$x++; // postfix increment operator $z=++$x; // prefix increment operator $a=$x--; // postfix decrement operator $b=--$x; // prefix decrement operator echo "The value of x in postfix increment operator is : " .$y.'<br>'; echo "The value of x in prefix increment operator is :".$z.'<br>'; echo "The value of x in postfix decrement operator is :".$a.'<br>'; echo "The value of x in prefix decrement operator is :" .$b.'<br>'; ?> Output www.vineetsaini.wordpress.com
  • 7. Conditional Operators The Conditional operator is a special type of operator which is conditional. There are two expressions in a conditional operator. If condition one is true then return expression 1 otherwise return expression 2. Syntax of Conditional Operator condition ? exp1 : exp 2 ; Examples of the Conditional operator are as follows: <?php $x=5; $y=6; $z=($x>$y) ? 'True' : 'False' ; echo $z; ?> Output www.vineetsaini.wordpress.com
  • 8. Conclusion So in this article you saw the types of operators and how to use these operators in PHP. www.vineetsaini.wordpress.com