In your case, the simplest change is probably this:
$query4 = "(
SELECT count(*)
FROM checklist c
JOIN task AS t ON t.id = c.placement_id AND t.active=1
WHERE c.placement_id = m.id
) / (
SELECT (case when count(*) > 0 then count(*) end)
FROM task
WHERE active = 1
)";
This will replace 0
values with NULL
-- so the division will return NULL
instead of an error.