You can spare yourself a lot of php code bloat by writing a refined sql SELECT
clause with a conditional expression:
SELECT roll_num, IF(attended_no REGEXP '(^|,)3(,|$)', 'Present', 'Absent')
SQL Demo
The regular expression checks that the 3
value is never preceded or followed by another number -- so that 13
and 39
are never falsely matched. The regex pattern will correctly match if the 3 is the first value, the last value, or the only value in the comma separated string.
What does the regular expression mean? I'm glad you asked...
( #start of capture group 1
^ #match the start position of the input string
| #or
, #match a comma
) #end of capture group 1
3 #match the literal digit 3
( #start of capture group 2
, #match a comma
| #or
$ #match the end position of the input string
) #end of capture group 2
When you want to understand what a pattern is doing or you wish to test some fringe scenarios, feed it to regex101.com and upskill yourself.