Apply function only if end of url has /amp/ [closed]

In your example, $_SERVER['REQUEST_URI'] would return /category/post-slug/amp/ which is not the same as get_permalink($post->ID) which would return https://example.com/category/post-slug/

There are lots of ways to go about this, but one easy one that comes to mind is…

if ( strpos($_SERVER['REQUEST_URI'], '/amp/' ) !== false ) {

The strpos() PHP function finds the position of 2nd string within the 1st string, and returns false if it’s not found.

So, that essentially searches the Request URI for the existence of ‘/amp/’ and if it’s found, then it proceeds with the rest of our function.