I don't get this PHP error

This PHP error is driving me nuts. I have no idea what’s causing it.
Error: Parse error: syntax error, unexpected end of file in C:\Program Files (x86)\Ampps\www\includes\class-query.php on line 176

Here’s the code (line 155 - 176):

function do_inbox($user_id) {
    $message_objects = $this->get_message_objects($user_id);
    
    if (is_array($message_objects)){  
        foreach ( $message_objects as $message ) 
    {
      
    }?>
     <div class="status_item">
      <?php $user = $this->load_user_object($message->message_sender_id); ?>
      <h3>From: <a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
      <p><?php echo $message->message_subject; ?></p>
      <p><?php echo $message->message_content; ?></p>
     </div>
    <?php
    }
   }
  }
 
 
 $query = new query;
?>

I would say you have an extra } somewhere and it might not be on line 176. Without seeing everything above this it is a bit difficult to see which it is.

Try doing some indenting to help find it also aligning the opening { above the closing } can also help see the problem

On quick glance, this looks janky…you’ve got a foreach loop which doesn’t do anything? My guess is the closing bracket before the ?> isn’t supposed to be there…

if (is_array($message_objects)){  
    foreach ( $message_objects as $message ) 
{

}?>

That was my thought - the extra } is spurious, and if the parse error wasn’t there, it would throw errors in the merged php/html code for undefined objects or something similar. The parse error on the last line is usually a giveaway that something was opened and not closed, or closed but not opened.

1 Like

I’ve removed the extra bracket and made some other changes, and now I get this error:
“Parse error: syntax error, unexpected ‘?>’, expecting function (T_FUNCTION) in C:\Program Files (x86)\Ampps\www\includes\class-query.php on line 176”

Here’s the full code:

