简体   繁体   English

PHP开关盒连接

[英]PHP switch case Connects

I have the following code: 我有以下代码:

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';

Now let's take an example, and apply the following code to header.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;

And the following, to footer.php: 然后是footer.php:

if ($page == 'profile'):
}else{
header('Location: index.php');
}
endif;

Site structure it's following: 网站结构如下:

index.php 的index.php

pages (Folder) 页数 (文件夹)

= =

header.php header.php文件

index.php 的index.php

footer.php footer.php

profile.php profile.php

But the problem its header.php and footer.php , wich seems to not make connection one with another, cuz i receive the following error: 但是问题它的header.php和footer.php,这似乎不使彼此之间建立连接,因为我收到以下错误:

Parse syntax error, T_ENDIF 解析语法错误,T_ENDIF

How can i change code to make connection for header.php with footer.php ? 我如何更改代码以与footer.php建立header.php的连接?

Cheers! 干杯!

There are two syntaxes in PHP for IF - THEN - ELSE. PHP中的IF有两种语法-THEN-ELSE。 One is if (condition) : ... endif; 一种是if (condition) : ... endif; . The other one is if (condition) { } else { } . 另一个是if (condition) { } else { } Don't mix these. 不要混合这些。 If you use the colon : and endif; 如果使用冒号:endif; , don't use curly braces { } . 请勿使用花括号{ } If you use curly braces (recommended!), don't use colon and endif. 如果使用花括号(推荐!), 请不要使用冒号和endif。

Inside your switch statement, I recommend you put the default at the end. 在您的switch语句中,建议您将default放在最后。

As others have pointed out, you are mixing if() { ... } and if(): endif; 正如其他人指出的那样,您正在混合if() { ... }if(): endif; , which is confusing things. ,这使事情变得混乱。 There's no actual difference between the two, but they have to match up in pairs - you can't write if ( test_something() ) { do_something(); endif; 两者之间没有实际区别,但它们必须成对配对- if ( test_something() ) { do_something(); endif; if ( test_something() ) { do_something(); endif; This is documented in the manual under Alternative syntax for control structures : 手册中的“控制结构的替代语法”中对此进行了说明:

Note: Mixing syntaxes in the same control block is not supported. 注意:不支持在同一控制块中混合语法。

The other thing to note is that each PHP file included must have valid syntax on its own - PHP doesn't stick all the code together and then parse it - so you can't open an if statement in one file and then close it in another. 还要注意的另一件事是,每个包含的PHP文件必须自己具有有效的语法-PHP不会将所有代码粘贴在一起然后进行解析-因此,您不能在一个文件中打开if语句,然后在其中关闭它另一个。 (I can't actually find a good manual reference for this; if someone knows one, let me know in the comments, or edit it in here.) (为此,我实际上找不到很好的手册参考;如果有人知道,请在评论中让我知道,或在此处进行编辑。)

If you always indent further when you open an if statement or similar block, the structure of code generally becomes much clearer. 如果在打开if语句或类似的块时总是缩进,则代码的结构通常会更清晰。 If we do this to your code, and strip out everything else, we are left with this; 如果我们对您的代码执行此操作,并删除其他所有内容,那么我们将剩下这个; see the comments I've added for where it's going wrong: 查看我添加的关于错误原因的评论:

// 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;

Put your default statement after all specific case statement. 将您的默认语句放在所有特定的case语句之后。 Other wise the default statement block will be executed and other cases ignored 否则,将执行默认语句块,并忽略其他情况

    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;

    }

Edit: 编辑:

There is no endif; 没有endif; statement in php 用PHP声明

Sorry, I learned somthing... Thanks :) 抱歉,我学到了一些东西...谢谢:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM