Follow:  Follow PHPExercises on Twitter  Follow us on Facebook

PHP Beginnings Ex. #3: Arithmetic Operators and Variables

PHP includes all the standard arithmetic operators. For this PHP exercise, you will use them along with variables to print equations to the browser. In your script, create the following variables:
$x=10;
$y=7;

Write code to print out the following:

10 + 7 = 17
10 - 7 = 3
10 * 7 = 70
10 / 7 = 1.4285714285714
10 % 7 = 3

Use numbers only in the above variable assignments, not in the echo statements. You will need a third variable as well.

Note: this is intended as a simple, beginning exercise, not using arrays or loops. Some of the solutions in comments include these structures. If you don't understand them, just keep learning, and you will.

Before you consider entering your answer into the comments, please read Commenting Guidelines. Comments that simply present the answer code will not be approved.

 

Comments

how it was executed you have put the same $result option for different operators how apache server got all the stuff
please explain
madhu

the $result was use multiple times with different values in a single php file how comes that there was no conflict? otherwise your code is wrong aswel.

The value of the variable is changed between each echo statement. That's how variables work, they change as you assign new values. View the result here: http://phpexercises.com/answers/BegScript3.php. That is output from the live script.


<?php 
$a=7;
$b= 10;
echo "$a+$b="; echo $a+$b; echo "";
echo "$a-$b="; echo $a-$b; echo "";
echo "$a*$b="; echo $a*$b; echo "";
echo "$a/$b="; echo $a/$b; echo "";

?>


Arithmetic Operators and Variables




<?php


$x=10;
$y=7;
$zadd= $x+$y;
$zminus= $x-$y;
$zmul= $x*$y;
$zdiv= $x/$y;
$zmod= $x%$y;

echo "$x + $y = $zadd ";
echo "$x - $y = $zminus ";
echo "$x * $y = $zmul ";
echo "$x / $y = $zdiv ";
echo "$x % $y = $zmod ";

?>



Is this correct? how could it be optimized?


<?php 
$x = 10;
$y = 7;
$operators = array('+','-','*','/');
$operations = array($x+$y,$x-$y,$x*$y,$x/$y);

for($count = 0; $count<=3; ++$count){
	$action = $operators[$count];
	echo "$x $action $y = $operations[$count] ";
}

?>

A simpler, cleaner way to solve this problem.

<?php 		
$x = 10;
$y = 7;
		
echo "$x + $y = ".($x+$y)."";
echo "$x - $y = ".($x-$y)."";
echo "$x * $y = ".($x*$y)."";
echo "$x / $y = ".($x/$y)."";
echo "$x % $y = ".($x%$y)."";
		
?>

Must admit I had a bit of a "clever girl" moment (a la jurassic park) when I saw this
that's the best one here.
I made same as the site recommends because I thought I have to use another variable. but your one is better.


	<?php 
		$x = 10;
		$y = 7;

		$op = array('+' => $x+$y , 
				    '-' => $x-$y , 
				    '*' => $x*$y ,
					'/' => $x/$y ,
					'%' => $x%$y);

		foreach ($op as $operator => $operation) {
			echo "$a $operator $b = ".$operation."";
		}
	 ?>

Brilliant but wrong.
Well, just a little.

$a $operator $b should be: $x $operator $y.
that is why I always test :P.

I made a little variation on your solution.
The following code comes after your array:

    $output = '';
    foreach($op as $operator => $operation)
        {
            $output .= $x;
            $output .= $operator ;
            $output .= $y;
            $output .= '=' .$operation .'';
        }
    echo $output;

Nice and clean for MCV model, als you would only have to place <?php echo $output ?> in the View.

Here is my overcomplicated solution that does the job in the fewest lines of code.


<?php
for($i=0,$x=10,$y=7,$problems=array('+','-','*','/','%'); $i<count($problems); $i++){
   echo "$x $problems[$i] $y" . " = " . eval("\$z = $x $problems[$i] $y;"). $z . '';}
 ?>



$x=10;
$y=7;
$ops=array('+','-','*','/','%');
foreach($ops as $op) {
  $result = eval('return '.$x.$op.$y . ';');
  echo $x . ' ' . $op .' ' . $y . ' = ' .$result . "";
}

Rolling that echo into the eval code

$x=10;
$y=7;
$ops=array('+','-','*','/','%');
foreach($ops as $op) {
  eval("echo '{$x} {$op} {$y} = ' . ({$x}{$op}{$y} . '');");
}