954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

If File Name is X then include Y

I am trying to make my website display a different header depending on the page. If the page is 'index.php', it should include 'index-header.php'. If it isn't 'index.php', then it should include 'page-header.php'.

So far, I have the following code:

<?php
if($_SERVER[SCRIPT_NAME] == '/index.php'){include (index-header.php);}
else { include ('page-header.php');
?>


When I upload it, I get the following error:
'Parse error: syntax error, unexpected $end in /home/a3808hos/public_html/test/header.php on line 4'

Thanks for the help :D

MichaelBerh
Newbie Poster
2 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

Have you tried `beutifiying` your code?
eg:

/* Code: */
session_start(); if(isset($_SESSION['somevar'])){echo $_SESSION['somevar']; }else{echo 'othervar'; }
/* Would Be:*/
session_start();
if(isset($_SESSION['somevar'])) {
    echo $_SESSION['somevar'];
} else {
    echo 'othervar';
}

This often helps you to find errors in your code.
Also, use single quotes or double quotes unless using a variable or a variable created using

define();


.
And, if you don't notice it, you missed out the last curly brace to close the if...else statement.

Matthew N.
Junior Poster
101 posts since Jul 2010
Reputation Points: 10
Solved Threads: 10
 
if($_SERVER[SCRIPT_NAME]...


change to

if(basename($_SERVER['PHP_SELF']) == 'index.php'){
diafol
Rhod Gilbert Fan (ardav)
Moderator
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

the source of this error is the missing closing brace in line 3 as matthew wrote, errors are usually just before the reported line number
the missing quotes (per the OP) $_SERVER['variable'] will cause another set of problems later
format is not optional

good code editors provide code highlighting that will negate this occurring again

the PHP manual is available online, as a downloadable text,chm ,any number of file types

perhaps, get the php manual and as a last resort, and, when nothing else works[indent] RTFM[/indent]

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 
<?php
if(basename($_SERVER['SCRIPT_NAME']) == 'index.php'){
include ('index-header.php');
} else {
include ('page-header.php');
}
?>
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: