PHP Operators: Maths

There are various kinds of operators used in PHP, but we’re going to start with the most basic and commonly known operator - the Math operators.

What are operators? They are basically a special set of characters (or symbols, if you like) which tells PHP to perform a certain operation. For example, the ‘+’ (plus sign) is used for arithmetic adding of 2 values or variables.

Now you know why I’m starting with the Math operators, cos anyone who has had any basis schooling in Mathematics will be familiar with the Math operators.


These are the Math operators:

+ (plus sign) adds two numbers together
- (minus sign) subtracts one number from another
* (asterisk sign) multiplies two numbers together
/ (forward slash) divides one number by another
% (percent sign) returns the remainder when one number is divided by another - commonly referred to as the modulus.

Here are some examples of how to use these operators:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//this will print out the result
print 5  2; 
print '<br>';
 
//this example uses 1 variable
$var1 = 10; //var1 is assigned with the value of 10
print $var1-7; //will substract 7 from var1
print '<br>';
 
//this example uses 2 variables
$var1 = 8;
$var2 = 2;
print $var1*$var2; //will multiple var1 and var2 together
print '<br>';
 
//this example shows the divide operator
$var1 = 8;
$var2 = 2;
print $var1/$var2;
print '<br>';
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
Other similar posts:


Leave a Reply

Google