Switch to full style
:read: Start PHP with us. Includes topics to help you in php
Post a reply

PHP operators

Sat Sep 06, 2008 3:01 pm

I will start with you by the list of operators type :
    * Assignment Operators
    * Arithmetic Operators
    * Comparison Operators
    * String Operators
    * Combination Arithmetic & Assignment Operators

1- Assignment Operators


This is done using "="
For example
Code:
   <?
        $x=5;
        $y=$x;
         
    ?>


now both x , and y have the value 5

2 - Arithmetic Operators



Operator English Example

Code:
   
    +  Addition            1 + 2
     -    Subtraction           4 - 2
     *  Multiplication            2 * 2
    /     Division              20 / 4
    %  Modulus                  7 % 3


Code:
<?php 
$addition 
2
$subtraction 2
$multiplication 2
$division 20 4
$modulus 3
echo 
"addition: 1 + 2= ".$addition."<br />"
echo 
"subtraction: 4 - 2 = ".$subtraction."<br />"
echo 
"multiplication:  2* 2 = ".$multiplication."<br />"
echo 
"division: 20 / 4 = ".$division."<br />"
echo 
"modulus: 7 % 3 = " $modulus ;
   


the output is :
Code:
addition: 1+ 2 = 3
subtraction: 4 - 2 = 2
multiplication: 2 * 2 = 4
division: 20 / 4 = 5
modulus: 7 % 3 = 1


3- Comparison Operators


Operator English 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


4. String Operators


The dot (.) Operator between two strings as we saw before . like :
Code:
   $mstring1="Welcome to ";
   $mstring2="Codemiles Forums";
    echo $mstring1.$mstring2;


the output is :
Code:
Welcome to Codemiles Forums



5- Combination Arithmetic & Assignment Operators


If you want to increase a variable by some value . you can do the follow :
$x=$x+1;

or using short hand :
$x+=1;

here all some examples
Code:
Operator   English    Example    Equivalent Operation
+=          Plus Equals     $x += 1;    $x = $x + 1;
-=           Minus Equals     $x -= 1;    $x = $x - 1;
*=          Multiply Equals    $x *= 1;    $x = $x * 1;
/=           Divide Equals    $x /= 1;    $x = $x / 1;
%=         Modulo Equals    $x %= 1;    $x = $x % 1;
.=              Concatenate Equals    $my_str.="codemiles";    $my_str = $my_str . "codemiles";



before i close this topic i will talk about some thing which is . how to do pre/post increment and decrement . It is more shorter for adding or subtracting 1 from a variable.

$x++ is equal to $x+=1 or $x=$x+1
$x-- is equal to $x-=1 or $x=$x-1

Is $x++ different from $++x in your code ? Yes :confused:

you specify the increment before or after the line of code is executed
Example
Code:
$value=10;
  echo "my value is ".$x++;
echo "my value is ".$x;
$value=10;
  echo "<br/>my value is ".$++x;
echo "my value is ".$x;



the output is :
Code:
my value is 10 my value is 11
my value is 11 my value is 11


i wish it is clear :wink:

thanks



Post a reply
  Related Posts  to : PHP operators
 Using Assignment Operators     -  
 shortcut operators     -  
 difference between the >> and >>> operators     -  
 Bitwise operators     -  
 using bit-wise operators in c++     -  
 Equality Operators     -  
 C++ Increment Operators     -  
 Comparison Operators in java     -  
 Arithmetic Operators in java     -  
 Relational Operators table     -  

Topic Tags

PHP Basics