I don't understand why the if statement does not have {}

I have the following line in a wordpress function if(empty($paged)) $paged = 1;

and I don't understand why it doesn't have an open {

and close }

. It's opening and closing itself, not sure if it's 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";
     }
}

?>

      

+3


source to share


3 answers


If the code you want to run after the if

conditional is only one line, you can omit the parentheses. I find this confusing, so I don't, but it's valid PHP .

So what your code does:

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

      

So it will be:

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

      

It won't:



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

      

And it won't:

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

      

$othervar

, in both cases will "execute" outside the conditional - ie. will always be set to 2.

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

+7


source


The answer has already been accepted, but if anyone would like more details about the operators,

The simplest form of the operator if

is as follows (no lists else

or elseif

):

if ( expression )
     statement

Expressions are pretty simple: anything that evaluates to a 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 expression at the end ;

.

Statements

The key to understanding is the statement. From your description, you would expect the statement to be some kind of code surrounded by curly braces, for example.

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

      

However, this is just one type of statement called a compound expression or block statement. A compound statement consists of {

, followed by a list of statements and then a terminating statement }

. As an aside, since compound statements are also statements, you can insert compound statements inside compound statements, but you usually don't see compound statements on their own (they usually bind to other statements like bodies if

and while

).

Expression operators



Note that $x = 2;

and $y = 123;

are also operators! These types of operators are called expression expressions and are defined as an expression followed by ;

. Using the expression examples above, the following expressions are valid:

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

      

Wherever you can use an operator, you can use a compound operator or an expression operator.

Other types of operators

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

  • while

    /do

  • for

    /foreach

  • switch

  • break

  • continue

  • return

  • if

  • ... and much more

Anywhere you can use an operator, you can also use one of the above. Or you can use a compound statement containing some combination of these. Therefore, going back to the original example of operators if

, they are all valid because the operator is if

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 still valid because at the end of the day they are all just statements.

+2


source


It just says that if the $ paged global variable is empty, set it to default: 1.

Sometimes you don't need to introduce open and close {} when the if action will be just one line or block, like while, etc. brackets are optional.

So these examples are valid:

if(condition) one line; 

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

      

Anyway, I do think that omitting the curly braces is bad practice, you can find the answer here .

+1


source







All Articles