syntax error, unexpected T_RETURN, expecting T_FUNCTION oop php

21,454

Solution 1

Your return statement should come before the last closing brace.

        while($posts = $result->fetch_assoc()) {
            array_push($posts, new Post($post['id'], $post['created'], $post['author'], $post['title'], $post['body']));      
        }

        return $posts;                                          
    }

Solution 2

Your return statement needs to be inside the function getPosts(). Currently it is outside or you have one } on the wrong line.

Share:
21,454
Tim
Author by

Tim

Updated on July 18, 2022

Comments

  • Tim
    Tim almost 2 years

    Im receiving an error as stated above. Its referring to my return statement. Any one got any clues on this?! Thankful for all help! Regards!

    public function getPosts() {
        $result = $this->db->query("SELECT * FROM posts");
    
        $posts = array();
        while($posts = $result->fetch_assoc()) {
            array_push($posts, new Post($post['id'], $post['created'], $post['author'], $post['title'], $post['body']));      
        }
    
    } 
    return $posts;                                          
    
  • Tim
    Tim over 13 years
    thanks for the quick answers. I found the answer just after I posted the question :) a bit to quick on my finger I reckon. Thanks!
  • OM The Eternity
    OM The Eternity over 13 years
    @Pekka the potter get scared of unicorn!!! :P