In my application, I was handling a querying function to fetch data only if the query was a SELECT statement or similar.
Initially, I used the following code:
if (stripos(trim($query), 'SELECT') === 0) {
//do the fetching
}
Later, I had to add other conditions since I was calling some stored procedures. The statement ended up like this:
if (stripos(trim($query), 'SELECT') === 0 || stripos(trim($query), 'CALL')) {
//do the fetching
}
But it was not looking good at all, felt quite verbose.
Upon researching, I found that PHP 8 introduced a new string function called str_starts_with to check if a string starts with a particular character or substring:
if (str_starts_with($query, 'SELECT') || str_starts_with($query, 'CALL')) {
//do the fetching
}
Looking good, right? :-)