Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this simple php code do?

Tags:

php

wordpress

I was browsing a WordPress 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?

like image 313
detj Avatar asked Dec 07 '22 04:12

detj


2 Answers

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.

like image 139
ryeguy Avatar answered Dec 26 '22 18:12

ryeguy


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
  }
  ?>
like image 23
Eineki Avatar answered Dec 26 '22 17:12

Eineki