Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.5 PHP Variables and Arrays

In this lesson, I'll show you an essential aspect of the WordPress programming language: how to use PHP variables and arrays. First, you'll see how PHP variables are used to store, retrieve, and share data values. Then, I'll show you how PHP arrays are used to manipulate entire lists of data values.

4.5 PHP Variables and Arrays

Hello and welcome back to this Tuts+ course on learning PHP for WordPress. In this part of the course we are gonna look at variables. You can use variables to stored values or data, and then output it whenever you need to. So let's start by creating some simple variables. And some of this will be familiar with you, because we've already been using variables at earlier points in the course. So here I've got blank PHP file that I'm using to demonstrate this technique. And first I'm gonna set up a simple text variable. So that creates a variable called message, which is hello world, it's just text. And then it echoes out the contents of that variable. So that would be the same as if I'd just typed echo hello world. So at the moment it doesn't really add a lot, but you'll find as you need to echo out more complicated and more detailed variables, or if you need to use them multiple times in your code. Storing something as a variable can be incredibly useful. So as well as using variables for simple text, you can also use them to store the result of a function that you run in WordPress. So this variable is storing today's date, and now I can echo that out. And that would simply echo out today's date. Again doesn't add a lot, you're writing more code than if you'd just typed echo date. But it shows you that you can store that and you could then use it multiple times in your code. Now let's look at something a little bit more complicated. Storing the results of a database call. And here I'm gonna use some code that you've already seen in this course where I used the get pages function to output a list of the top level pages in my site. And you'll see in this part here was where I specified those arguments. So let's copy them. And here you can see another use of a variable. So what I'm doing here is converting a variable that stores an array of values, i.e., a list of all the pages in your site, to a variable that stores a single set of values. It's still an array, because my page will be storing lots of data about that individual page, but it just relates to the one page. So as I go through each value in the variable MyPages, I store that value or that array of values. Which are all the fields relating to that particular page as MyPage, and then I can work with MyPage as a new variable. And note here that I'm having to use the ID of the page with the getPageLink function. That's because simply using the MyPage variable will just return all of the different fields for that page. So what I could do instead, if I wanted to, is store that as another variable. So mypage-ID is the ID of the current page. And you might find that useful if you're then writing code that will reference that ID repeated times. So here instead of using mypage-ID, I use my new variable. So here you can see I'm using a number of variables. I've got args to start with, which is an array of variables that are the arguments that I'll be using for get pages. And then I've got MyPages, which is another array, which stores all of the data relating to all of the pages fetched by this instance of get pages. This should be MyPages again, because that checks that MyPages is populated and it has actually effects on pages. And then within that, let me just correct that. Within that I've got so each MyPages as MyPage. So again I've got a new variable here, which again is an array of all the values for that particular page. And here I've got mypage-ID, that isn't an array. So that is just a single value which is the ID of this page. And if I wanted to I could echo that out. I could just echo mypage-ID to see what the ID was. But generally that's not something that we'd want to do. So then finally with this, I need to close that unordered list. And you may have noticed this quite a lot going on with my echo function here. And I'm gonna go through all of that with you in the next part of the course, which will be about how you echo out your text and your content. So those were a few examples of how you can use variables in WordPress. Now let's put it all together. So I'm gonna use three variables, and I'm gonna echo them all out in one sentence. I'm sure I've got my syntax right, so here I've got a message which is Hello Everybody. And because I'm using this same variable, I'm overriding the same instance of this variable here. So you can override a variable later on in your code. Which is useful, because you can work with one value of it and then work with another value of it. And here again I've got date = today's date. Now if I wanted to, if I've already defined that variable earlier on in my same file, I don't have to call that again. So I'm gonna comment that out. And then finally, I'm gonna use another function or template tag provided by WordPress, which is get_post_meta. And I'm gonna use this to fetch the value of a custom field against my current post. So this code that I'm running here can only be run in the loop. Because we're working with the current post. Or alternatively, you could run it in a for each loop like this, so you could run it in a standard loop or in a for each loop. So I've now got three variables. I've got my message, my date, and my mood, which isn't get_post_mate, it's get_post_meta. And here, because I'm in a standard loop, I'm using get_the_ID to fetch the ID of the current post. Now this works in a standard loop. And in a moment I'll show you how this can work in a for each loop like this one up here. So finally I want to echo all that out. Now if the first thing that you're echoing is either the results of a function or it's a variable, you don't put a quotation mark in front of it. But then you do need to do that for any text. So this would echo out, Hello Everybody, today's date is the 2nd of February 2018, in my case recording now. And I'm feeling good, if that's the mood that I've set in this post. And don't forget, this is the loop. Now let's have a look at how we could implement this up here. So let's copy this, Now I only need to define the message and the date once. Outside my for each loop, because that will be the same every time I reiterate this loop. However, the mood is something I'll need to define within each for each loop, for each page. And here I need to actually use get posts instead of get pages. So I'm gonna change my arguments, To post per page = 5, which will fetch the five most recent posts. And I'm gonna call this myposts. Right, so we've now got this. Hang on, I've spotted an error there. Which will also be in there, that wouldn't outpost the post title correctly. Let me just correct that. Right, so let's work through this. We've got an array of arguments, post_per_page is 5. $myposts = get_posts with those arguments, so that'll fetch the 5 latest posts, not the top level pages. If $myposts has returned some values, I then echo out an unordered list with a class of posts. For each iteration of $myposts, I call it mypost. I set a variable called mypost-ID which is the ID of mypost, and then I echo out some content. But before I do that, I need to define my variables and I'm gonna copy these. So I've got my message and my date, I then need to define my mood. And the first variable for this is mypost-ID, because we've already defined that as the ID of the current post. The second variable is the custom field mood and the third variable is true. And what that does is ensures that it only brings back one result. So if more than one value has been recorded for this post against that custom field, only the first one will be returned. So I want to echo out a list, I don't need a link. Let's get rid of that. So I'm echoing, This. Right, so for each post it will echo out today's date is date, and I'm feeling good, or whatever the mood would be. Which is a bit odd because, why would we do that at the same time with the date for each one? So I'm gonna change date to the post date. So I'm gonna move that down here, And instead of date, I need to fetch the date of this post. So I use get_the_date instead. And the first parameter for that is the format, and the second one is the post ID. So it'll say Hello Everybody, I wrote this post on, and then the date of the post. And I was feeling and then the mood that I added for that post. So that's how you use variables to create a more complex line of code and output text and data together. In the next part of the course, we're gonna take more of a detailed look at echoing things out. You'll have noticed that I've used echo quite a lot already. And I've explained the syntax that I've used and how it all hooks together. See you next time and thanks for watching.

Back to the top