1. 33
    Calling functions with return values in PHP
    3m 33s

Calling functions with return values in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 2 years ago
Updated 2 years ago

A PHP function can encapsulate a block of code, but it can also return values. In this lesson, we will learn how to return values created within a function.

Instructor: [0:00] Since the post variable returns undefined here, if we try to execute this code, we will get an undefined variable error, which is the same error that PhpStorm is giving us.

[0:13] Our next step is to actually call the code of this getPost function and assign it to a value of this post variable. Let's go ahead and create a new posts variable. We will set it equal to our function name, which is getPosts.

[0:33] We will execute the function by calling it with a set of open and closed parenthesis. When we save and re-execute this code, we will actually get another error. This is a fatal PHP error.

[0:47] If we read it through, it says argument one, which is dollar-sign value, must be of type countable or array. Null given. It also gives us exactly where this error is occurring, which is the index.php file on line 22.

[1:06] If we go ahead and check line 22, that will be when we call the count function. Since the error is saying the argument type is null, that is essentially telling us that the post variable is null.

[1:18] Well, how can that be? Well, we created the function and an executed the code within it, but since we cannot access the code of the function outside of it, a null response is actually returned, which means nothing is returned.

[1:35] In order to get access to the result of the function call, the function must return some sort of value. We can do this with the return keyword. After this post variable within our function, let's go ahead and type return. Following return will be the data we want to return. This will be the post variable which is equal to this array of data.

[2:02] Now, if we save and re-execute the code, we will see that we get our desired result. We're only getting one element of the array because we still have this break code within our forward loop. Let's go ahead and remove that, save, and refresh. We will see we are getting all elements of our array.

[2:24] Looking back at our function, you can see that we are assigning a value to a variable and then returning that variable. This is a common pattern, so there is a shorthand for doing this. You can leave it just like this if you prefer for the increased readability.

[2:43] If you return a value that is then immediately assigned, its more common to just return that value. Instead of returning posts, we can remove this, we can remove the definition of our post variable and just return the array. If we refresh, we will see that we get the same result.

[3:05] If you are using an IDE like PhpStorm, you can also start to collapse blocks of code, such as code within functions, by clicking on the minus sign next to the function. To expand it back out, you can click the plus sign next to the function or click on the ellipses containing your code.

[3:25] Well, collapsing blocks of code can make your files a bit easier to understand.

egghead
egghead
~ 28 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today