PHP - 嵌套If语句问题

时间:2011-02-23 01:21:01

标签: php wordpress loops if-statement

我正在为我朋友的公司开发Wordpress主题,但这个问题都是关于PHP所以写在这里,在SO上。

我有两个侧边栏,就是我们在Wordpress中显示侧边栏的方式:

 if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('SidebarName') ) :  
 endif;    

所以这是一个if语句。

好的,现在,我想要显示一个侧边栏IF $ layout =“one”TWO sidebars IF $ layout ==“two”等。

此代码有效,但它重复了侧边栏内容我认为endifs正在弄乱父循环或类似的东西:

 if($layout="one") { 

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 1') ) :  
      endif; 

 } elseif($layout=="two") {

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(' Sidebar 1') ) :  
      endif;    

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 2') ) :  
      endif;     

 } elseif($layout=="three") { (...)

如何解决这个问题?当我只删除主if循环时 - 一切都像预期的那样工作,所以我确定我在上面某处丢失了。

抱歉,错别字,我正在使用$ layout ==而不是=

5 个答案:

答案 0 :(得分:1)

编辑:我刚做了一个测试,看起来这个答案是错误的。我能够将结果和大括号混合使用if和while语法。但是,手册确实说明了:

  

'注意:混合语法相同   不支持控制块。'

所以我对此感到有点困惑。


PHP If语句有两种类型的语法,一种带括号,另一种带冒号。

if () {

}

if ():

endif;

对于while,for,foreach等也有类似的语法。但是不允许将大括号与冒号语法混合使用。因此,您需要将代码更改为

 if($layout=="one") { 

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 1') ) {
      }

 } elseif($layout=="two") {

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(' Sidebar 1') ) {
      }    

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 2') ) {
      }     

 } elseif($layout=="three") { (...)

 if($layout=="one"):

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 1') ) :  
      endif; 

 elseif($layout=="two"):

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(' Sidebar 1') ) :  
      endif;    

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 2') ) :  
      endif;     

 elseif($layout=="three"):
  (...)

 endif;

您可以在http://php.net/manual/en/control-structures.alternative-syntax.php

了解更多相关信息

编辑:我多么愚蠢,我没注意到这一点。像其他答案一样,你使用了一个等号而不是两个来测试相等。

答案 1 :(得分:1)

没有“if循环”这样的东西。 :)

由于您基本上只是一次又一次地重复相同的条件,您可以在逻辑上将其重组为:

if (function_exists('dynamic_sidebar')) {
    switch ($layout) {
        case 'one' :
            dynamic_sidebar('Sidebar 1');
            break;
        case 'two' :
            dynamic_sidebar('Sidebar 1');
            dynamic_sidebar('Sidebar 2');
            break;
        case ...
    }
}

这也应该处理导致脚本行为异常的语法/拼写错误。

答案 2 :(得分:0)

看起来你的意思是使用等号运算符==,你正在使用赋值运算符=

if ($layout == "one")
// etc

顺便提一下,还要考虑使用更常见的if/else PHP语法。而不是使用elseifendif使用此语法。根据您的惯例和偏好支持。

if (condition)
{
  // code for condition
}
else if (condition)
{
  // code
}
else
{
  // else case code
}

答案 3 :(得分:0)

我同意Michael的观点,而且我不确定你的功能是如何设置的,但通常我会看到更类似于

的内容
if($layout == "one" && function_exists('dynamic_sidebar'))
{
    dynamic_sidebar('Sidebar 1');
}

答案 4 :(得分:0)

首先,这是错误的:

if ($layout="one")

始终在==语句中使用===ifelseif相同),因为=实际上会分配该值。这可能是造成你的错误的一部分。

更好的是,在这种特殊情况下,请使用switch statement

第三,您不必继续复制!function_exists('dynamic_sidebar')只需将其复制一次即可。它应该是这样的:

if function_exists('dynamic_sidebar')
{
    switch ($layout)
    {
        case 'one':
        dynamic_sidebar('Sidebar 1');
        break;

        case 'two':
        dynamic_sidebar('Sidebar 1');
        dynamic_sidebar('Sidebar 2');
        break;

        // ...
    }
}

更好的是,将整个事物更改为数字,然后执行for循环:

if function_exists('dynamic_sidebar')
{
    for ($i = 0; $i < $sidebarCount; ++$i)
    {
        dynamic_sidebar("Sidebar $i");
    }
}
相关问题