PHP

 Home  Computers & Internet  Web Programming PHP
What is PHP?
Echo
Comments
Variables
Constants
Data Types
number_format()
Character Strings
Mathematical Operators
Comparison Operators
Logical Operators
Joining Strings
explode()
implode()
strtolower()
strtoupper()
strlen()
ucfirst()
ucwords()
strrev()
str_replace()
str_repeat()
trim()
strip_tags()
addslashes()
stripslashes()
strpos()
strrpos()
nl2br()
isset()
unset()
empty()
POST
GET
If Statements
If Else Statements
Elseif Statements
Switch Statements
For Loops
While Loops
Do While Loops
Foreach Loops
File Create
File Open
File Read
File Write
File Delete
fgets()
file_get_contents()
Date & Time
$_SERVER
Sessions
Cookies
Arrays

MATHEMATICAL OPERATORS IN PHP

PHP can do mathematical operations on numbers.

Example:

$total= 1 + 2;

echo $total; //outputs 3

PHP can also do mathematical operations on variables that contain numbers.

Example:

$firstNumber=5;

$total= $firstNumber + 2;

echo $total; //outputs 7

Example:

$firstNumber=5;
$secondNumber=18;
$total= $firstNumber + $secondNumber;

echo $total; //outputs 23

PHP can perform addition (as above), subtraction, division, multiplication and modulus.

SUBTRACTION:

Example:

$total= 10 - 1;

echo $total; //outputs 9

Example:

$firstNumber=5;

$total= $firstNumber - 2;

echo $total; //outputs 3

Example:

$firstNumber=5;
$secondNumber=18;
$total= $firstNumber - $secondNumber;

echo $total; //outputs -13

DIVISION:

Example:

$total= 10 / 2;

echo $total; //outputs 5

Example:

$firstNumber=6;

$total= $firstNumber / 2;

echo $total; //outputs 3

Example:

$firstNumber=20;
$secondNumber=10;

$total= $firstNumber / $secondNumber;

echo $total; //outputs 2

MULTIPLICATION:

Example:

$total= 7 * 2;

echo $total; //outputs 14

Example:

$firstNumber=6;

$total= $firstNumber * 100;

echo $total; //outputs 600

Example:

$firstNumber=20;
$secondNumber=10;

$total= $firstNumber * $secondNumber;

echo $total; //outputs 200

MODULUS:

Modulus is the remainder after a division. The operator % is used.

Example:

$total= 7 % 2;

echo $total; //outputs 1

1 is calculated as follows:

2 can go into 7 three times whole. Therefore 2 * 3 = 6. Then 7-6=1. Therefore 1 is the answer.

Home | Privacy Policy | Terms Of Use | Contact Us