Operators in PHP

Dear pupils,

I hope you would have been practising the last lecture ( PHP Programming Basics ) as I am getting feedback from different students from different countries and I am very happy I am able to contribute to PHP through my series of PHP Articles for beginners and professionals.

Today we are going to discuss different types of operators used in PHP. I hope you remember the basic definition of the operators and operands from my last article ( PHP Programming Basics ) and I am not going to explain it again as we had promised in the very first article of this Series of PHP Tutorials that we will not see behind and this is just to compell you to concentrate on each and every article of this series and practise (O! I love practice)

PHP offers the following operators to perform different operations on the operands.

Arithmetic Operators:

Arithmetic operators enable you to perform different mathematical operations on different values.

Below are the quick details of arithmetic operators,

Addition: it is denoted by plus sign ( + ) and is used to add two values e.g 2 + 3 = 5

you can add variables as following,


 
<?php
 
// addition
 
$var1 = 4;
$var2 = 5;
$total = $var1 + var2;
 
echo "Total = " . $total;
 
// Total = 9
 
?>
 

Subtraction: it is denoted by minus sign ( - ) and is used to subtract first value from the second e.g 2 - 1 = 1

you can subtract two variables as following,


 
<?php
 
// subtraction
 
$var1 = 8;
$var2 = 5;
$total = $var1 - var2;
 
echo "Total = " . $total;
 
// Total = 3
 
?>
 

multipication: it is denoted by Asterisk sign ( * ) and is used to multiply two value e.g 2 * 3 = 6

you can multiply two variables as following,


 
<?php
 
// Multipication
 
$var1 = 8;
$var2 = 5;
$total = $var1 * var2;
 
echo "Total = " . $total;
 
// Total = 40
 
?>
 

Division: it is denoted by forward slash sign ( / ) and is used to divide first value on the second e.g 2 / 2 = 1

you can divide two variables as following,


 
<?php
 
// Division
 
$var1 = 15;
$var2 = 5;
$total = $var1 / var2;
 
echo "Total = " . $total;
 
// Total = 3
 
?>
 

Modulus(Division Remainder): it is denoted by percentage sign ( % ) and is used to give the division remainder e.g 3 % 2 = 1

you can find the modulus of two variables as following,


 
<?php
 
// Modulus
 
$var1 = 15;
$var2 = 5;
$total = $var1 % var2;
 
echo "Total = " . $total;
 
// Total = 0
 
?>
 

Increment: it is denoted by double-plus sign ( ++ ) and is used to increase value of one variable by one e.g


 
<?php
 
// Increment
 
$counter = 15;
$counter ++;   // 16
$counter ++;  // 17
 
echo "Counter = " . $counter;
 
// Counter = 17
 
?>
 

Remember there are two types of incrementation,

a) Prefix Increment: in which the increment operator comes before the variable name i.e ++$var;

In this type of increment the variable’s value is incremented and is available for the immediate use e.g


 
<?php
 
// Prefix Increment
 
$counter = 15;
 
echo "Counter = " . ++$counter;
 
// Counter = 16
 
?>
 

b) Postfix Increment: in which increment operation is performed however the resultant value is not available for the

immediate use and can be used only when the control access the variable in the next run e.g


 
<?php
 
// Postfix Increment
 
$counter = 15;
 
echo "Counter = " . $counter++;
 
// Counter = 15
 
?>
 

Decrement Operator: it is denoted by double-minus sign ( — ).

It is very similar to the increment operator however it decrease the value by one.

Assignment Operators:

Assignment operators are used to assign new values to a variable. We have the following assignment operator available in PHP,


<?php
 
$var1 = 3;
$var2 = 5;
 
$var1 = $var2;    //var1=5
$var1 += $var2;  //var1=8
$var1 -= $var2;  //var1= -2
$var1 *= $var2; //var1=15
$var1 /= $var2; //var1=0.6
$var1 %= $var2; //var1=3
 
?>

First of all in above all example i am assuming each operation for the basic values of var1 and var2 to be 3 and 5 respectively.

Comparison Operators:

comparison operators are used to compare two values. We can use following comparison operators in PHP,

( == ) is equal to
( != ) not equal to
( > ) greater than
( < ) less than
( >= ) greater than or equal to
( <= ) less than or equal to

Just learn these operators by heart we will use them in our up coming articles and you will be able to understand their exact use.

Logical Operators:

We have three logical operators in PHP,

AND ( && ) Operators: you can use both ‘AND’ and double-empersand sign ( && ) for this operator in PHP. The basic logical use of this operator in a rough psuedocode could be

If first condition is true && second condition is also true

OR ( || ) Operators: you can use both ‘OR’ and double-pipeKey sign ( || ) for this operator in PHP. The basic logical use of this operator in a rough psuedocode could be

