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?
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
.
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
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With