0

Im learning PHP and wanted to ask with writing the following IF statements which is the more proper way it should done. Have been reading up online about it though for now cant find an answer.

In general also is it necessary to add an ENDIF also?

<?php if ( in_category( 'workshops' )) {
  get_template_part( 'template-parts/content', 'workshops' ); 
} ?>

OR

<?php if ( in_category( 'workshops' )) : ?>

<?php get_template_part( 'template-parts/content', 'services' ); ?>

<?php endif ?>
Bjarni
  • 103
  • 4
  • The former one is the preferred way by most devs. That way you don't drop in and out of PHP (well, you could do the latter one without doing that as well), and the content gets wrapped in brackets, and makes it easier to follow complex code. – junkfoodjunkie Oct 31 '16 at 00:16
  • Would it be correct to add an endif to that first one too or leave it out? – Bjarni Oct 31 '16 at 00:19
  • 1
    The first one should not have an `endif`, no. The `}` is the "endif" of that statement. – junkfoodjunkie Oct 31 '16 at 00:22
  • Okay, that is good to know, learnt something new today about the `}` which is the "endif" of to statement. Thanks. – Bjarni Oct 31 '16 at 00:25

0 Answers0