Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Disclaimer
  • Corona Virus Stats (Covid-19)
  • Work with us
  • FB
  • LinkedIn
  • Twitter
  • Instagram.com
Tekraze

Tekraze

Dive Into Technology

  • Guides
    • Developer Guide
    • PC Guide
    • Web Guide
    • Android Guide
    • Music
    • Tutorials
  • Feed
    • Tech News
    • Shared Tech
    • Gaming Videos
    • Unboxing videos
  • Forums
    • Android Apps
    • Angular Npm Packages
    • Useful Site Links
    • Tech Queries
    • Windows OS Help
    • Web Guide and Help
    • Android Os And Rooting
    • Jhipster Discussion
    • Git & GitHub forum
    • Open Source Forum
  • Work with us
  • Toggle search form
  • Google to buy $4.5 bn stake in digital unit of India's Reliance 1
    Google to buy $4.5 bn stake in digital unit of India’s Reliance Tech News
  • Google, India's Reliance to develop entry-level smartphone 2
    Google, India’s Reliance to develop entry-level smartphone Tech News
  • 5 best apps for Android privacy 3
    5 best apps for Android privacy Android Guide
  • AI or artificial intelligence a contribution to a better future or just another trend?
    AI: a contribution to a better future or just another trend? Latest tech
  • How to choose the right GPS truck tracking system for your business
    How to choose the right GPS truck tracking system for your business Latest tech
  • Four Cybersecurity Essentials for Startups 4
    Four Cybersecurity Essentials for Startups Shared Tech Posts
  • Realme Narzo 10 Tekraze
    Realme Narzo 10 and Narzo 10A Launched Tech News
  • How AI is changing B2B Banner image
    How AI is changing B2B Businesses Tech News
PHP-Operators 5

PHP-Operators

Posted on April 8, 2018 By RAHUL CHANANA 2 Comments on PHP-Operators

We have learned in the last tutorial, about Reference Variable in PHP. Now, we will discuss operators in PHP.

Table of Contents

  • PHP Operators:
      • Types of Operators:
    •  Arithmetic Operators:
          • Description
          • Example
          • Result
    • Example:
    • Comparison Operators:
    • Example:
    • Logical Operators:
    • Example
    • Assignment Operators:
    • Example:
    • Incrementing and Decrementing Operators:
    • Example
    • Conditional Operator:
    • Example

PHP Operators:

Simple answer can be given using expression 2+ 5 is equal to 7. Here 2 and 5 are called operands and + is called operator. PHP language supports following type of operators.

Types of Operators:

  • Arithmetic Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Assignment Operators
  • Incrementing and Decrementing Operators
  • Conditional (or ternary) Operators
| Also Read | Variables and its Scope in PHP

 Arithmetic Operators:

The arithmetic operators are used to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Here’s a complete list of PHP’s arithmetic operators.

Operator
Description
Example
Result
+Addition$x + $ySum of $x and $y.
–Subtraction$x – $yDifference of $x and $y.
*Multiplication$x * $yProduct of $x and $y.
/Division$x / $yQuotient of $x and $y.
%Modulus$x % $yRemainder of $x divided by $y.

 

Example:

<?php
$x = 10;
$y = 4;
echo($x + $y);  // 0utputs: 14
echo($x - $y);   // 0utputs: 6
echo($x * $y);  // 0utputs: 40
echo($x / $y);   // 0utputs: 2.5
echo($x % $y);   // 0utputs: 2
?>

 

Comparison Operators:

The comparison operators are used to compare two values in a Boolean form.

 

OperatorNameExampleResult
==Equal$x == $yTrue if $x is equal to $y
===Identical$x === $yTrue if $x is equal to $y, and they are of the same type
!=Not equal$x != $yTrue if $x is not equal to $y
<>Not equal$x <> $yTrue if $x is not equal to $y
!==Not identical$x !== $yTrue if $x is not equal to $y, or they are not of the same type
<Less than$x < $yTrue if $x is less than $y
>Greater than$x > $yTrue if $x is greater than $y
>=Greater than or equal to$x >= $yTrue if $x is greater than or equal to $y
<=Less than or equal to$x <= $yTrue if $x is less than or equal to $y

 

Example:

<?php
$x = 25;
$y = 35;
$z = "25";
var_dump($x == $z);        // Outputs: boolean true
var_dump($x === $z);     // Outputs: boolean false
var_dump($x != $y);        // Outputs: boolean true
var_dump($x !== $z);     // Outputs: boolean true
var_dump($x < $y);        // Outputs: boolean true
var_dump($x > $y);       // Outputs: boolean false
var_dump($x <= $y);   // Outputs: boolean true
var_dump($x >= $y);   // Outputs: boolean false
?>

| Also Read | Introduction To PHP

 

Logical Operators:

The logical operators are typically used to combine conditional statements.

OperatorNameExampleResult
andAnd$x and $yTrue if both $x and $y are true
orOr$x or $yTrue if either $x or $y is true
xorXor$x xor $yTrue if either $x or $y is true, but not both
&&And$x && $yTrue if both $x and $y are true
||Or$x || $yTrue if either $$x or $y is true
!Not!$xTrue if $x is not true

 

