tags:

views:

133

answers:

3

I was browsing a worpress site, and spotted this line

<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(1) ) : else : ?>
    <li id="recent-posts">
        <ul>
            <?php get_archives('postbypost', 5); ?>
        <ul>
    </li>
<?php endif; ?>

What do the colon before and after else do exactly?? How does this thing work?

+10  A: 

That function will only execute dynamic_sidebar if it's already declared. The colons are PHP's alternate syntax for control structures. They're meant to be used in templates/views.

In this case, it looks like the if has an empty body and it's only used to call dyanamic_sidebar if it exists, since the call to dynamic_sidebar(1) will not occur if the first boolean check fails.

else will output anything between itself and the <?php endif; ?>. In this case, it would fire when the function dynamic_sidebar does not exist or if dyanmic_sidebar(1) does not return true.

ryeguy
u r right. I've updated the code.
detj
+4  A: 

It is an alternative Syntax for control structure.

It means:

 <?php 
  if (function_exists('dynamic_sidebar') && dynamic_sidebar(1)) {
  } else {
 ?>
    <li id="recent-posts">
       <ul>
        <?php get_archives('postbypost', 5); ?>
       <ul>
    </li>
 <?php
  }
  ?>
Eineki
+1  A: 

The dynamic_sidebar function in Wordpress, when called, will display the sidebar with the id of the number that is passed in (in this case, it is one). The snippet of code will print out that sidebar (if it exists, and the function dynamic_sidebar is defined), otherwise, it will print out whatever is below the code you posted all the way up to the line in the place of the sidebar.

Timothy Armstrong