Okay I've got a straightforward question. Why doesn't this work?

	<?php if (isset($_GET['s'])) { ?>
	<li class="selected"><a href="<?php the_permalink(); ?>">special page</a></li>
	<?php } ?>

<?php elseif (isset($_GET['show'])) { ?>
<li><a href="<?php the_permalink(); ?>">article</a></li>
<li class="selected"><a href="?show=comments">discussion</a></li>
<li class="menu-break"><?php edit_post_link('edit this page'); ?></li>
<?php } ?>

<?php else { ?>
<li class="selected"><a href="<?php the_permalink(); ?>">article</a></li>			
<li><a href="?show=comments">discussion</a></li>
<li class="menu-break"><?php edit_post_link('edit this page'); ?></li>
<?php } ?>
    	<?php if (isset($_GET['s'])) { ?>
    	<li class="selected"><a href="<?php the_permalink(); ?>">special page</a></li>
    	<?php } elseif (isset($_GET['show'])) { ?>
    	<li><a href="<?php the_permalink(); ?>">article</a></li>
    	<li class="selected"><a href="?show=comments">discussion</a></li>
    	<li class="menu-break"><?php edit_post_link('edit this page'); ?></li>
    	<?php } else { ?>
    	<li class="selected"><a href="<?php the_permalink(); ?>">article</a></li>			
    	<li><a href="?show=comments">discussion</a></li>
    	<li class="menu-break"><?php edit_post_link('edit this page'); ?></li>
    	<?php } ?>
    

    that works, not found of the syntax but its your call

      Don't have a closing ?> tag after the end of an if/else block and then repoen with <?php before the following elseif/else, as that is interpreted as output between the two blocks (even if it's just white-space, it's still output).

      <?php
      if (isset($_GET['s']))
      {
      ?>
         <li class="selected"><a href="<?php the_permalink(); ?>">special page</a></li>
      <?php
      }
      elseif (isset($_GET['show']))
      {
      ?>
         <li><a href="<?php the_permalink();?>">article</a></li>
         <li class="selected"><a href="?show=comments">discussion</a></li>
         <li class="menu-break"><?php edit_post_link('edit this page');?></li>
      <?php
      } else
      {
      ?>
         <li class="selected"><a href="<?php the_permalink(); ?>">article</a></li>
         <li><a href="?show=comments">discussion</a></li>
         <li class="menu-break"><?php edit_post_link('edit this page'); ?></li>
      <?php
      }
      ?>
      

        Thanks a lot. If you've got a better way to write it by the way, I'm totally open to it. I just needed this to work.

          One alternative:

          <?php
          if(isset($_GET['s']))
          {
             printf(
                "<li class='selected'><a href='%s'>special page</a></li>\n",
                the_permalink()
             );
          }
          elseif(isset($_GET['show']))
          {
             printf(
                "<li><a href='%s'>article</a></li>\n" .
                "<li class='selected'><a href='?show=comments'>discussion</a></li>\n" .
                "<li class='menu-break'>%s</li>\n",
                the_permalink(),
                edit_post_link('edit this page')
             );
          }
          else
          {
             printf(
                "<li class='selected'><a href='%s'>article</a></li>\n" .
                "<li><a href='?show=comments'>discussion</a></li>\n" .
                "<li class='menu-break'>%s</li>\n",
                the_permalink(),
                edit_post_link('edit this page')
             );
          }
          ?>
          

          Personally, I find something like that easier to read and maintain as opposed to all that in-and-out of PHP mode; but your mileage may vary.

            I get it. It keeps everything in a single set of tags and keeps me from having to bounce in and out of php. Is there a particular reason why that's better than my method? Or is it just personal preference?

              As far as how PHP processes it, there's probably no reason one is necessarily better than the other. If you did some sort of metering you might find out that one saves a microsecond or two, but it really comes down to which saves you the most time, remembering that the time includes debugging plus future maintenance time when you come back to the code a year later and need to figure out what it's doing.

              I personally find code that goes in and out of PHP mode much harder to read and maintain. Ultimately you want to get to the point where you can separate the HTML output as much as possible from the control and business logic, which is one of the goals of design patterns such as MVC or N-Tier.

                Write a Reply...