3

I have the following line in a function in wordpress if(empty($paged)) $paged = 1; and I don't understand why it doesn't have an opening { and a closing }. Is it self opening and closing, not sure if that is even a term

Here is the function I am working with

<?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";
     }
}

?>
mhatch
  • 4,441
  • 6
  • 36
  • 62
Cool Guy Yo
  • 5,910
  • 14
  • 59
  • 89

3 Answers3

7

If the code you want to run after the if conditional is only one line you can omit the brackets. I find it confusing, so I don't do it but it is valid PHP.

So, what your code does works:

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

So would this:

if(empty($paged)) 
    $paged = 1; // semi-colon marks the end of the line

This would not:

if(empty($paged)) $paged = 1; $othervar = 2;

Nor would this:

if(empty($paged)) 
   $paged = 1; 
   $othervar = 2;

$othervar, in both cases, would "execute" outside the conditional-- ie., it would always be set to 2.

There is another thread here: PHP - If/else, for, foreach, while - without curly braces?

Community
  • 1
  • 1
s_ha_dum
  • 2,840
  • 2
  • 18
  • 23
  • +1 - I also find it much less clear when code is written without braces. Especially when my colleagues don't know how to indent, too! – Bojangles Feb 09 '13 at 20:03
2

An answer was already accepted, but in case anyone wanted some more details about statements,

The simplest form of an if statement is the following (without else or elseif lists):

if (expression)
    statement

Expressions are fairly straightforward: anything that evaluates to some value is an expression. Examples of expressions include: assignments ($x = 2), function calls (f(123)), and even constant values (123). Note that there is no ; at the end of an expression.

Statements

The key thing to understand is what a statement is. From your description, you would expect a statement to be some code surrounded by curly braces, e.g.

{ 
    $x = 2; 
    $y = 123;
    // Other statements
}

However, this is only one type of statement, known as a compound statement or block statement. A compound statement consists of a {, followed by a list of statements, and then a terminating }. As an aside, since compound statements are also statements, you can nest compound statements inside compound statements, but you don't usually see compound statements by themselves (they are usually attached to other statements such as the bodies of if and while statements).

Expression statements

Note that $x = 2; and $y = 123; are also statements! These types of statements are known as expression statements, and are defined as an expression followed by a ;. Using the expression examples above, the following are valid expression statements:

$x = 2;
f(123);
3; // This is a fairly useless statement, but yes, it counts as one

Anywhere you can use a statement, you can use a compound statement or an expression statement.

Other types of statements

There are other types of statements that you are probably already familiar with:

  • while / do
  • for / foreach
  • switch
  • break
  • continue
  • return
  • if
  • ...and more

Anywhere you can use a statement, you can use one of the above too. Or you can use a compound statement that contains some combination of them. So going back to the original example of if statements, the following are all valid, because the if statement is followed by another statement:

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

Note that some of these are bad style or don't do anything useful, but are nonetheless valid because at the end of the day, they are all just statements.

Community
  • 1
  • 1
Walfie
  • 3,376
  • 4
  • 32
  • 38
1

It is just saying that if the global variable $paged is empty set it to a default value: 1.

Sometimes you don't need to put the opening and closing {}, when the action of the if is going to be only one line or block like for, while, etc the brackets are not mandatory.

So these examples are valid:

if(condition) one line; 

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

Anyway, I really think that omitting "curly brackets" is a bad practice, you can find the answer here.

Community
  • 1
  • 1
NewRehtse
  • 338
  • 1
  • 5
  • 15