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:
<!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 Operators</title>
</head>
<body>
<?php
$x=10;
$y=7;
$result=$x+$y;
echo "$x + $y = $result<br />";
$result=$x-$y;
echo "$x - $y = $result<br />";
$result=$x*$y;
echo "$x * $y = $result<br />";
$result=$x/$y;
echo "$x / $y = $result<br />";
$result=$x%$y;
echo "$x % $y = $result<br />";
?>
</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
$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
please explain
madhu
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
Can I use this code?
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?
Only two variables are required if you use concatenation.
A simpler, cleaner way to solve this problem.
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.
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:
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.
How not to do it...
Rolling that echo into the eval code