PHP/Operator/Arithmetic Operators

Материал из Web эксперт
Перейти к: навигация, поиск

An example of PHP"s automatic type conversion is the addition operator "+".

 
<?
$foo = "0"; 
$foo++; 
$foo += 1; 
$foo = $foo + 1.3; 
$foo = 5 + "10 L"; 
$foo = 5 + "10 S"; 
?>



Arithmetical Operators

 
<?php 
  $addnum = 20+30;
  $addstr="I love "."PHP";
  $sub = 35.75 - 28.25;
  $mul = 8 * 50;
  $mod = 65 % 2;
  $inc = 5; $inc = ++$inc;
  $dec = 5; $dec = --$dec;
  $result = "addnum:$addnum  <br>";
  $result .= "addstr:$addstr <br>";
  $result .= "sub:$sub       <br>";
  $result .= "mul:$mul       <br>";
  $result .= "mod:$mod       <br>";
  $result .= "inc:$inc       <br>";
  $result .= "dec:$dec       <br>";
?>
<html>
 <head>
  <title>Arithmetical Operators</title>
 </head>
 <body>
  <h3> <?php echo($result); ?> </h3>
 </body>
</html>



Arithmetic Operators

 
Example           Operation              Result 
-$a               Negation               Negative value of $a 
$a + $b           Addition               Sum of $a and $b 
$a - $b           Subtraction            Difference of $a and $b 
$a * $b           Multiplication         Product of $a and $b 
$a / $b           Division               Quotient of $a and $b 
$a % $b           Modulus                Remainder of $a divided by $b



Calculation Results for Postfix and Prefix Operators

<?
$count = 0;
$result = $count++;
print("Post ++: count is $count, result is $result<BR>");
$count = 0;
$result = ++$count;
print("Pre ++: count is $count, result is $result<BR>");
$count = 0;
$result = $count--;
print("Post --: count is $count, result is $result<BR>");
$count = 0;
$result = --$count;
print("Pre --: count is $count, result is $result<BR>");
?>



Changing the default precedence using parentheses

 
<?
echo 2 * 3 + 4 + 1;
echo 2 * (3 + 4 + 1);
?>



Increment/decrement Operators

 
example    name                 effect
++$a       Pre-increment        Increments $a by one, then returns$a.
$a++       Post-increment       Returns $a, then increments $a by one.
�$a        Pre-decrement        Decrements $a by one, then returns $a.
$a�        Post-decrement       Returns $a, then decrements $a by one.



Incrementing and decrementing

 
<?
$birthday = $birthday + 1;
// Add another one to $birthday
++$birthday;
// Subtract 1 from $years_left
$years_left = $years_left - 1;
// Subtract another 1 from $years_left
--$years_left;
?>



Peculiarities of the ++ and -- Operators

<?
$b=true;
echo "b: $b<br>";
$b++;
echo "b: $b<br>";
?>



postfix and prefix increment operators also work on letter values

<?php
     $a = "G98";
     print("\$a = \"" . ++$a . "\"<br />");
     print("\$a = \"" . ++$a . "\"<br />");
     print("\$a = \"" . ++$a . "\"<br />");
?>



Postfix and Prefix Operators in Action

<html>
<head>
   <title>Postfix and Prefix Operators</title>
</head>
<body>
     <?php
          $a = 5;
          print("\$a = " . $a++ . "<br />");
          print("\$a = " . ++$a . "<br />");
          print("\$a = " . $a . "<br />");
          print("\$a = " . $a. "<br />");
     ?>
</body>
</html>



Postfix and Prefix Operators summary table

Operation                                                 Operator    Class     Example                
-----------------------------------------------------------------------------------------
Preincrement: 
Increment operand by one before the variable is used.     ++          Prefix    ++$a
Postincrement: 
Increment operand by one after the variable is used.      ++          Postfix   $a++
Predecrement: 
Decrement operator by one before the variable is used.    --          Prefix    --$a
Postdecrement: 
Decrement operand by one after the variable is used.      --          Postfix   $a--



Shorthand Operators summary table

Operation       Operator        Example        Expansion
Assignment      =               $a = 5         $a = 5
Addition        +=              $a += 5        $a = $a + 5
Subtraction     -=              $a -= 5        $a = $a - 5
Multiplication  *=              $a *= 5        $a = $a * 5
Division        /=              $a /= 5        $a = $a / 5
Concatenation   .=              $a .= "Add"    $a = $a . "Add"
Modulus         %=              $a %= 5        $a = $a % 5
Bitwise AND     &=              $a &= 5        $a = $a & 5
Bitwise         |=              $a |= 5        $a = $a | 5
inclusive OR
Bitwise         ^=              $a ^= 5        $a = $a ^ 5
exclusive OR (XOR)
Bitwise NOT     ~=              $a ~= 5        $a = $a ~ 5
Bitwise         <<=             $a <<= 5       $a = $a << 5
left-shift
Bitwise         >>=             $a >>= 5       $a = $a >> 5 
right-shift



Using Assignment Operators

<?
$origVar = 100;
echo "<P>Original value is $origVar</P>";
?>



Using autoincrement to add to a variable

 
<?php
$counter=1;
$counter++;
echo $counter
?>



Using pre- and postincrement

 
<?php
$test=1;
echo "Preincrement: ".(++$test);
echo "<BR>";
echo "Value afterwords: ".$test;
echo "<BR>";
$test=1;
echo "Postincrement: ".($test++);
echo "<BR>";
echo "Value afterwords: ".$test;
?>



Using the autodecrement operator

 
<?php
$counter=1;
$counter--;
echo $counter
?>



Using the negation operator

 
<?
$finished = false;
if ($finished == false) {
    print "Not done yet!";
}
if (! $finished) {
    print "Not done yet!";
}
?>



Variables Are Assigned by Value

<html>
<head>
<title>Variables are assigned by value</title>
</head>
<body>
<?php
$aVariable = 42;
$anotherVariable = $aVariable;
$aVariable = 325;
print $anotherVariable;
?>
 </body>
</html>