Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
If-else条件在PHP中给了我错误_Php_Session Variables - Fatal编程技术网

If-else条件在PHP中给了我错误

If-else条件在PHP中给了我错误,php,session-variables,Php,Session Variables,我有两个网站,每个都有自己的域名。在加载时,我执行了一个php文件,该文件在两个站点上都使用,并且具有相同的功能 以下是该文件的代码: <?php echo '1'; if ($_SESSION["template"]=="template1";) { echo '2'; if ($_REQUEST["cmdAction"]=='A') echo file_get_contents('http://localhost/images/templat

我有两个网站,每个都有自己的域名。在加载时,我执行了一个php文件,该文件在两个站点上都使用,并且具有相同的功能

以下是该文件的代码:

<?php
echo    '1';
if ($_SESSION["template"]=="template1";)
{
    echo    '2';
    if ($_REQUEST["cmdAction"]=='A')
        echo file_get_contents('http://localhost/images/template1/a.php'); 
    else if ($_REQUEST["cmdAction"]=='B')
        echo file_get_contents('http://localhost/images/template1/b.php');
    else if ($_REQUEST["cmdAction"]=='C')
        echo file_get_contents('http://localhost/images/template1/c.php');
}

else if ($_SESSION["template"]=="template2";)
{
    echo    '3';
    if ($_REQUEST["cmdAction"]=='A')
        echo file_get_contents('http://localhost/images/template2/a.php'); 
    else if ($_REQUEST["cmdAction"]=='B')
        echo file_get_contents('http://localhost/images/template2/b.php');
    else if ($_REQUEST["cmdAction"]=='C')
        echo file_get_contents('http://localhost/images/template2/c.php');
}
else {

echo 'NO DATA';
}
    echo    '4';

?>

在这两个站点中的每一个站点上,我都设置了一个会话变量,但在上面的代码中,它似乎并不像我期望的那样工作


我遗漏了什么吗?

if()
else-if()
语句中删除分号,在使用嵌套的if-else时也添加括号,因为它使人更容易理解,看起来也更好

    <?php
    echo    '1';
    if ($_SESSION["template"]=="template1") 
    {
        echo    '2';
        if ($_REQUEST["cmdAction"]=='A')
        {
            echo file_get_contents('http://localhost/images/template1/a.php'); 
        } 
        else if ($_REQUEST["cmdAction"]=='B') 
        {
            echo file_get_contents('http://localhost/images/template1/b.php');
        } 
        else if { ($_REQUEST["cmdAction"]=='C') 
        {
            echo file_get_contents('http://localhost/images/template1/c.php');
        }
    } 
    else if ($_SESSION["template"]=="template2") 
    {
        echo    '3';
        if ($_REQUEST["cmdAction"]=='A') 
        {
            echo file_get_contents('http://localhost/images/template2/a.php'); 
        } 
        else if ($_REQUEST["cmdAction"]=='B') 
        {
            echo file_get_contents('http://localhost/images/template2/b.php');
        } 
        else if ($_REQUEST["cmdAction"]=='C') 
        {
            echo file_get_contents('http://localhost/images/template2/c.php');
        }
    }
    else {
        echo 'NO DATA';
    }
    echo    '4';
?>

if()
else-if()
语句中删除分号,在使用嵌套的if-else语句时也添加括号,因为它使人更容易理解,看起来也更好

    <?php
    echo    '1';
    if ($_SESSION["template"]=="template1") 
    {
        echo    '2';
        if ($_REQUEST["cmdAction"]=='A')
        {
            echo file_get_contents('http://localhost/images/template1/a.php'); 
        } 
        else if ($_REQUEST["cmdAction"]=='B') 
        {
            echo file_get_contents('http://localhost/images/template1/b.php');
        } 
        else if { ($_REQUEST["cmdAction"]=='C') 
        {
            echo file_get_contents('http://localhost/images/template1/c.php');
        }
    } 
    else if ($_SESSION["template"]=="template2") 
    {
        echo    '3';
        if ($_REQUEST["cmdAction"]=='A') 
        {
            echo file_get_contents('http://localhost/images/template2/a.php'); 
        } 
        else if ($_REQUEST["cmdAction"]=='B') 
        {
            echo file_get_contents('http://localhost/images/template2/b.php');
        } 
        else if ($_REQUEST["cmdAction"]=='C') 
        {
            echo file_get_contents('http://localhost/images/template2/c.php');
        }
    }
    else {
        echo 'NO DATA';
    }
    echo    '4';
?>

查看您的代码后,我编辑了我的答案。下面的代码与您的代码完全相同,但需要的代码要少得多

<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
    echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php'); 
} else {
    echo 'NO DATA';
}
?>

查看您的代码后,我编辑了我的答案。下面的代码与您的代码完全相同,但需要的代码要少得多

<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
    echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php'); 
} else {
    echo 'NO DATA';
}
?>


从if和else if括号中删除分号,并检查会话变量的值。整个过程可以通过
echo file\u get\u contents('http://localhost/images/'.$_SESSION[“template”]./'.strtolower($_REQUEST[“cmdAction”])..php')。另外还需要
isset($\u SESSION[“template”])
&
isset($\u REQUEST[“cmdAction”])
。从if和else if括号中删除分号并检查SESSION变量的值。整个过程可以通过
echo file\u get\u contents('http://localhost/images/“.$”会话[“模板”]./”.strtolower($\请求[“cmdAction”])"php";。此外,还需要
isset($\u会话[“模板”])
&
isset($\u请求[“cmdAction”])