Removing WWW from URI (or domain) with PHP

Few weeks ago I wrote how to remove WWW from domain with .htaccess. In some cases or hosting plans it’s not possible to view or use .htaccess file (e.g. some low cost or free hosting providers). Solution is PHP code which redirects all www requests to non www page.