Mathematical Operations with Variables in PHP
Mathematical operations can be performed not only on numbers but also on variables. Let's add, for example, the values of two variables:
<?php
$a = 1;
$b = 2;
echo $a + $b; // will output 3
?>
It is not necessary to immediately output the result of the operation; you can first write it to some variable, and then output the value of that variable:
<?php
$a = 1;
$b = 2;
$c = $a + $b; // write the sum to variable $c
echo $c; // will output 3
?>
Create a variable $a
with a value of
10
and a variable $b
with a value of
2
. Output their sum, difference,
product, and quotient (result of division).
Create a variable $c
with a value of
10
and a variable $d
with a value of
5
. Sum them up, and assign the result
to variable $res
. Output the
value of variable $res
.
Create a variable $a
with a value of
1
, a variable $b
with a value of
2
, and a variable $c
with a value of
3
. Output their sum.
Create a variable $a
with a value of
10
and a variable $b
with a value of
5
. Subtract $b
from $a
and assign the result to variable
$c
. Then create a variable $d
,
assign it a value of 7
. Add variables
$c
and $d
, and write the result
into variable $res
. Output
the value of variable $res
.