This one stumped me for a bit when i was trying to use jQuery to populate checkboxes with a list of items pulled from a database using PHP.
The fact of the matter is that PHP’s
in_array() returns a boolean whereas jQuery’s
inArray() returns the index of the matching element, or if the element is not found it will return
undefined, depending on the browser.
If you are used to using PHP like me jQuery’s
inArray() can give you a headache when you are using the returned value of the function in an if-else condition. This is because
-1 evaluates as
true, when you really want it to evaluate as false.
Make it so that your if-else condition checks for a value
>=0 (greater than or equal to zero). Since array’s don’t have negative indexes you’re safe doing it this way.
What did i learn from this? When working with a new function check the documentation for what type of data it will return.