Skip to content
Advertisement

PHP Query – Check string character

if (substr('xcazasd123', 0, 2) === 'ax'){
}

Above code is working where it able to check if the “variable” is starting with ‘ax’, but what if i wanted to check “multiple” different validation ?

for example : ‘ax’,’ab’,’ac’ ? Without creating multiple if statement

Advertisement

Answer

You can use array to store your keywords and then use in_array() if you want to avoid multiple if statements.

$key_words =["ax","ab","ac"]
if (in_array( substr('yourstringvalue', 0, 2), $key_words ){
  //your code
}

References: in_array — Checks if a value exists in an array

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement