PHP Lesson 7 – calculations (add, sqrt(), pow(), sub, mul) with variables

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

PHP Lesson 7 - calculations (add, sqrt(), pow(), sub, mul) with variables - PHP Lesson 7 - calculations (add, sqrt(), pow(), sub, mul) with variables - PHP Lesson 7 - calculations (add, sqrt(), pow(), sub, mul) with variables - how to install php script
In addition to texts, as shown in the first part, you can also store numbers in variables and calculate with them, for example, add, subtract or multiply them.

PHP Variable Basics

If one assigns a number to a variable, in contrast to the text here, no quotation marks may be used. You write the number directly into your code.

<?php 
$number1 = 10;
$number2 = 5; 
echo "number1: $number1 <br />";
echo "Number2: $number2"; 
?>

The variables 10 and are now stored in these variables.

The adding is done by +. In the following script you define two numbers and store the sum of the two numbers in the variable $result:

<?php 
$number1 = 10; 
$number2 = 5; 
$result = $number1 + $number2; 
echo "Result: $result"; 
?>

For the calculation, the numbers do not necessarily have to be stored in variables. You can also write these directly into the mathematical expression. Subsequently, we store a number in the variable $ num and add the value 5 to it.

<?php
$number = 1;
$result = $number + 5;
echo $result;
?>

Here are some more examples:

<?php
$number1 = 10;
$number2 = 5;
echo $number1 + $number2; //add
echo "<br />";
echo $number1 - $number2; //subtract
echo "<br />";
echo $number1 * $number2; //multiply
echo "<br />";
echo $number1 / $number2; //to divide
echo "<br />";
echo pow($number1, $number2); // number1 up number2 (10 ^ 5)
echo "<br />";
echo sqrt(64); // root of 64
echo "<br />";
echo 2 * $number1 + 5 * $number2 - 3; //calculate 2 * 10 + 5 * 5 - 3
?>

Of course, these results can also be saved again in extra variables.

Decrement and increment in PHP

To increase the value of a variable by 1 or reduce it by 1, there is a simple trick:

<?php 
$increase = 1; 
$increasw++;
echo $increase;
 ?>

You can also lower a number by – by the value 1:

<?php
$lower = 2;
$lower--;
echo $lower;
?>

If a double plus sign in the form of ++ is appended to the variable name (a number variable), the content of this variable, ie the number, is incremented, ie increased by 1. On the other hand, if is used, the value is decremented, that is, decreased by 1.

Short notation for assignments

The following script increases the contents of the variable by the value 10:

<?Php
$number = 1;
$number = $number + 10;
echo $number;
?>

Since programmers tend to be lazy, PHP has a shorthand notation that does exactly the same:

<? Php
$number = 1;
$number += 10;
echo $number;
?>

+ = Increases the contents of the variable $ num by the value 10. Of course you can also write – =, * = or / = to subtract, multiply or divide values.

Floating point numbers – php

In PHP, you can also define comma numbers. It should be noted that here the English spelling is used, ie instead of a comma as in German a point is used. But the mathematical operations remain the same.

<?php 
$number1 = 2.5; 
$number2 = 5.5;
$result = $number1 * $number2; 
echo "Result: $result"; 
?>

 

 

About The Author

Related posts

Leave a Reply