PHP包含页面错误

时间:2014-12-19 15:51:23

标签: php include

仍然试图了解PHP的工作方式:) 请帮我一个解决方案&想法,这是我正在使用的时候:

<?php
    $content = array(
        'id01'=>'sub_id01.php',
        'id02'=>'sub_id02.php'
    );
    if(in_array($_GET['show'], array_keys($content))) {
        include($content[$_GET['show']]);
    } else {
        include('sub_id00.php');
    }
?>

<?php
$content = array(
    'id00'=>'N/A',
    'id01'=>'ID01',
    'id02'=>'ID02',
);
if(!empty($_GET['show']) && isset($content[$_GET['show']])) {
    echo $content[$_GET['show']];
} else {
     echo $content['id00'];
}
?>

第一个例子包含页面,第二个例子包括简单的代码&#39;

问题是如果没有ID集(index.php),则ut显示默认页面/代码。如果ID设置错误,它还会显示默认页面/代码。 如何更新它,所以没有任何ID设置,它会显示默认页面,如果设置了一些错误的ID,它会显示一些错误的页面/代码? 干杯!

更新<!/强>

经过一段时间后,我已将其更新为:

<?php
    $content = array(
        'id01'=>'sub_id01.php',
        'id02'=>'sub_id02.php'
    );

    if (in_array($_GET['show'], array_keys($content)))
    {
        include($content[$_GET['show']]);
    }

    elseif (isset($_GET['show']))
    {
        include('sub_error.php');
    }
    else {
        include('sub_id00.php');
    }
?>

<?php
    $content = array(
        'error'=>'error msg',
        'id00'=>'N/A',
        'id01'=>'ID01',
        'id02'=>'ID02',
    );

    if(!empty($_GET['show']) && isset($content[$_GET['show']]))
    {
        echo $content[$_GET['show']];
    }
    elseif (isset($_GET['show']))
    {
        echo $content['error'];
    }
    else
    {
        echo $content['id00'];
    }
?>

:)

1 个答案:

答案 0 :(得分:1)

    $content = array(
        'id01'=>'sub_id01.php',
        'id02'=>'sub_id02.php'
    );
    if (isset($_GET['show']))
    {
        if (array_key_exists($_GET['show'], $content)) 
        { 
            //$_GET id is set and it exists in content
            include($content[$_GET['show']]);
        }
        else
        {
            //$_GET id is set but does not exist in content
            //include whatever page you have for a wrong id here
        } 
    }
    else 
    {
        //else no $_GET was set 
        //include default page
    }