Your Ad Here

 Navigation

Home
Source code
Tutorials
RSS feeds
Articles
Books
Software
Downloads
Hosting
manuals
Forums
Script directory
Training
Our Links

 Sponsors
 Links
programmershelp
dvd information
software directory
 Downloads
 Misc

Webmaster Resources
Only PHP
ScriptSearch.com
Scripts.com - Get the best scripts NOW!
AndreaPHP Programming



Basic Math operators in PHP


Basic Math Operators

The basic math operators in PHP should be familiar to anyone with programming experience . The set of mathematical operators available are listed below.
Operator Description
+ The addition operator
- The subtraction operator , can also be used for negation like this -9
* The multiplication operator
/ the division operator
% the modulus operator , returns the remainder after division . For example 25 % 3 would give us 1 .

Here is an example

<?php
$number1 = 9;
$number2 = 4;
echo $number1 + $number2;
?>

Which produces the following
13

Incrementing

To increment a variable in PHP we do the following

$yearsindebt++;

This increments the $yearsindebt variable by one , we can also do the following .

$mybill += 20;

which is the same as saying $mybill = $mybill + 20;

Order Of Precedence

The math operators all have to obey an order of precedence which is used in mathematics . This often catches out beginners . Lets look at an example

$calc = 25 + 4 * 2;

What do you think the value of $calc is , well you could add 25 + 4 and get 29 then multiply by 2 and get 58 . Wrong . The rules of precedence say that multiplication gets calculated before addition so you get 4 * 2 is 8 and then 25 + 8 = 33 . 33 is the correct answer . A big difference from 58 .

At school I was taught to use BODMAS to help remember order of precedence this is basically Brackets , Division , Multiplication , Addition , Subtraction. What if you want to add the 25 + 4 first and then multiply this by 2 , well you can do the following.

$calc = (25 + 4) * 2;

Now the brackets get evaluated first because they come before multiplication in the order of precedence..

 




Books
 Sponsors
 Random Code
download a web page to a file(file system)

For loop(beginner)

display a random entry from aMySQL database(database)

 Random Article
  Network

Programming resources
Tutorials directory
General bid directory
sell software, make money
Find me wholesalers
Find me dropshippers
Free recipes online/a>
UK products and price comparison
Anime videos
Free lyrics search
free stuff
UK stores
Gambling directory
Sexy free wallpapers
Hosting resources
ASP site
Golf resources
iPod resources
Coupons and deals
Baby names
Domain names
Dating site
Scripts directory
Maxi directory
bigarticle : free articles
dawgwitch search
List of directories






beginners PHP Copyright © 2004 onwards by beginnersPHP.