Syntax error, unexpected T_RETURN, waiting for T_FUNCTION oop php

Im getting the error as above. This applies to my return application. Does anyone have any clues? Thanks for the help! Hello!

    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;                                          
+3
source share
2 answers

Your return statement should appear before the last closing bracket.

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

        return $posts;                                          
    }
+10
source

Your operator returnmust be inside the function getPosts(). It is currently outside or you have }the wrong line.

+3
source

Source: https://habr.com/ru/post/1763781/


All Articles