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 PHP handle undefined variables, null values, and constants?ComputerScienceOne · Using Variables
- How are variables and variable types handled in PHP?ComputerScienceOne · Entry Point & Command Line Arguments
- What types of values does PHP support?ComputerScienceOne · Using Variables
- How can strings be used as indices in PHP arrays?ComputerScienceOne · Strings as Indices
- How can strings be used as indices in PHP arrays?ComputerScienceOne · Non-Contiguous Indices
- How does PHP compute the roots of a quadratic equation?ComputerScienceOne · Computing Quadratic Roots
- How do you declare functions and specify their return values in PHP?ComputerScienceOne · Declaring Functions