CodeBlanks Logo Icon CodeBlanks

Displaying the post list

PHP course · Intermediate · Lesson 68/115

Walk the posts array with foreach and display each post.

Walking an array with foreach

To hold several posts in one variable you use an array. With $posts = [new Post(...), new Post(...)]; you put two posts in the same array. Then foreach ($posts as $post) { ... } walks all elements in order and runs the loop body for each.

At each step the $post variable holds the current Post instance. So you can print each post's data with accesses like $post->title or $post->author. This pattern appears in every app that produces list output.

Summary output with the summary method

You add a summary(): string method to the Post class. It joins the title and author with $this->title . " - " . $this->author and returns a single string. So you don't repeat the same concatenation every time you print a post.

To call the method you write echo $post->summary();. The output looks like First post - Ayşe. The method reduces code repetition and lets you manage the post format from one place.

Exercise Add the summary method and a posts array

Add a public function summary(): string method to the Post class. Inside it write return $this->title . " - " . $this->author;. Then create a $posts array and add new Post("First post", "Hello world", "Ayşe") and new Post("Second post", "PHP is nice", "Alex"). Walk with foreach ($posts as $post) and print echo $post->summary() . "\n"; for each.

Show example solution
string { return $this->title . " - " . $this->author; } to the Post class. Write $posts = [ new Post("First post", "Hello world", "Ayşe"), new Post("Second post", "PHP is nice", "Alex"), ];. Print with foreach ($posts as $post) { echo $post->summary() . "\n"; }.
Example solution:
<?php
// Put several posts in an array and print with summary
class Post {
    public string $title = "";
    public string $content = "";
    public string $author = "";

    public function __construct(string $title, string $content, string $author) {
        $this->title = $title;
        $this->content = $content;
        $this->author = $author;
    }

    public function summary(): string {
        return $this->title . " - " . $this->author;
    }
}

$posts = [
    new Post("First post", "Hello world", "Ayşe"),
    new Post("Second post", "PHP is nice", "Alex"),
];

foreach ($posts as $post) {
    echo $post->summary() . "\n";
}

Test yourself

  1. What does a foreach loop do?

    • Only increments numbers by one
    • Defines a class
    • Reads a file
    • Walks through all elements of an array in order
    Show answer

    Walks through all elements of an array in order — Correct. foreach walks all elements of an array in order and runs the loop body for each.

  2. Complete the element part of the foreach loop

    foreach ($posts as ____) {
        echo $post->summary() . "\n";
    }
    Show answer
    foreach ($posts as $post) {
        echo $post->summary() . "\n";
    }
  3. Match the loop and array concepts

    foreach
    Loop construct
    $posts
    Posts array
    as $post
    Loop variable
    summary()
    Summary method

All lessons in Intermediate