0

Here is the issue there are 3 files
1. head.php
2. nav.php
3. footer.php

Problem is if user logged in then i want to show the different layout structure if not then another structure. What I'm doing is i don't want to repeat views like html opening or closing tag in every file so that's why I'm creating partial files for that like head.php and so on. So it's a problem in control structure statement if else clause, It is written on head.php file and there are errors come.
Parse error: syntax error, unexpected end of file in head.php
Parse error: syntax error, unexpected 'endif' (T_ENDIF), expecting end of file footer.php

I know PHP says write if else clause on the same file and end it. But is there a way to solve this problem.

Code head.php:

<?php $loggedIn = false; ?>
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
    <meta http-equiv="content-type" content="text/html" charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale =1, shrink-to-fit=no">
    <title>Title</title>
</head>
<body>
<?php if (!$loggedIn) : ?>
    <p>You are not logged in </p>
    <?php require 'footer.php'; ?>
<?php else :
require 'nav.php';
require 'footer.php.php'; ?>

Code nav.php:

<nav>
    <ul>
        <li><a href="">Home</a></li>
        <li><a href="">Logged Out</a></li>
    </ul>

</nav>

Code footer.php:

</body>
</html>
<?php endif; ?>
John Doe
  • 137
  • 4
  • 2
    You __can't__ split `if` between several files. Over. – u_mulder Sep 02 '17 at 08:30
  • You can add same condition in files you needed – Jigar Shah Sep 02 '17 at 08:31
  • @u_mulder then what approach i use – John Doe Sep 02 '17 at 08:32
  • @u_mulder you mark this question with the duplicate question that doesn't make sense i'm just asking about approach what you use for that kind of problem – John Doe Sep 02 '17 at 08:38
  • This question is a duplicate of that one - because you get the same error. That being said, you can't split conditions up into different files like that. You need to check for that condition for each file (in which case you need to ensure the variable you are testing on is within the scope), or restructure your approach. Those are your two options. – Qirel Sep 02 '17 at 09:11
  • @JohnDoe You can use switch statement to achieve this. Enter this code in your head.php file. `

    You are not logged in

    `
    – Ahmad Hussnain Sep 02 '17 at 09:33

0 Answers0