PHP Arrays

·

1 min read

An array is like a folder, we can take a group or list of things and put it in the array, if we want to represent a list of our favorite books, for example, we can use an array.

To loop over the array of books we use the foreach function :

The result should be :

We can also write the foreach in another manner :

   <?php foreach($books as $book): ?>
     <li><?= $book   ?></li>
    <?php endforeach; ?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>Demo</title>
</head>
<body>

<h1>Recommended Books</h1>
<?php
$books=[
        "Do Androids Dream of Electric Sheep",
         "The Langoliers",
         "Hail Mary"

]
?>

<ul>
   <?php foreach($books as $book): ?>
     <li><?= $book   ?></li>
    <?php endforeach; ?>
</ul>

</body>
</html>