1

I am a programmer and I am picking up PHP, I cannot seem to search for this so I hope this is not a duplicate. Currently this is giving me an error at line 4 on header.php which says: unexpected $end

I know that it is complaining about the syntax but I don't fully understand how the files are parsed processed and get executed at runtime (hence creating this error). I would be grateful if somebody can fill me in if I have missed any core concept in PHP, thanks.

header.php

<?php
if(true){
    echo "Helloworld";
?>

body.php

<?php
include('header.php');

echo "The great brown fox jumps over the lazy dog.";

include('footer.php');
?>

footer.php

<?php
}else{
    echo "bye bye";
}
?>
3
  • and in the real world your doing what? hypothetical questions get hypothetical answers!. Commented Feb 8, 2012 at 3:21
  • @JohnB Indeed! I felt something wasn't right when I typed that lol Commented Feb 8, 2012 at 3:39
  • @Dagon I was implementing SESSION so any not-logged-in users will have no access to the page, however I didn't want to add to every file (although just around 10 at this point) an if-else clause where the else would redirect the non-logged-in users to the login page. Perhaps this is a silly way of doing it, if it is, I didn't know better, and would love to hear from experienced guys like you some proper common practices in PHP, thanks. Commented Feb 8, 2012 at 3:39

3 Answers 3

3

Each file included must be able to be parsed on its own without a syntax error. Your header has a syntax error of an unclosed if block. Likewise, the footer has a syntax error of an else block coming out of nowhere. You cannot do it this way.

Instead you can use something like:

// Global variable defined above includes:
$condition = TRUE;
include('header.php');
echo "The great brown fox jumps over the lazy dog.";
include('footer.php');


// header.php
if ($condition) {
  echo "hello world";
}

// footer.php
if (!$condition) {
  echo "bye";
}
Sign up to request clarification or add additional context in comments.

2 Comments

But within ONE SINGLE FILE, I can indeed have an if-else clause that spans across multiple <?php ?> , right? So this would work if everything is in one file but not three (using include)?
@Gapton Yes you can open an if(), go through many <?php ?> and close the else later at the end, as long as it all works in one file.
0

Also using output buffing to grab content is way better then just including raw html into your code flow.

<?php 

function get_page_section($path){
    if(file_exists($path)){
        //grab "output" to the return variable
        ob_start();
        require($path);
        $return = ob_get_contents();
        ob_end_clean();
        return $return;
    }else{
        //path not found
        throw new Exception('get_page_section('.$path.') not found');
    }
}

$header = get_page_section('header.php');
$footer = get_page_section('footer.php');


echo $header.
     "The great brown fox jumps over the lazy dog.".
     $footer;

?>

Comments

0

in header.php type the following code

<?php
if(true){
    echo "Helloworld";
 }else{
     echo "bye bye";
     exit();
  }
?>

in the body.php continue as you wrote before

<?php
include('header.php');

echo "The great brown fox jumps over the lazy dog.";

this is the simplest way you can solve the probelm and hope this would help

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.