This is what those functions do exactly:
substr()
is used to generate a sub-string of specified length from another string.
strlen()
will return the length of the provided string.
Code substr($query,0,strlen($query)-2)
removes comma and space from foreach
Loop.
solved What is the purpose of strlen($query)-2;