note 69588 deleted from function.array-search by crell
| From: | crell@php.net | Date: | Thu, 31 Mar 2022 20:40:03 +0000 |
| Subject: | note 69588 deleted from function.array-search by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-217954@lists.php.net to get a copy of this message | ||
Note Submitter: mark dot php at mhudson dot net
----
I was going to complain bitterly about array_search() using zero-based indexes, but then I realized
I should be using in_array() instead.
// if ( isset( $_GET['table'] ) and array_search( $_GET['table'], $valid_tables)
) { // BAD: fails on first[0] element
// if ( isset( $_GET['table'] ) and ( FALSE !== array_search( $_GET['table'],
$valid_tables) ) ) { OK: but wasteful and convoluted
if ( isset( $_GET['table'] ) and in_array( $_GET['table'], $valid_tables) ) {
// BETTER
The essence is this: if you really want to know the location of an element in an array, then use
array_search, else if you only want to know whether that element exists, then use in_array()