Expressions in PHP are formed from literal values (i.e. integers, floats, strings etc.), operators and the return values of function calls.
An example of an expression with two literals (in this case integers) separated by an operator would be $my_var = 4 + 5;.
When two literals of different types (e.g. integer and float) are used then PHP will automatically type the result. For example $my_var = 4.0 + 5; will be a float type.
Operator Precedence
In common with other programming languages, PHP gives precedence to multiplication and division over addition and subtraction etc.
Memorising and using the order of operator precedence usually leads to mistakes and unreadable code therefore the easiest way is to use parenthesis.
For example $my_variable = 3 + 4 * 2; means that $my_variable = 11;. However, $my_variable = 3 + (4 * 2); is less error prone and much clearer.
Type Conversion
PHP has several methods that allow variables of one type to be converted to another. The following functions explicitly convert types.
$var = intval("10"); converts the string "10" to the integer 10.
$var = strval(10.2); converts the float 10.2 to the string "10.2".
$var = floatval(10); converts the integer 10 the float 10.0.
Type-Casting
PHP also supports type-casting whereby the desired type is placed in parentheses in front of the variable.
(int) $var - casts $var as an integer.
(float) $var - casts $var as a float.
(bool) $var - casts $var as a boolean.
(string) $var - casts $var as a string.
(array) $var - casts $var as an array.
(object) $var - casts $var as an object.
Examples of Type-Casting
Value of $var
(int) $var
(float) $var
(bool) $var
(string) $var
10.8
10
10.8
true
"10.8"
0
0
0
false
"0"
"9"
9
9
true
"9"
"5 inches"
5
5
true
"5 inches"
true
1
1
true
"1"
false
0
0
false
""
Automatic Type Conversion
PHP will automatically convert the type of a variable if two different types are combined in an expression or if a variable is passed to a library function that expects a different type.
The same rules are applied as for type-casting. Example $var = "10" + 0.2; will set $var as a float equal to 10.2.
Finding a Variable's Type
Because PHP is a loosely typed language the following functions can be used to check a variable's type - returning either true or false.
is_int($var) returns true if $var is an integer.
is_float($var) returns true if $var is a float.
is_string($var) returns true if $var is a string.
is_bool($var) returns true if $var is boolean.
is_array($var) returns true if $var is an array.
is_object($var) returns true if $var is an object.
Finding a Variable's Content
PHP provides two means to test the contents of a variable.
isset() tests if a variable is set with a non-NULL value. A variable is considered to be NULL if ...
It has not yet been set to any value.
It has been unset().
It has been assigned the constant NULL.
empty() tests if a variable has a value.
These appear to be the same but they're not. For example if $var = 0 then both isset($var) and empty($var) return true. Whereas if $var = NULL then isset($var) returns false but $empty($var) returns true.
A variable can be destroyed by using unset() e.g. unset($var) will set $var equal to NULL.