Get Current File Name

<?php
    $pageName = basename($_SERVER['PHP_SELF']);
?>

Potential use:

<body id="body_<?php echo $pageName; ?>">

Append ID to body to do different CSS styles on different pages.

Comments

  1. User Avatar
    Gilbert
    Permalink to comment#

    Chris your snippets section is working. I found this on Google it was just what I needed.

  2. User Avatar
    Zlatan Halilovic
    Permalink to comment#

    sweet

  3. User Avatar
    cshen
    Permalink to comment#

    yeah! this is working, thank you.

  4. User Avatar
    sourav
    Permalink to comment#

    echo $pageName = basename($_SERVER[‘SCRIPT_NAME’]);

    It will give current file name… as index.php or about.php

  5. User Avatar
    Marcio Toledo

    But you will have ‘.php’ as id or class. If you want get only name befor the ‘.php’ here is de code:

    // Recuperando o nome do arquivo sem '.php'
      $currentFile = $_SERVER["PHP_SELF"];
      $parts = Explode('/', $currentFile);
      $parts = $parts[count($parts) - 1];
      $parts = Explode('.', $parts);
      $pageName = $parts[0];
    
    • User Avatar
      Maxi
      Permalink to comment#

      I agree. Simply I enclose files with regular dot(s) in the name:

      $currentFile = $_SERVER["PHP_SELF"];
      $parts = explode('/', $currentFile);
      $parts = $parts[count($parts) - 1];
      $name = explode('.', $parts);
      unset ($name[count($name) - 1]);
      $pageName = implode('.',$name);
      
  6. User Avatar
    Hilco
    Permalink to comment#

    better yet:
    echo basename($_SERVER['REQUEST_URI'], '.php');

    • User Avatar
      nico
      Permalink to comment#

      great, thank you

    • User Avatar
      NoOneOfConsequence
      Permalink to comment#

      Yo…Hilco – thanks.

      This rocks for my purposes…dynamically searching a database for stuff that’s only meant to be shown on certain pages.

  7. User Avatar
    immeëmosol
    Permalink to comment#
    echo substr(
      __FILE__
      , strrpos( __FILE__ , '/' )
      , strrpos( __FILE__ , '.' )
    );

    ? http://php.net/manual/en/language.constants.predefined.php .

  8. User Avatar
    Dmitry
    Permalink to comment#

    You have an error in your code, $_SERVER[PHP_SELF] instead of $_SERVER[‘PHP_SELF’]

  9. User Avatar
    hey
    Permalink to comment#

    never know how these php snippets work

    any link to demo to show few site, with the screencapture on the SERVER code that php was implemented

    got very confuse, that these things work or NOT

  10. User Avatar
    Johnslegers
    Permalink to comment#

    A very useful feature that seems totally absent in Javascript.

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag