Concept
How does PHP handle different variable types in arithmetic expressions?
ComputerScienceOne / Type Juggling
"The expectations of an arithmetic expression involving two variables that are either integers or floating point numbers are straightforward. We expect the sum/product/etc. as a result. However, since PHP is dynamically typed, a variable involved in an arithmetic expression could be anything, including a Boolean, object, array, or string. When a Boolean is involved in an arithmetic expression, true is treated as 1 and false is treated as zero. If an array is involved in an expression, it is usually a fatal error. Non-null objects are treated as 1 and null is treated as 0. PHP does allow you to “add” two arrays together which results in their union."
Related Ideas
- How can PHP values and variable states be converted and checked explicitly?ComputerScienceOne · Type Juggling
- How does Java handle arithmetic with wrapper classes?ComputerScienceOne · Operators
- How does C handle arithmetic operators and integer division?ComputerScienceOne · Operators
- What types of values does PHP support?ComputerScienceOne · Using Variables
- What is the order of precedence and associativity of PHP operators?ComputerScienceOne · Order of Precedence