Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

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.

 

Comments

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

echo 'Value now is '. $result = 8 . '<br/>';
echo 'Add 2, Value now is '. $result +=2; echo '<br/>';
echo 'Subtract 4, Value now is '. $result -= 4; echo '<br/>';
echo 'Multiply by 5, Value now is '. $result *= 5; echo '<br/>';
echo 'Divide by 3, Value now is '. $result /= 3; echo '<br/>';
echo 'Increment by one, Value now is '. $result++ ;echo '<br/>';
echo 'Decrement by one, Value now is '. $result-- ; echo '<br/>';

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.. :)

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

echo 'Value now is '.($result = 8).'';
echo 'Add 2, Value now is '.($result +=2).'';
echo 'Subtract 4, Value now is '.($result -= 4).'';
echo 'Multiply by 5, Value now is '.($result *= 5).'';
echo 'Divide by 3, Value now is '.($result /= 3).'';
echo 'Increment by one, Value now is '.(++$result).'';
echo 'Decrement by one, Value now is '.(--$result).'';

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.

$variable = 8;
echo "Value is now ".$variable."<br />";

$variable += 2;
echo "Add 2. Value is now ".$variable."<br />";

$variable -= 4;
echo "Subtract 4. Value is now ".$variable."<br />";

$variable *= 5;
echo "Multiply by 5. Value is now ".$variable."<br />";

$variable /= 3;
echo "Divide by 3. Value is now ".$variable."<br />";

$variable++;
echo "Increment value by 1. Value is now ".$variable."<br />";

$variable--;
echo "Decrement value by 1. Value is now ".$variable."<br />";

$x = 0;
	$x += 8;
	printf ("Value is : %d<br/>",$x);
	$x +=2;
	printf ("Value is : %d<br/>",$x);
	$x -=4;
	printf ("Value is : %d<br/>",$x);
	$x *=5;
	printf ("Value is : %d<br/>",$x);
	$x /=3;
	printf ("Value is : %d<br/>",$x);
	$x++;
	printf ("Value is : %d<br/>",$x);
	$x--;
	printf ("Value is : %d<br/>",$x);

<?php

$base = 8;

echo "value is now ".$base."<br>"; 

for($i=0; $i<6; $i++)
{
	
	switch ($i)
	{
	
		case 0;
			
			$base = $base + 2;
			
			echo "Add 2. value is now ".$base."<br>";
						
		break;
		
		case 1;
		
			$base = $base - 4;
			echo "Subtract 4. Value is now ".$base."<br>";
		
		break;
		
		case 2;
			
			$base = $base * 5;
			echo "Multiply 5. Value is now ".$base."<br>";
		
		break;
		
		case 3;
		
			$base = $base / 3;
			echo "Divide by 3. Value is now ".$base."<br>"; 
			
		break;
		
		case 4;
		
			$base = ++$base;
			echo "Increment Value by one. Value is now ".$base."<br>";
			
		break;
		
		case 5;
		
			$base = --$base;
			echo "Decrement value by one. Value is now ".$base."<br>";
			
		break;
	}
}
?>

 <?php
  $commands = array(
	'add' => "+",
	'substract' => "-",
	'multiply' => "*",
	'divide' => "/",
	'increment' => "+",
	'decrement' => "-");

  $thevalue = 8;

  function docommand($command) {
	global $commands, $thevalue;
	$parse_command = explode(' ', $command);
        if ($commands[@strtolower($parse_command[0])] != NULL) {
		$dowith = strtolower($parse_command[0]);
		$value = $parse_command[count($parse_command)-1];
		$result = "$command. " . eval("\$thevalue = $thevalue $commands[$dowith] $value;") . "Value is now $thevalue.";
		echo "$result<br>";
	} else {
		echo "Invalid command: $parse_command[0]<br>";
	}
  }
  
  echo "Value is now $thevalue. <br>";
  docommand("Add 2");
  docommand("Increment value by 6");
  docommand("Decrement value by 8");
  docommand("Multilpy value by 10");
  docommand("Divide by 5");
  docommand("Multiply this $thevalue with $thevalue");
 ?>


$s= "Value is now ";
$v = 0;
echo  $s . ($v+=8) .'.';
echo "\nAdd2. " . $s . ($v+=2) .'.';
echo "\nSubtract 4. " . $s . ($v-=4) .'.'; 
echo "\nMultiply by 5. " . $s . ($v*=5) .'.';
echo "\nDivide by 3. " . $s . ($v/=3) .'.';
echo "\nIncrement value by one. " . $s . ++$v .'.';
echo "\nDecrement value by one. " . $s . --$v .'.';

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

<?php 
$variable = 8;
$a = "Value is now $variable";
echo " $a";
$variable += 2 ;
echo "Add 2. $a". "<br />";
$variable -= 4 ;
echo " Subtract 4. $a" . "<br />";
$variable *= 5 ;
echo " Multiply by 5. $a" . "<br />";
$variable /= 30 ;
echo " Divide by 30. $a" . "<br />";
$variable += 1 ;
echo " Increment value by 1. $a" . "<br />";
$variable -= 1 ;
echo " Decrement value by 1. $a" . "<br />";
?>

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

<?php 
$variable = 8;
$a = "Value is now $variable";
echo " $a";
$variable += 2 ;
echo "Add 2. $a". "<br />";
$variable -= 4 ;
echo " Subtract 4. $a" . "<br />";
$variable *= 5 ;
echo " Multiply by 5. $a" . "<br />";
$variable /= 30 ;
echo " Divide by 30. $a" . "<br />";
$variable += 1 ;
echo " Increment value by 1. $a" . "<br />";
$variable -= 1 ;
echo " Decrement value by 1. $a" . "<br />";
?>

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

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 :)

            $var=8;
            print "Value is now " . $var . "<br>";
            print "Add 2. Value is now " . ($var+=2) . "<br>";
            print "Substract 4. Value is now " . ($var-=4) . "<br>";
            print "Multiply by 5. Value is now " . ($var*=5) . "<br>";
            print "Divide by 3. Value is now " . ($var/=3) . "<br>";
            $var++;
            print "Increment value by one. Value is now " . $var . "<br>";
            $var--;
            print "Decrement value by one. Value is now " . $var . "<br>";