- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
This will produce the following output −
- Return True if a document exists in MongoDB?
- Why does MySQL evaluate “TRUE or TRUE and FALSE” to true?
- 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
- 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
- Why does my MongoDB group query return always 0 in float conversion? How to fix it?
- Return true or false in a MySQL select if another field contains a string?
- Return True if two Numpy arrays are element-wise equal within a tolerance
- Compare and return True if two string arrays are equal in Numpy
- Return True if all entries of two arrays are equal in Numpy