WordPress parse error: syntax error on PHP 5.2.x

Here is a short WordPress troubleshooting tutorial for theme tweaking.

A theme I’m working on runs fine and was tested on PHP 4.3.x locally in my machine, I tried testing on PHP 5.2.x and I immediately encountered syntax error on WordPress. Traced the problem to be inside the Loop in WordPress. Sharing it here for those having hard time making their themes work on their host.



If you are getting these PHP Token errors like the following:

Parse error: syntax error, unexpected T_ENDWHILE

Parse error: syntax error, unexpected T_ELSE

Parse error: syntax error, unexpected T_ENDIF

Solution: check your if/else or while scripts and modify them accordingly to confirm to WordPress or your initial PHP tags.

Example script courtesy of undersigned.net, This code changes every other post to their own css tags. This runs fine and tested on PHP 4.3.x only

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();

$loopcounter++; ?>

<div class=“post”>

<?php if ($loopcounter % 2 == 0) { ?>

<h1 class=“even”><?php the_title(); ?></h1>

<?php } else { ?>

<h1 class=“odd”><?php the_title(); ?></h1>

<? } ?>

<?php the_content(); ?>

</div>

<?php endwhile; ?>

You used the if/else normal php codes inside the WordPress loop tag. Make a strict code by simply changing your if/else to WordPress tags to make it work on PHP 5.2.x.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();

$loopcounter++; ?>

<div class=“post”>

<?php if ($loopcounter % 2 == 0) : ?>

<h1 class=“even”><?php the_title(); ?></h1>

<?php else : ?>

<h1 class=“odd”><?php the_title(); ?></h1>

<? endif; ?>

<?php the_content(); ?>

</div>

<?php endwhile; ?>

That’s it, issue fixed! It should be similar to other scripts using similar coding style and not just to WordPress. Happy theme tweaking!

2 thoughts on “WordPress parse error: syntax error on PHP 5.2.x

  1. Hi,

    Thanks for this wordpress troubleshooting turorial, I haven’t encountered this issue yet but it might be useful for me in the future.

    Thanks!!

Leave a Reply

Your email address will not be published. Required fields are marked *