Adding Two Numbers in PHP
Syntax
In PHP, you can add two numbers using the +
operator.
$sum = $num1 + $num2;
Example
Below is an example of a PHP script that adds two numbers together:
$num1 = 5;
$num2 = 3;
$sum = $num1 + $num2;
echo "The sum of $num1 and $num2 is $sum.";
Output
When running the above script, you should see the following output:
The sum of 5 and 3 is 8.
Explanation
In the above script, we first define two variables $num1
and $num2
with integer values of 5 and 3. We then add those two variables together using the +
operator and store the result in the $sum
variable. Finally, we use the echo
statement to output the sum of the two numbers in a string.
Use
Adding two numbers together is a basic mathematical operation that is used in a variety of applications, such as calculating totals, averages, and percentages.
Important Points
- In PHP, the
+
operator is used to add two numbers together. - Variables must be defined and assigned values before they can be added together.
- The
echo
statement is used to output the result of the addition as a string.
Summary
Adding two numbers in PHP is a simple process that involves using the +
operator and assigning the result to a variable. The resulting value can then be output using the echo
statement.