Calling functions with return values in PHP

We have our function defined, so our next step is to call it. Calling a function will execute the code within that function.

Let’s create a new $posts variable after our title, and set it equal to our function name. We execute the function by calling it with a set of open & close parenthesis.

When we try to execute this code, we will see we receive a fatal PHP error. If we read it through, it will say argument 1 must be of type Countable, pipe, array. The pipe symbol stands for “or”, so it is expecting the argument to be countable or array, and that null was given. It also gives us exactly where this error is occuring: in the index.php file, on line 22. If we check line 22, we will see that this is where we are calling the count() function. Since the error is saying teh argument type is null, that is essentially telling us that $posts on this line is null.

How can that be? Well, we created the function and it executed the code within it, but since we cannot access the function within it, a null response is returned, which means nothing.

In order to get access to the result of a function call, that function must return some sort of value, and it can do this with the return keyword.

After this $posts variable, let’s go ahead and type return, followed by the data we want to return. This would be the $posts variable, which is equal to an array of data.

When we now re-execute our code, we will see that we get our desired result. Let’s go ahead and remove this break statement within the foreach loop, so we can confirm that all of the data within this function is returned, and in fact it is.

<?php
function getPosts()
{
    $posts = [
        [
            'title' => 'How to learn PHP',
            'content' => 'This is how you learn PHP.',
        ],
        [
            'title' => 'How to learn MySQL',
            'content' => 'This is how you learn MySQL.',
        ],
        [
            'title' => 'How to learn Nginx',
            'content' => 'This is how you learn Nginx.',
        ],
    ];
    return $posts;
}
$title = 'My Blog';
$posts = getPosts();
$numPosts = count($posts);
$postText = $numPosts === 1 ? 'post' : 'posts';
$numPostsDisplay = "$numPosts $postText";
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<?php for ($i = 0; $i < $numPosts; $i++) : ?>
    <h3><?= $posts[$i]['title'] ?></h3>
    <p><?= $posts[$i]['content'] ?></p>
<?php endfor ?>

Also note that within the function, we are assigning a value to a variable, and then immediately returning it. This is a common pattern, and there is a shorthand to doing this. You can leave it just like this if you prefer the increased readability, but if you return a value that is immediately assigned, it’s more common to just return it.

So we can remove the assignment to $posts, and just return the code which builds the array.

<?php
function getPosts()
{
    return [
        [
            'title' => 'How to learn PHP',
            'content' => 'This is how you learn PHP.',
        ],
        [
            'title' => 'How to learn MySQL',
            'content' => 'This is how you learn MySQL.',
        ],
        [
            'title' => 'How to learn Nginx',
            'content' => 'This is how you learn Nginx.',
        ],
    ];
}
$title = 'My Blog';
$posts = getPosts();
$numPosts = count($posts);
$postText = $numPosts === 1 ? 'post' : 'posts';
$numPostsDisplay = "$numPosts $postText";
?>
<h1><?= $title ?></h1>
<h2><?= $numPostsDisplay ?></h2>
<?php for ($i = 0; $i < $numPosts; $i++) : ?>
    <h3><?= $posts[$i]['title'] ?></h3>
    <p><?= $posts[$i]['content'] ?></p>
<?php endfor ?>

If you are using an IDE like PHPStorm, you can now start to also collapse blocks of code, such as code within functions, by clicking on the minus sign next to the function, or the ellipses that are displayed within the open & close curly brackets.

We can always expand it later by clicking the related plus sign, but collapsing blocks of code can make the rest of our code a bit easier to read.

Complete and Continue  
Extra lesson content locked
Enroll to access source code, comments & track progress.
Enroll for Free to Unlock