PHP Get Current Page Slug

In this tutorial we will see How To Get Current Page Slug in PHP. $_SERVER['REQUEST_URI'] which is a part of $_SERVER variable can be used to get the URL or slug of current page.

$_SERVER['REQUEST_URI']

$_SERVER is a superglobal array, it contains information about headers, paths, and script locations.

$_SERVER['REQUEST_URI'] contains the URL or slug of current page.

Take a look at the code given below.

<?php
$slug = $_SERVER['REQUEST_URI'];
echo $slug;
?>

In this example Slug of Current Page is stored in variable named $slug and it is displayed using PHP echo statement.