What is PHP?
PHP is a general-purpose server-side language for web development. PHP supports many protocols and standards, such as HTTP, SMTP, POP3, IMAP, FTP, and XML. PHP integrates with other web development technologies such as HTML, CSS, JavaScript, and SQL. Due to its open nature and huge developer community, PHP has many libraries and frameworks that simplify and speed up web application development. PHP is also a cross-platform language, allowing it to run on various operating systems such as Windows, macOS, Linux, and Unix.
What is an array in PHP?
An array in PHP is a variable we use to store data containing various elements. The elements of an array are identified by a unique key or index, which can be a number or a string. PHP has two types of arrays: indexed arrays and associative arrays. PHP provides many built-in functions for working with arrays, including searching, sorting, splitting a string to array, converting an array to string, converting an array to JSON, and getting the length of an array.
PHP In Array Syntax
The following is the syntax of the in_array() function:
Where:
- search: specifies the element or value to look up in the given array
- array: specifying an array to search for a value
- mode (optional): specifies the mode to perform the search. If set to "TRUE", the in_array() function searches for the search-string and specific type in the array. The default value for this parameter is "FALSE".
PHP In Array Examples
The following are examples of checking if an element exists in a PHP array:
Checking if a element exists in an associative array
The following is an example of checking if a value exists in an associative array:
Checking if an element exists in a multidimensional array
The following is an example of checking if a value exists in a multidimensional array:
Checking if an element exists in an array, case-insensitive
To check if an array contains an element, case insensitively, you can convert all elements in the array and the target element to lowercase (or uppercase) and then use the in_array() method to check for the presence of the target element: