Visit the Official PHP Website
Search this Site

PHP Programming

By James N Hitz

Assigning Values to Variables

Assigning a Value to a variable is simple. Check this out:

$item = "Book";
$quantity = 5;
$unit_cost = 23.55;
$tax = .01;
$total = ($quantity * $unitcost) * (1 + $tax);
$pricePerItem = $total/$quantity;

Here the variables are getting their value from a direct assignment ie:

$variable = value;

or from a compound statement:

$variable = (formula) * (another formula)...

When making formulas (Expressions) we make use different PHP operators. It is wise to note that there is a well defined order of precedence. This means that some PHP operators are more Operators than others (ever read 'Animal Farm'?). Hey don't worry if some people get the VIP treatment when you don't. This behaviour extends to PHP operators too.

For the 4 most common arithmetical operators, the order is thus:

Multiplication -> Division -> Addition -> Subtraction
      *              /            +           -

This order of precedence can be over-ridden using brackets which work from inside-out. Meaning. The innermost brackets are evaluated first and then the outer brackets and eventually the outermost... and so on until all the brackets are evaluated. You must have dome mathematics in school and have probably heard of BODMAS (or BEDMAS) (We here use BOMDAS and BEMDAS. I assume you are comfortable with this. If you aren't, just Take a Peek (Opens New Window) and...

" * / + and - Are these the only Operators in PHP?"

Heck No!

<< Types of PHP Variables | PHP Operators >>