簡體   English   中英

解析錯誤:語法錯誤,單獨文件的文件意外結束

[英]Parse error: syntax error, unexpected end of file on separate file

我試圖在另一個文件中包含的單獨文件中添加if條件的開頭和if條件的結尾。

我有一個名為cp.php的文件,其中包含header.php和footer.php,看起來像這樣:

<?php 
require_once "includes/header.php";
?>
<p>Welcome <?php echo htmlentities($_SESSION['username']); ?>!</p>
<p>
   This is an example protected page.  To access this page, users
   must be logged in.  At some stage, we'll also check the role of
   the user, so pages will be able to determine the type of user
   authorised to access the page.
</p>
<?php
  require_once "includes/sidebar.php";
  require_once "includes/footer.php";
?>

header.php

<?php
  include_once 'includes/db_connect.php';
  include_once 'includes/functions.php';
  sec_session_start();
?>
<html>
  <head></head>
  </body>
  <?php if (login_check($db) == true) : ?> // this if I am trying to end in footer.php

footer.php

  <?php else : ?>
  <?php header('index.php');
  <?php endif; ?>
  </body>
</html>

但是我得到了Parse error: syntax error, unexpected end of file in header.php on line 9這將是<?php if (login_check($db) == true) : ?> 我在哪里弄錯了。

刪除headers.php中第9行頂部的“:”。 用空括號替換

<?php if (login_check($db) == true) {} ?>

PHP首先解析文件,然后執行它們,這意味着每個文件都必須完整且正確。 不幸的是,您無法跨多個文件使用if語句。

改為執行此操作,並將其從HTML代碼中刪除:

if (login_check($db) !== true) { header('Location: index.php'); exit; }

把整個邏輯放在開始

if子句不能跨越多個文件。 您必須將整個if-logic移到一個PHP文件中,或將其添加到兩個文件中。

如果條件結構應為:

if($a) {
    echo $a;
}
else:
    echo $c;
endif;

這樣您將插入一個endif; 在條件結束時。

鏈接將給您一個想法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM