What are the advantages and disadvantages of using a search function that does not rely on a database in PHP?
When using a search function that does not rely on a database in PHP, the main advantage is that it can be faster and more lightweight since it does not require querying a database. However, the disadvantage is that it may not be as efficient for handling large amounts of data or complex search queries.
<?php
// Example of a search function in PHP that does not rely on a database
function searchFunction($searchTerm, $dataArray) {
$results = array();
foreach($dataArray as $data) {
if (stripos($data, $searchTerm) !== false) {
$results[] = $data;
}
}
return $results;
}
// Example usage
$dataArray = ['apple', 'banana', 'orange', 'grape'];
$searchTerm = 'an';
$results = searchFunction($searchTerm, $dataArray);
print_r($results);
?>
Keywords
Related Questions
- What is the role of session_start() in PHP session management and how should it be used effectively?
- Are there best practices for handling file streams in PHP to prevent errors like "supplied argument is not a valid stream resource"?
- What are some best practices for defining the replacement string in preg_replace in PHP?