Php Don';不理解为什么if语句没有{}

Php Don';不理解为什么if语句没有{},php,if-statement,conditional,Php,If Statement,Conditional,我在wordpressif(empty($paged))$paged=1的函数中有下面一行

我在wordpress
if(empty($paged))$paged=1的函数中有下面一行{
和结尾
}
。它是自动打开和关闭的,甚至不确定这是否是一个术语

这是我正在使用的函数

<?php
function kriesi_pagination($pages = '', $range = 2)
{  
     $showitems = ($range * 2)+1;  

     global $paged;
     if(empty($paged)) $paged = 1;

     if($pages == '')
     {
         global $wp_query;
         $pages = $wp_query->max_num_pages;
         if(!$pages)
         {
             $pages = 1;
         }
     }   

     if(1 != $pages)
     {
         echo "<div class='pagination'>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo;</a>";

         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
             }
         }

         if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>&raquo;</a>";
         echo "</div>\n";
     }
}

?>

只是说,如果全局变量$paged为空,则将其设置为默认值:1

有时,当if的操作只有一行或一个块时,比如for、while等,括号不是强制性的,您不需要放置开始和结束{}

因此,这些例子是有效的:

if(condition) one line; 

if(condition) 
    while {
        //several lines; 
    }

无论如何,我真的认为省略“花括号”是一种不好的做法,你可以找到答案。

如果你想在
条件后运行的代码只有一行,你可以省略括号。我觉得很困惑,所以我不做,但是

那么,您的代码的作用是:

if(empty($paged)) $paged = 1;
这也会:

if(empty($paged)) 
    $paged = 1; // semi-colon marks the end of the line
if(empty($paged)) 
   $paged = 1; 
   $othervar = 2;
这不会:

if(empty($paged)) $paged = 1; $othervar = 2;
这也不会:

if(empty($paged)) 
    $paged = 1; // semi-colon marks the end of the line
if(empty($paged)) 
   $paged = 1; 
   $othervar = 2;
$othervar
在这两种情况下都将在条件之外“执行”——即,它将始终设置为2


这里还有另一条线索:

答案已经被接受,但如果有人想了解更多关于陈述的细节

if
语句的最简单形式如下(没有
else
elseif
列表):

但是,这只是一种类型的语句,称为复合语句或块语句。复合语句包括一个
{
,后面是语句列表,然后是一个终止的
}
。另一方面,由于复合语句也是语句,因此可以将复合语句嵌套在复合语句中,但通常不会单独看到复合语句(它们通常附加到其他语句,例如
if
while
语句的主体)

表达式语句 请注意,
$x=2
$y=123也是语句!这些类型的语句称为表达式语句,定义为后跟
的表达式。使用上述表达式示例,以下是有效的表达式语句:

$x = 2;
f(123);
3; // This is a fairly useless statement, but yes, it counts as one
在任何可以使用语句的地方,都可以使用复合语句或表达式语句

其他类型的陈述 您可能已经熟悉其他类型的语句:

  • while
    /
    do
  • for
    /
    foreach
  • 开关
  • break
  • 继续
  • 返回
  • if
  • ……还有更多
在任何可以使用语句的地方,也可以使用上述语句之一。或者,您可以使用包含它们的一些组合的复合语句。因此,回到
if
语句的原始示例,以下语句都是有效的,因为
if
语句后面跟着另一个语句:

if (123)         // If statement
    $x = 3;      // Expression statement

if ($a == $b)    // If statement
{                // Beginning of compound statement
    $z = 1;      // Expression statement
    {            // Beginning of compound statement
        5;       // Expression statement
    }            // End of compound statement
}                // End of compound statement

if ($z)          // If statement
    while (true) // While statement
        break;   // Break statement

if ($a)          // If statement
    if ($b)      // If statement
        if ($c)  // If statement
        {        // Beginning of compound statement
            f(); // Expression statement
        }        // End of compound statement

请注意,其中一些是糟糕的风格或没有任何用处,但仍然是有效的,因为归根结底,它们都只是语句。

+1-我还发现,如果编写代码时没有大括号,那么就不那么清楚了。尤其是当我的同事也不知道如何缩进的时候!