
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Why does php's in_array return true if passed a 0?
The reason behind in_array returning True could be the string-to-number conversion. When a string is passed to the function, it returns 0, which is the value that needs to be searched for.
PHP uses loose juggling, i.e. using == instead of === when elements are compared. Hence, their values are compared and not the types.
Another reason is type juggling, which means a variable type is dealt with in the context of the code.
For example- when a float value is assigned to a variable, it becomes a floating-point value. It behaves in a way when a string is casted to an integer
To avoid this kind of behaviour, a third parameter can be passed, which compares the data in a strict mode, wherein the values as well as types are compared.
Below is how the function behave when a string is passed −
Example
echo intval("Hello");
Output
This will produce the following output −
0
- Related Articles
- If ([] == false) is true, why does ([] || true) result in []? - JavaScript
- Return True if a document exists in MongoDB?
- Compare and return True if an array is less than another array in Numpy
- Compare and return True if an array is greater than another array in Numpy
- Why does MySQL evaluate “TRUE or TRUE and FALSE” to true?
- Compare and return True if a Numpy array is greater than equal to another
- Compare and return True if a Numpy array is less than equal to another
- JavaScript Determine the array having majority element and return TRUE if its in the same array
- 58000000 × 0 = 0 Why It's 0 Only
- Why does my MongoDB group query return always 0 in float conversion? How to fix it?
- Why in JavaScript, “if ('0' == false)” is equal to false whereas it gives true in “if(0)” statement?
- Why does -22 // 10 return -3 in Python?
- How to remove elements from an array until the passed function returns true in JavaScript?
- Return an array with numeric keys PHP?
- Why Does A Fast Car Slows Down If It's Engine Is Switched Off?
