Using the Function Intdiv in PHP
In php we often come across situations when we need to divide two numbers and return the quotient as an integer. For such cases the built in function intdiv is a great help. This function can be found in the sys class and it accepts two parameters dividend and divisor and returns the division value as an integer. The function also has the capability to return a division value as float, however if it divides a number by 0 or -1 then it throws an error message.
This is caused by a type mismatch where the variable is an integer and the return value is a string. Type juggling is one of the core features that php offers and it can help in many cases but there are certain moments where type coercion may cause problems.
One of the main issues with type juggling is the fact that php doesn’t enforce strict typing. Unless you use strict_types or set it as your default value, the language will perform implicit type conversion (coercion) when you pass int or float values into non-int types.
The reason why this happens is that the language uses union types which are kind-of compound types. A union type is defined as T|null meaning that it contains both int and float elements. So when we pass an int value into a union type it will convert to a float because the int element of the union is higher than the float element. This can be dangerous because if the int is too low it will result in a null pointer exception while if it is too high it may result in a division by zero error.