PHP Beginnings Ex. #4: Arithmetic-Assignment Operators and Variables
Arithmetic-assignment operators perform an arithmetic operation on the variable at the same time as assigning a new value. For this PHP exercise, write a script to reproduce the output below. Manipulate only one variable using no simple arithmetic operators to produce the values given in the statements.
Hint: In the script each statement ends with "Value is now $variable."
Value is now 8.
Add 2. Value is now 10.
Subtract 4. Value is now 6.
Multiply by 5. Value is now 30.
Divide by 3. Value is now 10.
Increment value by one. Value is now 11.
Decrement value by one. Value is now 10.
 
 
Here's the script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
<title>Arithmetic-Assignment Operators</title>
</head>
<body>
<?php
$num = 8;
echo "Value is now $num.<br/>";
$num += 2;
echo "Add 2. Value is now $num. <br/>";
$num -= 4;
echo "Subtract 4. Value is now $num. <br/>";
$num *= 5;
echo "Multiply by 5. Value is now $num. <br/>";
$num /= 3;
echo "Divide by 3. Value is now $num. <br/>";
$num++;
echo "Increment value by one. Value is now $num.<br/>";
$num--;
echo "Decrement value by one. Value is now $num.";
?>
</body>
</html>
See the output of the script in a separate window here. You can also view the output's HTML source from the new window, if you need to check that.
To open a PHP code editor in a new tab, click here.
Comments
Question.. :)
Please help me solve this..
its look kind a weird when I tried to echoing the $num like this :
note : Im using $result as $num
and the output is like this:
Value now is 8
Add 2, Value now is 10
Subtract 4, Value now is 6
Multiply by 5, Value now is 30
Divide by 3, Value now is 10
Increment by one, Value now is 10
Decrement by one, Value now is 11
Why the value in the last value (increment and decrement by one) is miscalculated?
Am I missing something?
I appreciate all of your effort for helping me,
thanks before.. :)
return value - vs - value change - operator precedence
you are actually using the direct return value from the decrement function.
if you write
echo "mytext".$result--
the correct return result would be given by
echo "mytext".(--$result)
another problem that you noticed was that the Dot (.) operator is not always processed last
so braces () become mandatory.
so i guess what you wanted to accomplish was
Increment and Decrement
In $result++, the result is first stored and then incremented and that same for $result--.
After the division of the value in $result by three, quotient is stored in $result. When $result++ is echoed it will show the value currently present there i.e.,10. and not 11 because still now it is not incremented. Later on the value in $result is incremented and that's shown while decrement operator is used. Only after that value in $result will be decremented.
If again the value of $result is echoed after that result will be 10. That's because now the $result contain the decremented value.
Whereas if ++$result or --$result is used then first the value in $result will be incremented or decremented respectively and then it will be stored in $result.
Thank you.
adding ++ before your variable
Hi, if you add your -- or ++ before your variable, it will print correct.
Search this page for:
PHP Increment / Decrement Operators
https://www.w3schools.com/PhP/php_operators.asp
My code here
using printf
switch case and loop
I went on a little far, but it was for learning purposes:)
My 9 line version
variable in a string
this was the result when
Value is now 8Add 2. Value is now 8
Subtract 4. Value is now 8
Multiply by 5. Value is now 8
Divide by 30. Value is now 8
Increment value by 1. Value is now 8
Decrement value by 1. Value is now 8
when I put a variable in a string and updated the variable
VARIABLE IN A STRING
i put a variable in a string. So if the value of variable changes shouldn't the string get updated by itself
because this code
Gave the following result
Value is now 8Add 2. Value is now 8
Subtract 4. Value is now 8
Multiply by 5. Value is now 8
Divide by 30. Value is now 8
Increment value by 1. Value is now 8
Decrement value by 1. Value is now 8
Increment and Decrement
I had some trouble by using the following statement for increment and decrement:
print "Increment value by one. Value is now " . $var++. "";
print "Decrement value by one. Value is now " . $var--. "";
So I decided to do the $varr++ and $var-- before the print. Here is my solution. 10 lines of a simple solution, no fancy arrays, no luxurious loops :)
adding ++ before your variable
Hi,
Try this one:
// if ++ comes after, it is not working!
// When -- is written after your variable, then it will work.