Your Ad Here

 Navigation
Home
Source code
Tutorials
RSS feeds
Articles
Wordpress plugins
Books
Sample Chapters
Software
Downloads
Hosting
manuals
Script directory
Training
Our Links




 Sponsors
 Links
gambling sites
wwwstores
golfing products
 Downloads
dzphp editor
EasyPHP
mysql_phpgenerator
php-studio-trial
phpdesigner_7_2_5
phpMyAdmin-3.4.3.2
/phptriad2-2-1
phpxedit_321
rapidphp 2011
WampServer2.1d-x64
WampServer2.1e-x32
xampp-win32-1.7.4-VC6-installer
xampplite-win32-1.7.3
 Misc
Webmaster Resources
Only PHP
ScriptSearch.com
Scripts.com - Get the best scripts NOW!
AndreaPHP Programming

Valid XHTML 1.0 Transitional


Valid CSS!


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
Short urls with Bit.ly(internet)
Better image resizing function(image)
list all databases(database)
 Random Article
  Network
Programming resources
Tutorials directory
Hosting resources
ASP site
Domain names
Progged
Maxi directory
bigarticle : free articles
A Code
Code N Tutorials
Get PHP
Programmers help




beginners PHP Copyright © 2004 onwards by beginnersPHP.