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:
//this will print out the result print 5 2; print '
'; //this example uses 1 variable $var1 = 10; //var1 is assigned with the value of 10 print $var1-7; //will substract 7 from var1 print '
'; //this example uses 2 variables $var1 = 8; $var2 = 2; print $var1*$var2; //will multiple var1 and var2 together print '
'; //this example shows the divide operator $var1 = 8; $var2 = 2; print $var1/$var2; print '