views:

170

answers:

1

I have a php code that works well in PHP 5.2 but throwing "Parse error: syntax error, unexpected '}'" after upgrading to PHP 5.3.

When I use echo/print or heredocs to print all the HTML part of the code, the error is gone.

My question is, why this error occurred? Is this mean that in PHP 5.3 we are no longer allowed to put HTML code inside PHP code?

This is my php code:

function pk_stt2_admin_print_delete_form(){
 global $wpdb;
 if ( isset($_POST['delete_terms']) && !empty($_POST['delete_terms']) ){
  $msg = 'Search terms';
  $success = pk_stt2_db_delete_searchterms( $_POST['delete_terms'] );
 } elseif ( isset($_POST['delete_all']) ){
  $msg = 'All search terms';
  $success = pk_stt2_db_delete_searchterms( 'delete_all_terms' ); 
 } else {
  ?>
  <div id="message" class="updated fade">
   <p>        Please enter the search terms to be deleted, separate them with a comma.
   </p>
  </div>
  <?php 
 }
 if ( $success ){
  ?>
  <div id="message" class="updated fade">
   <p>        <?php echo $msg; ?> have been deleted,
    <?php echo $success; ?>        rows effected.
   </p>
  </div>
  <?php
    } else {
  ?>
  <div id="message" class="updated fade">
   <p>        Failed to delete search terms.
   </p>
  </div>
  <?php
 } 
}

Thanks.

+3  A: 

No, it means you shouldn't do it inside of functions, just like you wasn't supposed to in previous versions.

Don't close the php tag before closing a function ( or class ).

Kemo
IMO: any HTML output in a function is questionable. Keep your business logic and view-templating separate.
bobince
so what is the best approach to output the HTML code I use in the function above?
poer
@poer if you're asking me, put it in a whole different file and say else return require_once 'file.php'; since you're already doing it the procedural way
Kemo
ic, thanks kemo.
poer