PHP Operators

Posted by tutor | Posted in PHP Tutorial | Posted on 12-12-2009-05-2008

0

Operators are used to manipulate the values or directly or to operate on variables to manipulate values.

All the operators are categorized under the following:

  • Assignment Operators
  • Arithmetic Operators
  • Comparison Operators
  • String Operators

This section lists the different operators available in PHP.

Assignment Operators

Assignment operators assign a value to a variable, or the value of a variable to another variable.

$my_var = 4;
$another_var = $my_var;

Now both $my_var and $another_var contain the value 4.

Assignments can also be used in conjunction with arithmetic operators, which we will be studying later in this tutorial.

Arithmetic Operators

    Operator Description Example
    + Addition 2 + 4
    - Subtraction 6-2
    * Multiplication 5*3
    / Division 15/3
    % Modulus 43%10
$addition= 2+3;
$subtraction= 3-2;
$multiplication= 2*3;
$division= 22/11;
$modulus = 5 % 2;

Comparison Operators

Comparison Operators are used to compare the values of two operators. These operators are used inside conditional statements and evaluate to either true or false. PHP provides all the comparison operators that are provided by other programming language.
Assume: $x = 4 and $y = 5;

    Operator Description Example Result
    == Equal To $x == $y false
    != Not Equal To $x != $y true
    < Less Than $x < $y true
    > Greater Than $x > $y false
    <= Less Than or Equal To $x <= $y true
    >= Greater Than or Equal To $x >= $y false

String Operators

As we have learned before the dot operator is used to concatenate or join two strings.

$a_string=”Hello”;
$b_string=” Tutor”;
$new_string=$a_string . $_string;
Echo $new_string;
Output

Hello Tutor