Php beginners function and arrrays

Hi!, I am beginner . I have apache2 server. But the code doesn’t generate output in the browser.

Welcome page

<?php $books = [ [ 'name' => 'Do Androids Dream of Electric Sheep', 'author'=> 'Philip K. Dick', 'releaseYear' => 1964, 'purchaseUrl' => 'https://example.com', ],
                [
                    'name' => 'Project Hail Merry',
                    'author'=> 'Andy Weir',
                    'releaseYear' => 2021,
                    'purchaseUrl' => 'https://example.com',
                ],
                
                [

                    'name' => 'The Martian',
                    'author'=> 'Andy Weir',
                    'releaseYear' => 2011,
                    'purchaseUrl' => 'https://example.com',
                ],

            ];
            
    
            function filterByAuthor($books, $author) {
                $filteredBooks = [];
            
                foreach ($books as $book) {
                    if ($book['author'] === $author) {
                        $filteredBooks[] = $book;
                    }
                }
            
                return $filteredBooks;
            }
            
           
            
         ?>

        <ul>
            <?php foreach ($filterByAuthor($books, 'Andy Weir') as $book) : ?>
                    <li>
                        <a href="<?= $book['purchaseUrl'] ?>">
                            <?= $book['name']; ?> (<?= $book['releaseYear'] ?>)-BY <?= $book['author'] ?>
                        </a>
                    </li>
            
            <?php endforeach; ?>

        </ul>

Hi,

Did you start apache server before running your php scripts. Because this problem seems in these cases:

  1. You haven’t started Apache server
  2. Your file extension is not in .php
  3. PHP Web server installation may be corrupted.

May be this answer will help you to understand and troubleshoot.