Move some inline php to a function