Php – Parse error: syntax error, unexpected ‘?>’, expecting function (T_FUNCTION) or const (T_CONST)

php

I keep getting the error

Parse error: syntax error, unexpected '?>', expecting function
(T_FUNCTION) or const (T_CONST)

From what I read this usually points to a missing "}", but everything seems to match up no matter how many times I read over it. I also tried removing one of the "}" in case I had one too many, but it came back with the error `

Parse error: syntax error, unexpected end of file

`.

Here's my code:

<?php
class Post{ 
     private $user_obj;
     private $con;

     public function __construct($con, $user){
            $this->con = $con;
            $this->user_obj = new User($con, $user);
     }


     public function submitPost($body, $user_to) {
         $body = strip_tags($body); //removes any HTML tags             
         $body = mysqli_real_escape_string($this->con, $body); //Allows the use of " ' " so it doesn't think it's a new string. 
         $check_empty = preg_replace('/\s+/', '', $body); //Deletes all spaces

         if($check_empty != "") {


             //Current date and time
             $date_added = date("Y-m-d H:i:s");
             //Get username
             $added_by = $this->user_obj->getUsername();

             //if user is on their own profile, user_to is 'none'
             if($user_to == $added_by) {
                 $user_to = "none";
             }

             //insert post
            $query = mysqli_query($this->con, "INSERT INTO posts VALUES('', 
            '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', 
            '0')");

             $returned_id = mysqli_insert_id($this->con);

             //Insert notification

             //Update post count for user
             $num_posts = $this->user_obj->getNumPosts();
             $num_posts++;
             $update_query = mysqli_query($this->con, "UPDATE users SET 
             num_posts='$num_posts' WHERE username='$added_by'");
         }
     }
?>

Best Solution

You failed to close the class Try this

    <?php
class Post{ 
     private $user_obj;
     private $con;

     public function __construct($con, $user){
            $this->con = $con;
            $this->user_obj = new User($con, $user);
}


     public function submitPost($body, $user_to) {
         $body = strip_tags($body); //removes any HTML tags
         $body = mysqli_real_escape_string($this->con, $body); //Allows the 
         use of " ' " so it doesn't think it's a new string.
         $check_empty = preg_replace('/\s+/', '', $body); //Deletes all 
         spaces

         if($check_empty != "") {


             //Current date and time
             $date_added = date("Y-m-d H:i:s");
             //Get username
             $added_by = $this->user_obj->getUsername();

             //if user is on their own profile, user_to is 'none'
             if($user_to == $added_by) {
                 $user_to = "none";
             }

             //insert post
            $query = mysqli_query($this->con, "INSERT INTO posts VALUES('', 
            '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', 
            '0')");

             $returned_id = mysqli_insert_id($this->con);

             //Insert notification

             //Update post count for user
             $num_posts = $this->user_obj->getNumPosts();
             $num_posts++;
             $update_query = mysqli_query($this->con, "UPDATE users SET 
             num_posts='$num_posts' WHERE username='$added_by'");
             }
    }
}
    ?>
Related Question