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.

Here's the script:

1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3.
4. <html xmlns="http://www.w3.org/1999/xhtml"  xml:lang="en" lang="en">
6. <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
7. <title>Arithmetic Operators</title>
9.
10. <body>
11.
12. <?php
13.
14. \$x=10;
15. \$y=7;
16.
17. \$result=\$x+\$y;
18. echo "\$x + \$y = \$result<br />";
19.
20. \$result=\$x-\$y;
21. echo "\$x - \$y = \$result<br />";
22.
23. \$result=\$x*\$y;
24. echo "\$x * \$y = \$result<br />";
25.
26. \$result=\$x/\$y;
27. echo "\$x / \$y = \$result<br />";
28.
29. \$result=\$x%\$y;
30. echo "\$x % \$y = \$result<br />";
31.
32. ?>
33.
34. </body>
35. </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.

\$result variable is used more than once for different operations

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

how did you use the \$result?

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.

Variable value is changed

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.

code for arithmetic operation

<?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 "";

?>

Can I use this code?

Arithmetic Operators and Variables

<?php

\$x=10;
\$y=7;
\$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 ";

?>

correct

It is correct, in the meanwhile you've forgot to "breake it"
echo "\$x % \$y = \$zmod ";

I've made up this one...

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] ";
}

?>

Only two variables are required if you use concatenation.

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

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.

Using Foreach.

<?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 ...

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.

I can name that tune in 2 lines of code!

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 . '';}
?>

How not to do it...

\$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} . '');");
}