What does this simple php code do?

I was browsing a WordPress site and noticed 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 exactly does the colon do before and after the other? How it works?

+4
source share
3 answers

This function will only perform dynamic_sidebar if it is already declared. Colons are PHP alternative syntax for control structures . They are intended for use in templates / views.

In this case, it looks like if has an empty body, and it is only used to call dyanamic_sidebar if it exists, since the dynamic_sidebar(1) call will not be executed if the first logical check fails.

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

+10
source

This is an alternative syntax for a 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 } ?> 
+4
source

When called, the dynamic_sidebar function in Wordpress displays a sidebar with the identifier of the number that is transmitted (in this case, it is one). The code snippet will print this sidebar (if it exists, and the dynamic_sidebar function is defined), otherwise it will print everything below the code that you placed, down to the line located on the sidebar.

+1
source

Source: https://habr.com/ru/post/1303819/


All Articles