<?php
 require_once('class-db.php');

 if ( !class_exists('query') ) {
  class query {
   public function load_user_object($user_id) {
    global $db;
    
    $table = 's_users';
    
    $query = "
        SELECT * FROM $table
        WHERE ID = $user_id
       ";
    
    $obj = $db->select($query);
    
    if ( !$obj ) {
     return "No user found";
    }
    
    return $obj[0];
   }
   
   public function load_all_user_objects() {
    global $db;
    
    $table = 's_users';
    
    $query = "
        SELECT * FROM $table
       ";
    
    $obj = $db->select($query);
    
    if ( !$obj ) {
     return "No user found";
    }
    
    return $obj;
   }
   
   public function get_friends($user_id) {
    global $db;
    
    $table = 's_friends';
    
    $query = "
        SELECT ID, friend_id FROM $table
        WHERE user_id = '$user_id'
       ";
    
    $friends = $db->select($query);
    
   if (is_array($friends)){  
        foreach ( $friends as $friend ) 
    

     $friend_ids[] = $friend->friend_id;
    }
    
    
   }
   
   public function get_status_objects($user_id) {
    global $db;
    
    $table = 's_status';
    
    $friend_ids = $this->get_friends($user_id);
    
    if ( !empty ( $friend_ids ) ) {
     array_push($friend_ids, $user_id);
    } else {
     $friend_ids = array($user_id);
    }
    
    $accepted_ids = implode(', ', $friend_ids);
    
    $query = "
        SELECT * FROM $table
        WHERE user_id IN ($accepted_ids)
        ORDER BY status_time DESC
       ";
    
    $status_objects = $db->select($query);
    
    return $status_objects;
   }
   
   public function get_message_objects($user_id) {
    global $db;
    
    $table = 's_messages';
    
    $query = "
        SELECT * FROM $table
        WHERE message_recipient_id = '$user_id'
       ";
    
    $messages = $db->select($query);
        
    return $messages;
   }
   
   public function do_user_directory() {
$users = $this->load_all_user_objects(); // assuming FALSE is returned if no users

if( $users ) { 
   foreach ( $users as $user ) { 
      echo '<div class="directory_item">';
        echo '<h3><a href="profile-view.php?uid='   .$user->id .'">'   .$user->user_nicename   .'></a></h3>';
        echo '<p>'   .$user->user_email   .'</p>';
    echo '</div>';
  } // endforeach 

}else{ 
   echo 'Opportunity to resume with a graceful error message';
} // endforeach 
}// endfunc  do_user_directory()
   
    function do_friends_list($friends_array) {
     if (is_array($friends_array)){  
        foreach ( $friends_array as $friendid )
     $users[] = $thia2s->load_user_object($friend_id);
    }
        
   if (is_array($users)){  
        foreach ( $users as $user )
    {
       
    }
 ?>
     <div class="directory_item">
      <h3><a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
      <p><?php echo $user->user_email; ?></p>
     </div>
    <?php
    }
   }
   
    function do_news_feed($user_id) {
    $status_objects = $this->get_status_objects($user_id);
    
    foreach ( $status_objects as $status ) {?>
     <div class="status_item">
      <?php $user = $this->load_user_object($status->user_id); ?>
      <h3><a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
      <p><?php echo $status->status_content; ?></p>
     </div>
    <?php
    }
   }
   
    function do_inbox($user_id) {
    $message_objects = $this->get_message_objects($user_id);
    
    if (is_array($message_objects)){  
        foreach ( $message_objects as $message ) 
    {
      
    }?>
     <div class="status_item">
      <?php $user = $this->load_user_object($message->message_sender_id); ?>
      <h3>From: <a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
      <p><?php echo $message->message_subject; ?></p>
      <p><?php echo $message->message_content; ?></p>
     </div>
    <?php
    }
   }
  
?>

Adding and removing things in hopes it will make the code OK may work at rare times, but it isn’t the best way to do it. Instead of stumbling around blindly there are some things you can do so you can at least make best guesses even if you don’t understand what the code is doing.

First, find a coding style you like and try to be consistent using it in your code.
IMHO it doesn’t matter so much if you indent using tabs or spaces, have spaces after commas and around parentheses or not, whether curly braces are on their own lines or not. etc.
The key is find a style you like and stick with it.

Arguably the most useful habit you can establish is matched indentation. A bit of extra whitespace won’t affect code efficiency to any noticeable amount and will make your code much easier to read, troubleshoot and maintain.

The second thing is use a text editor / IDE that can help you.
There are many and there are probably as many favorites as there are those using them.
Some have more features, some less.

I use Notepad++ and if the code has a mess of nested parenthesis I can select one and it’s corresponding “partner” will highlight. It does the same thing for nested curly braces. (and HTML tags).

Here’s your code with what I believe are non-offenders (and empty lines, which isn’t a good idea to do. I only did it to condense the example) removed. Do you see the problem?

<?php
 require_once('class-db.php');
 if ( !class_exists('query') ) {
  class query {
   public function get_friends($user_id) {
    global $db;
    $table = 's_friends';
    $query = "
        SELECT ID, friend_id FROM $table
        WHERE user_id = '$user_id'
       ";
    $friends = $db->select($query);
   if (is_array($friends)){
        foreach ( $friends as $friend )
     $friend_ids[] = $friend->friend_id;
    }
    }
    function do_friends_list($friends_array) {
     if (is_array($friends_array)){
        foreach ( $friends_array as $friendid )
     $users[] = $thia2s->load_user_object($friend_id);
    }
   if (is_array($users)){
        foreach ( $users as $user )
    {
    }
?>
     <div class="directory_item">
      <h3><a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
      <p><?php echo $user->user_email; ?></p>
     </div>
<?php
    }
   }
    function do_inbox($user_id) {
    $message_objects = $this->get_message_objects($user_id);
    if (is_array($message_objects)){
        foreach ( $message_objects as $message )
    {
    }
?>
     <div class="status_item">
      <?php $user = $this->load_user_object($message->message_sender_id); ?>
      <h3>From: <a href="profile-view.php?uid=<?php echo $user->ID; ?>"><?php echo $user->user_nicename; ?></a></h3>
      <p><?php echo $message->message_subject; ?></p>
      <p><?php echo $message->message_content; ?></p>
     </div>
<?php
    }
   }
?>

Thanks for the organization help! After small tweaks, I solved the problem, but I’ve now encountered this one:
“Parse error: syntax error, unexpected ‘<’ in C:\Program Files (x86)\Ampps\www\includes\class-query.php on line 43”

I’ve attached the code in a Notepad++ file as a reference.class-query.php (1.5 KB)

Usually means you left out a ?> immediately before it.

Before it, I have “<?php," so adding "?>” before my div tag, so that wouldn’t be the solution.

Here’s the code (lines 42 - 51):

<?php
	 <div class="status_item">
      $user = $this->load_user_object($message->message_sender_id); 
      <h3>From: <a href="profile-view.php?uid=<?php echo $user->ID; ?>"> echo $user->user_nicename; </a></h3>
      <p> echo $message->message_subject; </p>
      <p> echo $message->message_content; </p>
     </div>
    }
   }
?>

Forum script is far easier to read if wrapped in three back-ticks.

Try this:

<?php
/**
 * John_Betong -Just formatting
 *
 * @package 
 * @subpackage 
 * @since 
*/

declare(strict_types=1); // PHP 7 ONLY

error_reporting(E_ALL);
ini_set('display_errors', 'true');

if( file_exists('class-db.php') ) {
  require_once('class-db.php');
}  

if ( !class_exists('query') ) {

class query {

public function get_friends($user_id)
{
  global $db;
  $table = 's_friends';
  $query = "
      SELECT ID, friend_id FROM $table
      WHERE user_id = '$user_id'
     ";
 
  $friends = $db->select($query);
  if (is_array($friends)) {
    foreach ( $friends as $friend ) {
       $friend_ids[] = $friend->friend_id;
    }
  }
}//endfunc

public function do_friends_list($friends_array)
{
  if (is_array($friends_array)) {
    foreach ( $friends_array as $friendid ) {
       $users[] = $thia2s->load_user_object($friend_id);
    }
  }//

 if (is_array($users)) {
    foreach ( $users as $user ) {
      //
    }
  }//    

  echo '<div class="directory_item">';
      echo '
        <h3>
          <a href="profile-view.php?uid=' .$user->ID  .'">'
            .$user->user_nicename
          .'</a>'
        .'</h3>';
    echo '<p>' .$user->user_email .'</p>';
   echo '</div>';
}//endfunc



public function do_inbox($user_id)
{
  $message_objects = $this->get_message_objects($user_id);
  if (is_array($message_objects)){
    foreach ( $message_objects as $message )
    {
      //
    }
  }  

  echo '<div class="status_item">';
    $user = $this->load_user_object($message->message_sender_id); 
    echo '<h3>From:
        <a href="profile-view.php?uid=' .$user->ID .'">'
          .$user->user_nicename
        .'</a></h3>';
      
      echo '<p>' .$message->message_subject .'</p>';
      echo '<p>' .$message->message_content .'</p>';
    echo '</div>';
}//endfunc

}//endclass query {

}//endif ( !class_exists('query') ) {

echo 'last-line: ' . __LINE__;

Edited - try again

Please note that it is not good practise to have HTML script inside your class functions.

Far easy to return $result; then use the class variable in your View:

// View.php

<?php 
require 'query.php';

$class = new query;

echo '<h1>' .$class->title .'</h1>';

echo '<div id="content">' .$class->content .'</div>';

echo '<div id="footer">' .$class->footer .'</div>';

echo '</body></html>';



The problem is you are what I call “going in and out of PHP”

It can get messy and many prefer to avoid doing it rather than deal with it.

But there are a few ways that work and aren’t all that bad if kept to a minimum

For example, you can wrap PHP tags around variables and echo them

	 <div class="status_item">
    <?php  $user = $this->load_user_object($message->message_sender_id);  ?>
      <h3>From: <a href="profile-view.php?uid=<?php echo $user->ID; ?> "> <?php echo $user->user_nicename; ?> </a></h3>
      <p> <?php echo $message->message_subject; ?></p>
      <p> <?php echo $message->message_content; ?></p>
     </div>

Or use heredoc syntax

<?php 
$user = $this->load_user_object($message->message_sender_id); 
$my_div = <<<EOHTML
	 <div class="status_item">
      <h3>From: <a href="profile-view.php?uid=$user->ID">$user->user_nicename</a></h3>
      <p>$message->message_subject</p>
      <p>$message->message_content</p>
     </div>
EOHTML;

echo $my_div;
?>

<?php <div

is not the start of a valid PGP command/

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.