Automatic switching by file name in the sidebar design

Web pages and blogs, if the design is uniform in the sidebar, it is often the same pages. I use PHP to do a different design so that it automatically by file name.

A file name is acquired using PHP.

Use with substr() PHP function

The function of substr() is taking out a part of character string.

substr(string, start, [length])

substr() takes out and returns the character string for a length byte from the position specified by the character strings string and start. When start is positive, it becomes a character string taken out and a character string which counts from 0 of string and begins from a start position.

When start is negative, it becomes a character string taken out and a character string which counts from the back of string and begins from a start position.

Gets the file name with substr() PHP function

<?php
$f_name = substr($_SERVER['SCRIPT_NAME'],-9,9);
?>

"$_SERVER['SCRIPT_NAME']" expresses the file name behind a server's domain name with this example. For example, "/index.php" is expressed if it is "hogehoge.jp/index.php". If 9 characters are taken out from the 9th character, it will be set to "index.php" from the back in this.

The design of a side bar is automatically changed by file name.

As follows, operation in case a file name is "index.php", and operation in case a file name is except "index.php" can be changed. Of course, when there is no operation in case a file name is except "index.php", "else" or subsequent ones is deleted.

It is common to not only a side bar but a header, a footer, a menu, etc., and can use this method for the file to read. It is only a specific page, and it is effective when carrying out different operation.

<?php
$f_name = substr($_SERVER['SCRIPT_NAME'],-9,9);
if ($f_name == "index.php"){
Operation in case a file name is "index.php"
}
else {
Operation in case a file name is not "index.php"
}
?>

Moreover, it can also judge conversely by using a negative operator. It is performed as follows for describing HTML tags.

<?php
$f_name = substr($_SERVER['SCRIPT_NAME'],-9,9);
if ($f_name != "index.php"){
print <<<EOD
Operation in case a file name is not "index.php", for describing HTML tags.
EOD;
}
?>

The merit which changes the display of a side bar automatically by a file name

The necessity that a design prepares another file by a file name is lost. For example, in "index.php", a menu is displayed on a side bar, and it can be prevented from expressing a menu as the other page. Since the number of files decreases, management of a file becomes easy.

Moreover, in an index page, a picture can be displayed and it can also be made the quick text display of a display on the other ordinary page. In short, it is dependent on your idea.