Forming Expressions Using Operators in PHP
![]() |
Expressions are elements that can be evaluated to produce some value during the program execution. The simplest expressions are literals and variables.
A literal evaluates to itself, while a variable
evaluates to the value stored in the variable.More complex expressions can be formed using simple expressions and
operators. An operator is a symbol used to specify the operation to
be performed on operands. Some operators modify their operands,
while most do not.
PHP borrows most of its operands from C and Perl. Most operators in PHP are binary operators; they combine two operands (or expressions) into a single, more complex expression. PHP also supports a number of unary operators and a single ternary operator that combines three expressions into a single expression.
Operator Precedence & Associativity:
The order in which operators in an expression are evaluated
depends on their relative precedence. For e.g., among arithmetic
operators, the operators * and / are in the higher precedence
(evaluated first). And the operators + and – are having lower
precedence (evaluated last).
To force a particular order, we can group operands with the
appropriate operators in parenthesis. Operators placed within
parenthesis are evaluated first (i.e., they get the highest priority
to be executed first). But, within parenthesis, they are evaluated
in the order of their precedence. For example, the expression:
(2 + 4 * 3) / 2
evaluates the expression to 7 – multiplication first, addition next
(with in parenthesis) and then divide the result by 2. This is
because of the highest priority given to the expression within
parenthesis. The remaining operators, which are placed outside the
parenthesis, are evaluated in their order of priority.
Associativity defines the order in which operators with the
same order of precedence are evaluated. For example, the expression
(2 / 2 * 2) is evaluated as:
(2 / 2) * 2 = 2
and not as: 2 / (2 * 2) = 0.5
Here, the associativity is left to right; that means, in case of ambiguity (expression having operators with same precedence), the operators are evaluated from left to right.
Implicit Casting:
Many operators in PHP require that both their operands are of same
type. PHP variables can store values of any data type, which include
integers, floating-point numbers, strings and more. The conversion
of a value from one type to another is called casting.
PHP converts values from one type to another as necessary. This kind of casting (automatic conversion) is called implicit casting or type juggling in PHP. The rules for type juggling done by arithmetic operators are shown in the below table:
Type of First Operand
|
Types of Second Operand
|
Conversion Performed
|
Integer
|
Floating-point number
|
The integer is converted to a floating-point number
|
Integer
|
String
|
The string is converted to a number; if the value after conversion
is a floating-point number, the integer is converted to a
floating-point number.
|
Floating-point number
|
String
|
The string is converted to a floating-point number
|
Table: Implicit Casting rules for Binary Arithmetic Operators
We can use a string anywhere PHP expects a number. The string is
presumed to start with an integer or floating-point number. If no
number is found at the start of the string, the numeric value of that
string is 0. If the string contains a period (.)
or upper- or lower-case e, evaluating it numerically produces
a floating-point number. For e.g.,
“9 Lives” -1; //8 (int)
“3.14 Pies” * 2; //6.28 (float)
“1E3 Points of Light” + 1 //1001 (float)
Types of Operators:
- Arithmetic operators
- String Concatenation operator
- Auto increment and Auto Decrement operator
- Comparison operators
- Bitwise operators
- Logical operators
- Casting operators
- Assignment operators
- Miscellaneous operators
The first and foremost operator of all PHP operators is arithmetic
operators. Arithmetic operators are set of operators that perform
arithmetic operations on numeric values. Most of the arithmetic
operators are binary; however, the arithmetic negation (-) and
arithmetic assertion (+) operators are unary.
Binary arithmetic operators are: +, -, *, / and % and they
perform operations like addition, subtraction, multiplication,
division and modulo division respectively. If they are applied on
non-numeric values, they are converted into numeric values
automatically.
The unary arithmetic operator arithmetic negation (-)
multiplies the value of its operand by -1 and hence changes its sign.
For e.g., -(3-4) evaluates to 1. The arithmetic assertion operator
(+) returns the operand multiplied by +1, which has no effect. It is
used as a visual cue to indicate true sign of a value.
The second type of operator in PHP is String Concatenation
operator. This operator is denoted by the symbol dot (.),
which appends the right-hand operand to the left-hand operand and
returns the resulting string. Operands are first converted to
strings, if necessary. For e.g.:
$n = 5;
$s = ‘5 +’ . $n . ‘is 10’; //$s is ‘5 + 5 is 10’