#2 Arithmetic operations with PHP variables

With this second part of the php course, I am going to explain the main arithmetic operations available from php. In this tutorial I will use, for greater clarity, basic programming examples, some with primitive values ​​and others also using variables, explaining them in each case in detail, line by line.

This tutorial follows the first post of the php course on variables.


  • What arithmetic operations exist in php
  • Example perform operations in php
  • Arithmetic operations in php with variables
  • Extra: Operations with text-type php variables
  • End php arithmetic operations

What arithmetic operations exist in php

As in any other programming language, mathematics plays a fundamental role when programming, and in php, as it could not be otherwise, there is the possibility of performing most of the known basic arithmetic operations:

  • Sum, the sum in php is done with the symbol +.
  • Subtraction, for the subtraction we will use the symbol .
  • Division, division in php uses the / symbol.
  • Multiplication, use the symbol *.
  • Module, or also known as the remainder of a division will use the symbol %.
Addition +
Division /
Multiplication *
Module %

Example perform operations in php

Next I will leave a simple example of each operation, indicating what it would show on the screen if we execute the script:

$var1 = 1 + 2 + 4;
echo $var1;

In the first line of this script I create a variable $var1 and I assign the value of the result of the sum 1 + 2 + 4. He threw out variable $var1 would display on the screen 7.

This previous example would be applied to any of the previous operations, we should only change the symbol of the addition operation for the one that corresponds to the table. You could also mix without any kind of limit other arithmetic operations in the same line of code, in addition, you could add parentheses just like in any mathematical operation on paper.

$result = 1 + 2 + ( 3 * 4 / 7) – ( 43 % 3 );
echo $result;

As you can see in this script the similarity with the mathematics of a lifetime is clear. Assign to variable $result the value of the operation, which will follow the order of preference of operations and parentheses symbols. For example, as you know, in mathematics, division and multiplication take precedence, and the calculations between parentheses are always solved first.

Giving it one more twist, I am going to perform the calculation of an operation in the following script at the same time that I show it on the screen:

echo ( 1 + 2 + ( 3 – 2 ) * 5 ) ;

The text displayed on the screen will be 8.

Arithmetic operations in php with variables

Now I am going to perform a couple of examples of arithmetic operations between variables:

$a = 1;
$b = 3;
$multi = $a * $b;
echo ‘Result of multiplication is: ‘ . $multi;

This time I have performed the operation with two variables, $a and $btherefore, the value of the variable $multi will be the multiplication of the values ​​of $a and $bwhat is 3. Finally I have made the concatenation (one text plus another) of the text Result of multiplication and the value of $multi (3). The result displayed on the screen will be: Result of multiplication is: 3.

A more complex example so that you can imagine the possibilities of this use of variables would be:

$a = 23;
$b = 33;
$var3 = 10;
$result = $a + $b + $var3 * ( $a – $b);

Extra: Operations with text-type php variables

Although they are not normal arithmetic operations, when we use text variables in php, the need to add one text plus another arises naturally, thus forming a longer text (also called a string). How can we chain (concatenate) two texts? Easy, with the period symbol ( . ). An example would be:

$text1 = ‘Hello ‘;
$text_concatenated = $text1 . ‘ world’;
echo $concatenated_text . ‘ from srcodigofuente.com ‘ . ‘!!’;

In this example I have created the variable $text1 with the text Hello and a blank space (you can see it before the closing quote). Next I created a variable $concatenated_text which, as its name indicates, contains the sum (concatenation) of the value of the variable $text1 and the text blank space ( just after the quotation mark that opens the text ) and the word world. Thus, with all of the above, when I perform the echo (function that displays on the screen), I have once again concatenated the value of the variable $concatenated_text, the text from srcodigofuente.com and at the end the signs !!. The full result would be:

Hello world from srcodigofuente.com !!

Finally, some concepts about the use of texts in php:

  • Concatenate is the action of adding two texts, that is, joining the value of variables or characters between quotes. S
  • It is usually called a text string or in English String to the representation in the programming of consecutive characters, hence the string.
  • In php the text strings are represented between quotes, whether they are double or single.

End php arithmetic operations

To say goodbye, I hope this php course tutorial has been useful to you! If you have any questions or notice a problem, do not hesitate to contact me. I’ll wait for you in the next class of the course: learn to use conditionals in php.

Leave a Reply