📖 PHP Operators
Operators are symbols used in expressions to manipulate variables and literals. They help create more complex expressions by combining or comparing values.
For example
area = length * widthcircumference = 2 * pi * radiusamount = 500
Example
<?php
// Calculating the Area of a Rectangle
// assign values to variables
$length = 2;
$width = 4;
// perform arithmetic operation
$area = $length * $width;
// concatenate output to html
echo "<p>The area of the rectangle is " . $area . ".</p>";
?>
The example code above would output the following HTML.
<p>The area of the rectangle is 8.</p>
Operators
Arithmetic
[ + ] Addition
[ - ] Subtraction/Negation
[ * ] Multiplication
[ / ] Division
[ % ] Modulus
[ ++ ] Increment (Prefix/Postfix)
[ -- ] Decrement (Prefix/Postfix)
Assignment
[ = ] Assigns value to a variable
[ += ] Adds and assigns value
[ -= ] Subtracts and assigns value
[ *= ] Multiplies and assigns value
[ /= ] Divides and assigns value
[ %= ] Modulus and assigns remainde
Comparison
[ == ] Equal
[ != ] Not equal
[ > ] Greater than
[ < ] Less than
[ >= ] Greater than or equal to
[ <= ] Less than or equal to
Logical
[ && ] AND
[ || ] OR
[ ! ] NOT
String
[ . ] Concatenation
[ .= ] Concatenation and assignment
Example
<?php
// assignment operator
$myNumber = 24;
// arithmetic operator
$myMultiplication = 5 * 7;
$myAddition = 5 + $myNumber;
// comparison operator
$isGreater = $myAddition >= 10;
// logical operator
$isTrue = $myAddition && $myNumber;
// concatenation operator
$myColor = "Blue";
$myPoem = "Little boy " . $myColor . " come blow your horn.";
/* comparison and logical operators are generally used in expressions */
?>
Operator Precedence
Operator precedence defines the order in which a series of calculations are executed. If an expression contains multiple operations at the same level, they are executed from left to right.
- Operations enclosed in parentheses
- Logical NOT (
!), Increment (++), and Decrement (--) - Multiplication (
*), Division (/), Modulus (%) - Addition (
+), Subtraction (-), Concatenation (.)
Formatting Numbers
PHP provides built-in functions for formatting numbers.
round(number, decimal_places)
- Rounds the number to the specified decimal places.
- Number can be a literal or a variable.
- If decimal places are omitted, 0 is assumed.
number_format(number, decimal_places)
- Similar to
round, but adds commas for thousands. - If decimal places are omitted, 0 is assumed.
Example
<?php
// Formatting a Product Invoice
// assign values to variables
$price = 2.99;
$quantity = 2;
$taxRate = 0.08;
// perform arithmetic operations
$subTotal = $price * $quantity;
$tax = $subTotal * $taxRate;
$tax = number_format($tax, 2); // format tax to 2 decimal places
$total = $subTotal + $tax;
// format output to HTML
echo '<table>
<caption>Product Invoice</caption>
<thead>
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widgets</td>
<td style="text-align: center;">' . $quantity . '</td>
<td style="text-align: center;">$' . $price . '</td>
<td style="text-align: center;">$' . $subTotal . '</td>
</tr>
<tr>
<td>Tax</td>
<td style="text-align: center;"> </td>
<td style="text-align: center;">' . $taxRate . '</td>
<td style="text-align: center;">$' . $tax . '</td>
</tr>
<tr>
<td>Total</td>
<td style="text-align: center;"> </td>
<td style="text-align: center;"> </td>
<td style="text-align: center;">$' . $total . '</td>
</tr>
</tbody>
</table>';
?>
HTML output
| Description | Quantity | Unit Price | Cost |
|---|---|---|---|
| Widgets | 2 | $2.99 | $5.98 |
| Tax | .08 | $.48 | |
| Total | $6.46 |