PHP开关盒连接

PHP开关盒连接,php,switch-statement,case,Php,Switch Statement,Case,我有以下代码: switch ($page) { default: $page = 'error'; // header require_once 'pages/header.php'; // content require_once 'pages/error.php'; break; case 'index': case 'login': case 'register': case 'profile': // header

我有以下代码:

switch ($page)
{
default: 
    $page = 'error';

    // header
    require_once 'pages/header.php';

    // content
    require_once 'pages/error.php';
    break;

case 'index':
case 'login':
case 'register':
case 'profile':
    // header
    require_once 'pages/header.php';

    // content
    if (file_exists('pages/' . $page . '.php')) require_once 'pages/' . $page . '.php';


    break;

}

/*
* Footer
*/
require_once 'pages/footer.php';
现在让我们举一个例子,将以下代码应用于header.php:

if ($page == 'profile'):
include 'members/core/init.php';
if(isset($_GET['username']) && empty($_GET['username']) === false) {

$username   = htmlentities($_GET['username']);
if ($users->user_exists($username) === false) {
    header('Location:index.php');
    die();
}else{
    $profile_data   = array();
    $user_id        = $users->fetch_info('id', 'username', $username);
    $profile_data   = $users->userdata($user_id);
} 
endif;
以及以下内容,请访问footer.php:

if ($page == 'profile'):
}else{
header('Location: index.php');
}
endif;
网站结构如下:

index.php

页面(文件夹)

=

header.php

index.php

footer.php

profile.php

但是问题是它的header.php和footer.php,它似乎无法相互连接,因为我收到以下错误:

解析语法错误,T_ENDIF

如何更改代码以连接header.php和footer.php


干杯

将默认语句放在所有特定案例语句之后。否则,将执行默认语句块,并忽略其他情况

    switch ($page)
    {


    case 'index':
    case 'login':
    case 'register':
    case 'profile':
        // header
        require_once 'pages/header.php';

        // content
        if (file_exists('pages/' . $page . '.php')) require_once 'pages/' . $page . '.php';


        break;
    default: 
        $page = 'error';

        // header
        require_once 'pages/header.php';

        // content
        require_once 'pages/error.php';
        break;

    }
编辑:

没有
endif语句


对不起,我学到了一些东西。。。谢谢:)

将您的默认声明放在所有特定案例声明之后。否则,将执行默认语句块,并忽略其他情况

    switch ($page)
    {


    case 'index':
    case 'login':
    case 'register':
    case 'profile':
        // header
        require_once 'pages/header.php';

        // content
        if (file_exists('pages/' . $page . '.php')) require_once 'pages/' . $page . '.php';


        break;
    default: 
        $page = 'error';

        // header
        require_once 'pages/header.php';

        // content
        require_once 'pages/error.php';
        break;

    }
编辑:

没有
endif语句


对不起,我学到了一些东西。。。谢谢:)

在PHP中,IF-THEN-ELSE有两种语法。一个是
if(条件):
<代码>endif。另一个是
if(condition){}else{}
。不要把这些混在一起If使用冒号
endif
不要使用大括号
{}
如果使用大括号(推荐!),不要使用冒号和endif


switch
语句中,我建议您将
default
放在末尾。

在PHP中,IF-THEN-ELSE有两个语法。一个是
if(条件):
<代码>endif。另一个是
if(condition){}else{}
。不要把这些混在一起If使用冒号
endif
不要使用大括号
{}
如果使用大括号(推荐!),不要使用冒号和endif


在您的
开关
语句中,我建议您将
默认值
放在末尾。

正如其他人所指出的,您正在混合
if(){…}
if():endif,这是令人困惑的事情。两者之间没有实际的区别,但它们必须成对匹配-您不能编写
if(test_something()){do_something();endif;
,手册中记录了这一点:

注意:不支持在同一控制块中混合语法

需要注意的另一点是,包含的每个PHP文件都必须有自己的有效语法—PHP不会将所有代码粘在一起然后对其进行解析—因此不能在一个文件中打开
if
语句,然后在另一个文件中关闭它。(我实际上找不到一个好的手册参考;如果有人知道,请在评论中告诉我,或者在这里编辑。)

如果您在打开
If
语句或类似块时总是进一步缩进,代码的结构通常会变得更清晰。如果我们对您的代码执行此操作,并去掉所有其他内容,则只剩下此项;请参阅我添加的注释,了解哪里出错:

// header.php
    if ($page == 'profile'):

        if(isset($_GET['username']) && empty($_GET['username']) === false) {

            if ($users->user_exists($username) === false) {
            }else{
            } 

        // Closing with endif, but opened with {
        endif;

   // unclosed if statement at end of file

// footer.php:

    if ($page == 'profile'):

    // closing with } but opened with :
    }else{
    }

// stray endif, presumably intended to match the if: at the top of header.php
endif;

正如其他人所指出的,你把
if(){…}
if():endif;
混为一谈,这让人很困惑。两者之间没有实际的区别,但它们必须成对匹配——你不能编写
if(test_something()){do_something();endif;
,这在手册的以下部分中有说明:

注意:不支持在同一控制块中混合语法

需要注意的另一点是,包含的每个PHP文件都必须有自己的有效语法—PHP不会将所有代码粘在一起然后对其进行解析—因此不能在一个文件中打开
if
语句,然后在另一个文件中关闭它。(我实际上找不到一个好的手册参考;如果有人知道,请在评论中告诉我,或者在这里编辑。)

如果您在打开
If
语句或类似块时总是进一步缩进,代码的结构通常会变得更清晰。如果我们对您的代码执行此操作,并去掉所有其他内容,则只剩下此项;请参阅我添加的注释,了解哪里出错:

// header.php
    if ($page == 'profile'):

        if(isset($_GET['username']) && empty($_GET['username']) === false) {

            if ($users->user_exists($username) === false) {
            }else{
            } 

        // Closing with endif, but opened with {
        endif;

   // unclosed if statement at end of file

// footer.php:

    if ($page == 'profile'):

    // closing with } but opened with :
    }else{
    }

// stray endif, presumably intended to match the if: at the top of header.php
endif;

我收到了相同的错误:Parse error:syntax error,header中意外的T_ENDIF..Ty出于您的兴趣php中没有ENDIF;语句。只需使用If(条件){//stationblock1}else If(条件2){//statementblock2}else{//statementblock3}@Damodaran@HeikkiU你完全正确。但是编辑答案本身是不合适的。将
默认值
大小写放在不同的位置对大小写的计算方式没有影响。我收到了相同的错误:解析错误:语法错误,标题中意外的T_ENDIF..Ty出于您的兴趣,php中没有ENDIF;语句。只要使用If(条件){//stationblock1}else If(条件2){//statementblock2}else{//statementblock3}@Damodaran@HeikkiU就行了。但是编辑答案本身并不合适。将
默认值
案例放在最后一个以外的地方对案例的评估方式没有影响。我推荐使用记事本++,这很好。(如果你想得到另一个答案,请说得更具体一些。我不是来为你写代码的。)我很抱歉,但我真的需要这部分代码的帮助..我是php新手,所以我需要像你这样的经验丰富的人的帮助。你说在php中有两个用于If-THEN-ELSE的语法。一个是If(条件):…endif;。另一个是If(条件):…endif(条件){}否则{}。不要混合使用这些。如果使用冒号:和endif;,不要使用大括号{}。如果使用大括号(推荐!),不要使用冒号和endif。在switch语句中,默认值必须是最后一个,但我真的不知道如何使用