Arithmetic Operators

 

Operator

Name

Example

+

Addition

$a + $b

-

Subtraction

$a - $b

*

Multiplication

$a * $b

/

Division

$a / $b

%

Modulus

$a % $b

 

These are the standard arithmetic operators. Note that the modulus is the remainder of division operation, e.g.

27 # 10 = the remainder of 27 divided by 10, or 7 .

 

String Operators

There is only one string operator, the period or full stop. It joins together two strings into one combined string. e.g.

$a = 'Hi ';
$b = 'There';
$result = $a.$b

$result will contain 'Hi There'

 

Assignment Operator

The assignment operator (=) assigns values to variables. It should be read ‘Is set to' e.g.

$a = 5; ‘the variable $a is set to five'.

Note this is not equals, even though it uses the equals sign from mathematics.

 

Combination Assignment Operators

These are shorthand ways of using the arithmetic and string operators.

 

Operator

Use

Equivalent To

+=

$a += $b

$a = $a + $b

-=

$a -= $b

$a = $a - $b

*=

$a *= $b

$a = $a * $b

/=

$a /= $b

$a = $a / $b

%=

$a %= $b

$a = $a % $b

.=

$a .= $b

$a = $a . $b

 

Pre and Post Increment Operators

These operators (++ and --) either add or subtract one from a variable. They are often used for stepping through loops. e.g.

$a = 8;

echo ++Sa;

This will return 9 as the value of $a. This is called a pre-increment as the value is changed before any statements are executed. If you put the ++ after the variable it becomes a post-increment operator and does its job after the lines of code have been executed. e.g.

$a = 8;

echo Sa++;

Will display a value of 8 from the echo statement, and then $a will be changed to 9.

 

Comparison Operators

Comparison operators test a statement and return true or false. They are usually used to control what code gets executed. You use a comparison operator in a conditional statement; 'if this statement is true, then execute this block of code' (See Control Structures).

The main comparison operator is equals (==) note that it has two equals sign to differentiate it from the assignment operator.

The other Comparison operators are;

 

Operator

Name

USe

==

Equals

$a = $a + $b

===

Identical

$a = $a - $b

!=

Not equal

$a = $a * $b

!==

Not identical

$a = $a / $b

<>

Not equal

$a = $a % $b

<

Less than

$a = $a . $b

>

Greater than

$a = $a . $b

<=

Less than or equal to

$a = $a . $b

>=

Greater than or equal to

$a = $a . $b

 

Note; the identical (===) operator returns true if two variables have the same value and are of the same type.

 

Logical Operators

Logical operators combine the result of logical conditions; e.g.

$examMark >= 50 && $examMark <= 70

will test to see if the variable is greater than or equal to 50, and less than or equal to 70.

 

Operator (Name)

Use

Result

! (NOT)

!$a

Returns true if $a is false and vice versa

&& (AND)

$a && $b

Returns true if $a and $b are true

|| (OR)

$a || $b

Returns true if either $a and $b, or both are true

and

$a and $b

Similar to && but with lower precedence

or

$a 0r $b

Similar to || but with lower precedence

 

Using Operators to Work Out Form Totals

Add the following code to the bottom of the PHP script; (just below the line define('pr_price', 112);


$total_qty = 0;
$total_qty = $hd_qty + $mon_qty + $pr_qty;
echo 'Number of items ordered: '.$total_qty.'<br />';
$total_amount = ($hd_qty * hd_price) + ($mon_qty * mon_price) + ($pr_qty * pr_price);
echo 'Subtotal: $'.number_format($total_amount,2).'<br />';
$tax_rate = 0.10; //GST = 10%
$total_amount = $total_amount * (1 + $tax_rate);
echo 'Total including tax: $'.number_format($total_amount,2).'<br />';

 

First we add up the total number of items ordered by simply adding the numbers entered into the textfields of the order form. Next we calculate the price using the constant we declared earlier. Then we add 10% to the total to account for our tax, and we then have a final total.