Chapter
Type Juggling
From ComputerScienceOne by Dr Chris Bourke
Concepts
- How does the modulo operator calculate remainders in PHP?
"$x = 10 % 5; //x is 0\n\n$x = 10 % 3; //x is 1\n\n$x = 29 % 5; //x is 4"
- How does PHP handle different variable types in arithmetic expressions?
"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
- How does PHP convert numeric strings during arithmetic operations?
"When string values are involved in an arithmetic expression, PHP does something called type juggling. When juggled, an attempt is made to convert a string variable into a numeric value by parsing it.
- How can PHP values and variable states be converted and checked explicitly?
"Relying on type juggling to convert values can be ugly and error prone. You can write much more intentional code by using the several conversion functions provided by PHP. For example:\n\n$a = intval