Example

<?php
$year = 2014;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
echo "$year is a leap year.";
} else{
echo "$year is not a leap year.";
}
?>

 

Assignment Operators:

The assignment operators are used to assign values to variables.

OperatorDescriptionExampleIs The Same As
=Assign$x = $y$x = $y
+=Add and assign$x += $y$x = $x + $y
-=Subtract and assign$x -= $y$x = $x – $y
*=Multiply and assign$x *= $y$x = $x * $y
/=Divide and assign quotient$x /= $y$x = $x / $y
%=Divide and assign modulus$x %= $y$x = $x % $y

Example:

<?php
$x = 15;
echo $x;  // Outputs: 15

$x = 15;
$x += 30;
echo $x;  // Outputs: 45

$x = 30;
$x -= 20;
echo $x;  // Outputs: 10

$x = 5;
$x *= 20;
echo $x;  // Outputs: 100

$x = 100;
$x /= 20;
echo $x;  // Outputs: 5

$x = 100;
$x %= 15;
echo $x;  // Outputs: 10
?>

| Also Read | Reference Variable in PHP

Incrementing and Decrementing Operators:

OperatorNameEffect
++$xPre-incrementIncrements $x by one, then returns $x
$x++Post-incrementReturns $x, then increments $x by one
–$xPre-decrementDecrements $x by one, then returns $x
$x–Post-decrementReturns $x, then decrements $x by one

 

Example

<?php
$x = 5;
echo ++$x; // Outputs: 6
echo $x; // Outputs: 6

$x = 10;
echo $x++; // Outputs: 10
echo $x; // Outputs: 11

$x = 15;
echo --$x; // Outputs: 14
echo $x; // Outputs: 14

$x = 10;
echo $x--; // Outputs: 10
echo $x; // Outputs: 9
?>

Conditional Operator:

There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation.

OperatorDescriptionExample
? :Conditional ExpressionIf Condition is true ? Then value X : Otherwise value Y

 

Example

<?php
$a = 10;
$b = 20;

/* If condition is true then assign a to result otheriwse b */
$result = ($a > $b ) ? $a :$b;

echo "TEST1 : Value of result is $result<br/>";

/* If condition is true then assign a to result otheriwse b */
$result = ($a < $b ) ? $a :$b;

echo "TEST2 : Value of result is $result<br/>";
?>

| Also Read | PHP Data Types

So this is for today, in our next tutorial we will learn about Control Statements(Decison Making).Stay connected with us for more tutorials,  keep visiting for more coming in this series . Feel free to like, comment ,share and give your opinions in comments below. Your valuable comments help us in giving you more relevant content . Be a part of Tekraze family, have a nice day.

Content Protection by DMCA.com
PHP Tutorials Tags:Core PHP, Operators in PHP, PHP, Php-Operators, PHP5

Post navigation

Previous Post: BackEnd Technologies part 2- Databases | Tekraze
Next Post: Apple is Taking on Google in classroom War

Related Posts

  • PHP Data Types 6
    PHP Data Types PHP Tutorials
  • Introduction to PHP 7
    Introduction to PHP Web Guide
  • Reference Variable in PHP 8
    Reference Variable in PHP PHP Tutorials
  • How to Install PHP XAMPP 9
    How to Install PHP XAMPP PHP Tutorials
  • Variable & its Scope in PHP 10
    Variable & its Scope in PHP Developer Guide
  • Develop Mobile Apps with PHP & Have Some Kik in Life 11
    Develop Mobile Apps with PHP & Have Some Kik in Life Developer Guide

Comments (2) on “PHP-Operators”

  1. Robert Machado says:
    April 28, 2020 at 7:42 am

    tҺe website іѕ really good, I enjoy your blog!

    Reply
    1. Balvinder Singh says:
      April 28, 2020 at 12:27 pm

      Thanks, please share…

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Advertisements

Subscribe to updates

Enter your email below to subscribe





Posts by Categories

Advertisements
  • General requirements to build and run KIKAPP 12
    General requirements to build and run KIKAPP Android Guide
  • Google Map JSON Parser 13
    Google Map JSON Parser Developer Guide
  • What are the best Music Streaming Apps in India? 14
    What are the best Music Streaming Apps in India? Music
  • 5 factors that are slowing down your internet tekraze
    5 Factors That Are Slowing Down Your Internet Guest posts
  • Cloud Security for Business 15
    Cloud Security for Business Guest posts
  • How to Google Search Like A pro
    How to Google Search Like a Pro you must know Web Guide
  • The Latest Android Update Is Focused on Security
    The Latest Android Update Is Focused on Security Android Guide
  • Top Linux Distros - 2018 | Tekraze 16
    Top Linux Distros – 2018 | Tekraze Developer Guide

Affliate Links

Sell with Payhip

Earn with Magenet

Sell and Buy with Adsy

GainRock affiliate Program

Automatic Backlinks

Advertise with Anonymous Ads

accessily tekraze verificationIndian Blog Directory

Copyright © 2023 Tekraze.

Powered by PressBook News WordPress theme