How to add canonical tag to every page in website by PHP

PHP two line codes add Canonical tag in website every page

Add canonical tag dynamically by using PHP code easily with two lines of code. Canonical tag using on a webpage that tells search engines that specific URL represents the master copy not having any duplicate page.  Using canonical tag shows duplicate content present on different or multiple URLs.


$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];


<link rel="canonical" href="<?php echo dirname($actual_link).'/'; ?>" />