Either the first condition is true || the second condition is true

NOT ( ! ) Operators: you can use both ‘NOT’ and Exclamination Mark (Mathematician will call it Factorial Sign) sign ( ! ) for this operator in PHP. The basic logical use of this operator in a rough psuedocode could be

First value is NOT equal to the second value

Again do not confuse the use of these operators but just learn them by heart and I will use these all operators in my up coming articles for more complex PHP programmes and then your all doubts will be removed hopefully.

This is one of the most important articles of this PHP Tutorials Series. Please concentrate hard on this articles and try to understand all issue discussed today and feel free to ask if you have any question and remember if you cannot understand this foundation lectures of PHP it will be very hard for you to survive when we will ‘BURN IT’ as I had promised with you that I will take you the Extreme PHP Programming and the Only rule of our game is “Don’t look behind” so you will not be able to go back to these articles again so learn these things now this article will make your strong foundation towards complex programming.

Let’s end this lecture here.

Regards,
Haroon Ahmad

9 Comments

  1. haroon.ahmad said,

    January 22, 2007 at 2:02 pm

    Ok, There were some typos that I have fixed by now. If you find any other typo or mistake please do inform me.

    Thanks to all guys who helped me to remove these typos.

  2. haroon.ahmad said,

    January 22, 2007 at 2:14 pm

    ==================================
    Don’t forget the (===) operator for mathing type as well as string value. I’ve found this operator ESSENTIAL to properly evaluating some function built into PHP.
    ==================================

    yes it is very useful operator when you want to match the data types as well as values e.g

    <?php
     
    $value1 = 3;
     
    $value2 = "3";
     
    if($value1 === $value2)
     
    echo "matched";
     
    else
     
    echo "not matched";
     
    ?>

    You will get the NOT MATCHED output because value1 has 3 as integer value on the other hand 3 is string in value2 variable. So whenever you need to match data type as well as value you should use === operator.

  3. amit beniwal said,

    January 22, 2007 at 6:21 pm

    hi haroon,
    thanks for this great work to help people who heve a great urge to learn php. haroon i am a bigner in php i read a php book and i have a good theortical knowledge of php but i have no idea where i can programmg for php and how can i run a program in internet explorer. i have install wamp in my p.c. but unable to work on it. I also install a php editor but no result plz help me where can i write a programme and how can i run it? I am waiting for ur response plz…..
    you are doing a great job plz carry on

    Amit beniwal

  4. haroon.ahmad said,

    January 23, 2007 at 10:06 am

    Hi Amit,

    My articles on PHP are in fact a series of articles in which I have started right from scratch and am moving step by step towards expert level.

    Please read the first few articles of my this series and you will get answers of your all questions though if you still have any question after reading articles you are welcome to post your question as a comment to the related articles and I will answer your all questions.

    Please study,

    FAST PHP Tutorial (Part 1)
    How to Install PHP on Windows (Fast PHP Tutorial Part 2)
    How To Install PHP On Linux
    How To Install Apache on Windows
    Easy Installers to Install PHP, MySQL, Apache on Windows
    PHP Programming Basics
    Operators in PHP
    Decision Making using IF-ELSE in php
    Decision Making using PHP Switch Statement

    - Haroon Ahmad

  5. Articles Repository » Blog Archive » Detailed Guide To Arrays In PHP said,

    January 29, 2007 at 12:55 pm

    […] FAST PHP Tutorial (Part 1) How to Install PHP on Windows (Fast PHP Tutorial Part 2) How To Install PHP On Linux How To Install Apache on Windows Easy Installers to Install PHP, MySQL, Apache on Windows PHP Programming Basics Operators in PHP Decision Making using IF-ELSE in php Decision Making using PHP Switch Statement Loops In PHP […]

  6. prabu said,

    September 11, 2008 at 5:52 pm

    can anyone tell me the use and function of the operator

  7. haroon.ahmad said,

    September 11, 2008 at 6:56 pm

    @prabu, you need to read this article again. it is all about operators.

  8. diyath said,

    October 16, 2008 at 10:36 am

    I wont to match two string variable in if condition .I use this code
    if($name==$group)
    {
    $s=”0k”
    }
    but it did not work plese help me

  9. haroon.ahmad said,

    October 16, 2008 at 11:19 am

    first of all echo the $name and $group to see what values are coming in them. It is possible wrong values are coming in them and IF stands false. Secondly use an ELSE to make sure it is failing the logic. to compare strings with == is kind of tricky, let’s see the following

    if(”apple” == “Apple”) {
    echo “never executed”;
    }
    else {
    echo “A value is less than a”;
    }

    in this case ELSE is executed because asci code of ‘A’ is not equal to ‘a’.

    For comparing strings you can use stricmp

Post a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word