PHP/Operator/Arithmetic Operators
Содержание
- 1 An example of PHP"s automatic type conversion is the addition operator "+".
- 2 Arithmetical Operators
- 3 Arithmetic Operators
- 4 Calculation Results for Postfix and Prefix Operators
- 5 Changing the default precedence using parentheses
- 6 Increment/decrement Operators
- 7 Incrementing and decrementing
- 8 Peculiarities of the ++ and -- Operators
- 9 postfix and prefix increment operators also work on letter values
- 10 Postfix and Prefix Operators in Action
- 11 Postfix and Prefix Operators summary table
- 12 Shorthand Operators summary table
- 13 Using Assignment Operators
- 14 Using autoincrement to add to a variable
- 15 Using pre- and postincrement
- 16 Using the autodecrement operator
- 17 Using the negation operator
- 18 Variables Are Assigned by Value
An example of PHP"s automatic type conversion is the addition operator "+".
<source lang="html4strict">
<? $foo = "0"; $foo++; $foo += 1; $foo = $foo + 1.3; $foo = 5 + "10 L"; $foo = 5 + "10 S"; ?>
</source>
Arithmetical Operators
<source lang="html4strict">
<?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
"; $result .= "addstr:$addstr
"; $result .= "sub:$sub
"; $result .= "mul:$mul
"; $result .= "mod:$mod
"; $result .= "inc:$inc
"; $result .= "dec:$dec
";
?> <html>
<head> <title>Arithmetical Operators</title> </head> <body>
<?php echo($result); ?>
</body>
</html>
</source>
Arithmetic Operators
<source lang="html4strict">
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
</source>
Calculation Results for Postfix and Prefix Operators
<source lang="html4strict">
<?
$count = 0;
$result = $count++;
print("Post ++: count is $count, result is $result
");
$count = 0;
$result = ++$count;
print("Pre ++: count is $count, result is $result
");
$count = 0;
$result = $count--;
print("Post --: count is $count, result is $result
");
$count = 0;
$result = --$count;
print("Pre --: count is $count, result is $result
");
?>
</source>
Changing the default precedence using parentheses
<source lang="html4strict">
<? echo 2 * 3 + 4 + 1; echo 2 * (3 + 4 + 1); ?>
</source>
Increment/decrement Operators
<source lang="html4strict">
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.
</source>
Incrementing and decrementing
<source lang="html4strict">
<? $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; ?>
</source>
Peculiarities of the ++ and -- Operators
<source lang="html4strict">
<?
$b=true;
echo "b: $b
";
$b++;
echo "b: $b
";
?>
</source>
postfix and prefix increment operators also work on letter values
<source lang="html4strict">
<?php
$a = "G98"; print("\$a = \"" . ++$a . "\"
"); print("\$a = \"" . ++$a . "\"
"); print("\$a = \"" . ++$a . "\"
");
?>
</source>
Postfix and Prefix Operators in Action
<source lang="html4strict">
<html> <head>
<title>Postfix and Prefix Operators</title>
</head> <body>
<?php $a = 5; print("\$a = " . $a++ . "
"); print("\$a = " . ++$a . "
"); print("\$a = " . $a . "
"); print("\$a = " . $a. "
"); ?>
</body> </html>
</source>
Postfix and Prefix Operators summary table
<source lang="html4strict">
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--
</source>
Shorthand Operators summary table
<source lang="html4strict">
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
</source>
Using Assignment Operators
<source lang="html4strict">
<? $origVar = 100;
echo "Original value is $origVar
";?>
</source>
Using autoincrement to add to a variable
<source lang="html4strict">
<?php $counter=1; $counter++; echo $counter ?>
</source>
Using pre- and postincrement
<source lang="html4strict">
<?php
$test=1;
echo "Preincrement: ".(++$test);
echo "
";
echo "Value afterwords: ".$test;
echo "
";
$test=1;
echo "Postincrement: ".($test++);
echo "
";
echo "Value afterwords: ".$test;
?>
</source>
Using the autodecrement operator
<source lang="html4strict">
<?php $counter=1; $counter--; echo $counter ?>
</source>
Using the negation operator
<source lang="html4strict">
<? $finished = false; if ($finished == false) {
print "Not done yet!";
} if (! $finished) {
print "Not done yet!";
} ?>
</source>
Variables Are Assigned by Value
<source lang="html4strict">
<html> <head> <title>Variables are assigned by value</title> </head> <body> <?php $aVariable = 42; $anotherVariable = $aVariable; $aVariable = 325; print $anotherVariable; ?>
</body>
</html>
</source>