 there we go. That's my audio. Sorry folks, I had myself muted. So let me start over. Good morning, good afternoon, and good evening. And my apologies for not having the audio working. I'd have to apologize for Adrian when she gets back. That was totally my fault. So as you're joining us today, did us know in the chat who you're joining from? Adrian, that was my fault, not yours. It was my audio. I was muted. So I apologize for that. My name is Jonathan. I am from Cape Town in South Africa. I am a developer educator at Automatic, and I'm sponsored to work with a training team, and the training team are the folks in the WordPress community who work on the Learn WordPress website. We do tutorials. We do lesson plans. We do courses. We do online workshops like this. And our goal is just to help everybody learn about WordPress, how to use it, how to develop with it, how to build with it, and all of those kind of fun things. We have, I'm going to assume it's Jose, not Jose, but either way, I hope I'm right, from Sacramento. Rico from Switzerland, got Mark from Isoqua. I realized today, Mark, that your MLWP is simply your initials, not ML WordPress. Mark messaged me a question in Slack yesterday, and when I replied, I realized that the WP is literally just his surname. So that was quite fun to discover. Adrian from Anaheim, California, home of Disneyland. That's a place I'd love to go to sometime, Disneyland, any one of the Disneyland or Disney World around the world. So awesome. Welcome, everybody. It's great to have you joining us today. Right. So today, we're going to be discussing global variables, specifically global variables in PHP. So this will be a more heavy PHP session this week. I was thinking while I was preparing this workshop that it might be a good idea to discuss global variables in JavaScript as well, and how that all works. But this topic is specific to PHP, specifically because of how PHP manages global variables and things that you can do with global variables and the considerations around using or not using global variables. So this will be a very PHP specific focus workshop today. Before we get going, just a few announcements. Again, welcome, everybody. I see there are a few folks joining us still. So welcome to anybody who has just joined or anybody joining. If you can't see the slide, so right now you should see a slide on my screen that says announcements, my shared desktop. If you can't see that or at any point in time, if you can't see my screen, please let me know in the chat or you can unmute and let me know, and I'll re-enable the screen share. While I'm doing that, I apologize again. I do still have a little bit of a cough. My cold is a lot, lot better, but I do still have a little bit of a tickle on my throat. So I will do my best to cough off screen, but I apologize if that does happen during this presentation. We are presenting in focus mode as always. If you would like to enable your video, you're more than welcome to, but there's no need to do so. Focus mode means I can see your video and you can see mine, but you can't see each other. And that just prevents any zoom bombing, which may or may not occur. And then as always, you're welcome to ask questions. You're welcome to post them in the chat or unmute to ask questions. You're welcome to ask me to slow down if I get too fast or anything like that. I do allow time for questions through the course of the presentation. So you're welcome to keep those for those moments. If the question relates to something on screen that you're not understanding, you're welcome to unmute or just post in the chat, and I will keep an eye on the chat there. I don't have a co-host today, and that's only because the person who offered to co-host is going through the process of applying to be a co-host, and there's a whole vetting process there. So unfortunately, we just couldn't get that going in time. So please bear with me if I have to check a few things at the same time. Normally, I have somebody around to help me do that. If you're going to code along with me today, make sure your local install is ready. I will go through the requirements in a second. There are very few today. We're not going to add a lot of extra things. And then if I start speaking too fast, please do let me know. I will be posting this to WordPress TV afterwards, so we are recording this, and this will go live on WordPress TV sometime during the course of the day tomorrow. And then if you're looking for more WordPress tutorials and courses, you can go to learn.wordpress.org. If you're looking for more WordPress developer-focused news and updates, you're welcome to go to developer.wordpress.org slash news. It's a great place for all kinds of blogs and interesting articles about developer-focused topics. All right, our learning outcomes today. We're going to learn what our global variables in PHP and how they work. We're going to learn how to define them and how to use them. We're also going to chat about why they're considered bad and why WordPress is using them if they are considered bad. And then we're going to go through a short list of what global variables are available in WordPress and are recommended to make use of. Excuse me. And then we're going to talk about how we can interact with some of these global variables. The goal of today's session is not to dive into every single global variable that exists, but to give you the list that does exist that is recommended to work with so that if you need to use them, you know where to go looking for information and what global variables you can and should use. All right, then on a scale of one to five, I'd like you now to take a moment in the chat and let me know on a scale of one to five, one being, I don't know this topic at all, five being, I know this topic really, really well. How well do you know this topic? I'd like to get a feel for where we're sitting on this topic. I would consider myself a four on this one. I come from a world, I started using PHP when global variables weren't considered a bad thing, if you will. I started using PHP around about the time that it started adopting object-oriented programming. So people were using globals all over the place. And so I've often used globals for various different things that I know most of the global variables that exist. But there's always knowledge to learn. There's always new things to learn. So I'm probably about a four today. Sharon is a three. Jose is a two. Adrian's a one. Rika's a three. Moxie's is a two. Lassie says a two as well. Okay, so we've got some folks who know something. We've got some folks who know a little bit more. That's great. We're not going to be doing a massively deep dive into global variables today. But hopefully you'll go away with enough knowledge and understanding of how you can and should use them. All right, so if you're going to code along with me today, most of our code today is purely going to be example code. It's going to be just showing how things work. We're not going to be actually creating anything useful today. But we will need to write some code so that we can understand how global variables work. So all you need, if you want to code along with me to see the results on your side is a local WordPress install of whatever type you're comfortable with. A text editor to edit some files. And we're not going to be creating any new files. We're going to be hacking on core files, which I don't recommend unless you're experimenting like we are today. And then at least one active theme and plugin. So I'm going to be working with the current default theme, which is 2023. And then I'm going to be using the create block theme plugin. But it doesn't matter which plugin you have installed. You could be using the Hello Dolly plugin if you will. You could be using any one of the classic themes. We're basically just going to hack on the core plugin file, the main plugin file. We're going to write a little bit of code at the top of that file somewhere. And then in one of the footer files for the theme, we're going to write some code in there just so we can see how it all filters through. So if you want to install 2023 and create block theme, you're welcome to, if you really have an active theme and an active plugin on your site, just make sure it's active, the theme and the plugin, and then we'll go through from there. All right. Are there any questions at this point in time before we get started? I'm going to break for a quick refreshment and then we'll kick things off. Okay. So let's dive into global variables in PHP. And we're going to start, you'll see I've got a very short list of resources today. So we're going to start, I'm just moving my slides and not editing my code. We're going to start with the PHP manual, which has a section on language variable scope. So that's where we're going to start today. And essentially what we're talking about is the scope of a variable. So in other words, where does a variable apply? So to understand variable scope, what I'm going to do is I've got my Visual Code Studio open. I've got my LearnPress site open in my Visual Code Studio. And in the roots of the LearnPress folder next to where the index.php file sits, I'm going to create a new file there. Kaiser says, can I reshare my screen? No problem. Let me disable it and then reshare. Thank you for letting me know, Kaiser. Okay. I have, it's busy loading. I have shared my screen now. So please let me know if you can't see it. While I, in the root of my WordPress install, I'm going to create a new file, and I'm just going to call it, it's not typing, I'm just going to call it globals, globals.php. So that's a PHP file that I'm going to create. Okay, awesome. Kaiser can see what I'm doing. That's excellent. And inside this globals.php, I'm going to open up my PHP tags, which is the less than sign, question mark, and then PHP in lowercase. And then I'm going to define a variable. So if you've never worked with PHP before, a variable uses a dollar sign, and then the name of the variable. Typically, it's some kind of string. So in this case, I'm just going to call it variable one. And I'm going to assign it a value. I'm not going to use value one that Copilot is suggesting me. I'm just going to say something like variable contents, something like that, something simple. So that is a PHP variable. So now I can use that variable in functions and in methods and to do things with it, whatever the case may be. Now, in PHP, you have this thing where you can include files. So I can take files, I can put put code in other places, and then I can include the code there. And any variable that I've defined like this in the roots of this PHP file will be available in any files that I include. But if I create a user defined function, in other words, I'm the user defining this function. And I'm going to call it some function, just for the sake of being lame. And I then try and access that variable inside of the function. I'm not able to do that. So for example, if I was to say echo variable one here, you'll notice that visual code is actually telling me, hang on, there's a problem with this code. And then at the bottom of this file, if I just call some function like that, and I try and run it, I'm not going to see anything. So let's verify that to make sure that it does break. So I'm going to switch back over to my browser. I'm going to open up, I'm just going to move this out the way. I'm going to open up my local site, which is on learnpress.test. And I'm going to go to forward slash globals.php, which is the file that I created. Some reason my browser always removes that forward slash. I'm not sure quite why that is. No, don't want to do that. What is going on here? Let me do this again. Sorry, folks. That there we go. And you will see when I try and run that because I've got my debugger enabled, you might not see this, you might just see a white screen, or you might see this kind of message undefined variable variable one in globals.php on line six. And that is because in that function, the variable one does not exist according to the scope of this function. It cannot access the variable one at the top here. So I need to redefine it and reuse it in whatever the case may be. Now, one way I can fix that problem is I can pass variable one into some function like that. And then I can accept it as a parameter of the function. So let's call this function parameter, for example. And now if I update this to say echo function parameter, it's going to receive whatever is sitting inside variable one into function parameter. And then when it echoes function parameter, I'll see visible contents. So let's have a look at that. So if I refresh that now, there I see variable contents. Now, this is the way that you typically, and I'm going to share this code in the chat if anybody wants to play with it. This is the way you typically should share variables into your functions. You should pass them in as parameters. But sometimes that is not necessarily possible, or sometimes you've forgotten what the variable might be called, or sometimes you want something that you just want available everywhere. So what you can do to make that happen is you can make this a global variable. And the way to do that, there's kind of two ways you can do this. So the first thing that you can do, I'm just going to show you how this works. I'm going to take this out over here. And I don't want this code that Corp pilot has generated for me. Inside of this function, I can say this variable access the global variable variable one. So because I've defined variable one right at the top of my PHP file, its default is that it can be made available globally. But inside of my user defined function, I need to tell this function, look, get a hold of that globally defined variable. So now if I run this code, and let's change this to global variable contents, so we can see the change when we refresh the page, global variable contents, there we go. So now if I run this code, we should see that that changes. There we go, global variable contents. So what that means, I didn't have to pass anything into the function. Because I use that global keyword, it tells the function, okay, go and find a variable with this name called variable one. And if it exists, use it. And so it goes and it looks and it finds this variable one. And then it uses it inside of this function. Okay. Now before we get into the why you shouldn't do this, does anybody have any questions about how this works and why it works? Scoping, is that a question or a comment? I learned it is scoping. Okay, so that's what it's called. It's called variable scoping. Right. So this variable here, if I take out this global, this variable, variable one is not available in the scope of this function. So that's why it doesn't work. Okay. So now let's talk about why you shouldn't do this. So in this example that we're looking at, it's very easy for me to see that this is variable one and what its contents are, and what I expect it to do. But in a much bigger project, I might be in a position where this file that I'm working in is not where the global variable is defined. And I might decide maybe somewhere higher up in the code, because I'm working on an open source project like WordPress, or I'm developing a plugin for WordPress, I might decide, well, I want to create my own variable called variable one in my code. So let me give you an example of what I'm talking about. So to do this, let's move out of the globals.php. And I'm going to go into the index.php file of my WordPress install. So this is the index.php in the root of my WordPress install. It's the one that defines the use themes constant. And then it requires the blog header, which triggers the WordPress request. And let's say I have a variable called, I don't know, let's call it learn WordPress version, say, and we'll make it version 1.0.0. And that's the version that I want to use for some product that I'm working on or whatever the case is. And then let's say I'm working in a plugin. So learn WordPress version is now residing as part of the core WordPress code. Okay. And let's say I'm building a custom plugin. And I'm going to create a folder in this plugins directory and I'm going to call it learn WordPress. Actually, I'm not going to do that. Sorry. Let me change it. I'm going to go to my create block theme plugin that I'm working on, say, which makes it easier if I work on an existing plugin. And this is the root file for the create block theme plugin. So this is the one that handles things and works with things and does everything I need to do. So this plugin file, because this plugin is installed and active, this plugin file will be included during the page request. And let's say I don't know about that learn WordPress version variable. But in this plugin file, I want to create my own version of the learn WordPress variable. So I want to say learn WordPress version is version two, for example. When this code executes, so when WordPress executes, it's going to set up this variable as version, as with the value of 1.0.0. And then when it gets to this step in the plugin, it's going to say, well, I'm now redefining that variable as 2.0.0. So if somewhere else, let's say in my footer, so let me go to, this is where I said have a theme and a plugin available. Let's say I go to, let's say I'm busy working with a theme. Let me find this over here. So let's say I'm a theme developer and I'm developing on 2023. And I happen to know that there is this WordPress global called learn WordPress version. And so in my theme, let me just find 2023 here, here we go. In the footer default pattern, I want to echo that learn WordPress version. Let's make it a little bit simpler. Let's make it just WordPress version. Let's do it that way. Then it's a little bit more generic. So in my theme, let me just minimize this. Let me go to my footer here. So in my footer default, in my 2023 patterns, there's this proudly powered by WordPress thing. And I want to, at this point over here, I want to echo the version along with the footer details. So I want to echo the WordPress version global variable. And I want to have a little bit of space between the print F above it. So I'm just going to put a little bit of space in there. It's very, very, very rough code. But I want to get that WordPress version. So let's do that. Let's leave the echo in the footer, echo the WordPress version. And let's go back to the plugin and we'll just take this one out over here. So we see how it works. So it's set up as 1-0-0. It's used in the footer pattern of my theme. So let's go over and have a look at the site. So we see what that does. Kind of done things a bit weirdly. I don't think I've got the right theme active on this one. So let's go back to the dashboard. Come on. There we go. And let's go to parents, themes. Oh, 2023 is active. Okay. I should be seeing that version somewhere. Oh wait, I think it's because I haven't set it up properly. One second. So let's do this. Okay. Let me just make one small change. I think I need to make this a global variable. There we go. And I'll chat about that in a second as well. So let's make sure we can see it here. Yeah, now I've broken everything. One sec folks. One, two. What did I do that wrong there? Oh, sorry. So make it global first. And then say WordPress version equals one. And I'll chat about why I have to do this once I've got it working. And let's refresh. Still not seeing. Oh, you know what this probably is? This is probably because I have got this theme. I have made changes to it. So let's do 2022 as a better example. So I'm going to activate 2022. There we go. And I'm going to take this out of here. Going to 2022 and going to the... Oh, this is a template part. Last. So what's up? Okay, what's happening here? Let me show you what's happening here. I have made changes to my 2023 theme. And I've saved them to the database. I guarantee you that. So I'm going to make a quick switch to a different site. Going to go to my WordPress.test site. There we go. And I'm going to show you the code on that site. A little bit of a mistake on my part there, folks. I do apologize. So let me switch this out. Open WordPress. There we go. Okay. So let me go back one step. So let's find the index.php. Okay. So here I was using a test variable called test. There it is over there. And then in the footer. I can't find my themes. So themes 2023 patterns. Here's the footer default. And you'll see here I'm getting the global test variable and then I'm echoing the test variable. So let's use that. So let's make this, let's go back to what we had. So we said WordPress version. And we said 1.0.0. And then over here, we get the global WordPress version. And then you want to echo the WordPress version. We'll chat about globals there in a second. So let's have a look at that. So there I'm setting up WordPress version equals 1.1.0 in the index.php. So it's one of the very first files that gets loaded. In the footer defaults of the 2023, I'm saying echo, get the global WordPress version. That's what I did wrong. And then echo that WordPress version. And so if we go back to the browser and we refresh that and we scroll down to the bottom, you'll see there it is. Let me zoom in on this a little bit. Right at the bottom. So there we go. WordPress. Partly proud by WordPress. And there is the version number. Okay. So that might be something that exists. It's a global variable. I'm saying access it in the theme and then echo it out. But now let's say I'm a plugin developer. And I'm busy working on my create block theme plugin. And I decide as a plugin developer, I don't maybe know that that WordPress version global variable even exists. But I decide I want to create my own WordPress version. And I'm allowed to do this. And that's the problem with global variables. So at the top of my plugin file, I can say WordPress version equals something. And I could call this whatever I want. So I'm going to make this a text string that says this is the WordPress version. So this is now in a plugin file. Now remember that the way specifically in block themes, the way patterns are included is they're called via function. The way that plugin files are included are called via the include. So when this code runs, what happens is it overrides the original version. And you'll see that now says this is the WordPress version, which breaks the theme functionality. So because this is a global variable, and because I'm able to access it in the plugin, I can affect how things run. And that is one of the reasons why global variables in modern PHP development are generally frowned upon. The way to work around global variables is to do things like passing variables to functions instead of globalizing them. There are other things like using dependency injection in object oriented programming and various other ways to work around this. But the, and I don't want to use the word problem because it's not the right word, but the concern or the issue that we have, maybe not so much the issue, but what we need to understand about WordPress is WordPress comes from a time when using global variables was a done thing. Some of the more modern ways of doing things didn't exist. So there are global variables in play that we need to be aware of so that we don't in our code in our plugin or our theme code try and override those global variables. Now, the example that I've shown you now is a very silly little simple example. There are also the other thing that you can do. I'll show you this very quickly is instead of using variables, you can use something like a constant like the debug constants. So you can define a constant. And if you define a constant and then try and define it again, PHP will actually say to you, no, you can't do this. You need to do it in another way. Okay. So that's the reason why we shouldn't ideally as plugin and theme developers or any developers, we shouldn't ideally create new global variables. But we should be aware of the ones that exist and then not override them. Okay. Any questions around why global variables are considered bad, but why we need to be aware of them before we dive into what global variables do exist and how we should interact with them. Sure. So I'm specifically doing this in the footer default pattern in the 2023 theme. The reason I'm doing it in the footer default pattern is because that's where there is some PHP code that exists. I can't do it in a block file, a block template file, because that's an HTML file. But it's essentially just this global WordPress version. And then echoing out that version. Now, while we're talking about that, I think it's a good idea to understand how this whole global thing works. So because I've defined the WordPress version like this very, very early on, it's acceptable for me to just define it as WordPress version. And then where I'm using it in the footer, I say I use the global keyword WordPress version, which then tells PHP, right, go and fetch that original global variable that was set up or that original variable to set up and make it available here to me. And then I can use it. However, what you'll generally see in the world, and I see your question I'll get to in a second, what you will generally see is the first time a global variable is defined. Folks will actually use the global keyword before that variable like this, especially if it's inside another function or another class or whatever. And when we dive into a few of the examples in a second, I'll show you where that code lives and how that looks. But in this case, because it's in the very, very first file that loads, the way I've set it up like this is perfectly fine. And then I call it, where's that code here? I call it, I call it, I'm not really using the right word, but I basically access it, if you will, by defining the global keyword, the same variable name, and then I can use it. Okay. Elliot says post is a global in the classic theme. Is it the same in a block theme? That's a good question. And we're going to dive into that in a second. So bear with me on that one. We'll actually do that in a block theme and I'll show you what's going on there. Okay. So as to Elliot's question, there are a list of global PHP variables available to you in the developer documentation. I will share this link with you in the chat. And you will see that it says WordPress specific global variables are used throughout WordPress codes for various reasons. Almost all data that WordPress generates can be found in a global variable. It says, note that it's best to use the appropriate API functions where available. And I'll talk about what that means in a second, instead of modifying global variables directly to access a global variable in your code, you first need to globalize the variable with the global variable keyword, which we spoke about. And then it also says accessing other globals besides the ones listed on this list below is not recommended. And that's very, very important. So this is the list of global variables that it's acceptable to access and use and work with. And you will see the very first one in the inside the loop variable is the post global that Elliot was talking about. And that is basically in the scope of a query loop, you could access a post global. And that is the post object for the current post. And then you have various other data available to you. Elliot, to your question about if post is a global in a block theme, as far as I understand, and I've never dived into this because I've never tried to access the post variable in a block theme, but as far as I understand it's only if you're inside of, for example, the query loop block or the query loop block in a block template, or if you're inside a single or a singular template, and only if you have used it inside of a pattern inside that template. So let me show you what I'm talking about there. So in the 2023 theme, there is, for example, the single HTML template. So in the single HTML template, we don't have PHP code. It's all HTML that gets rendered when the page or the post gets rendered. And you will see that inside of this template, we have a group, and then you'll have things like post featured image, post title, post content and those kind of things. Those blocks work because in the scope of this template when it's being rendered, the post variable is available. But to be able to access it, you would need to include a PHP pattern in this file and then access it in that way. The better way to do it is to use one of the post specific blocks like post featured image, like post title, post content. It then actually pulls that data from the post global variable. So it's not as available, but the better way to use it is to use the block mockup. Okay. So these are the list of the variables. You will see that we have post, we have posts, we have author data. So these you can all access in a classic theme in a single post. There are also things like browser detection Boolean variables that you can access. So you can check, is it a phone? Is it on Chrome, Safari, various other options? And I want to show you the is phone one, for example, because it's a nice little example of how this gets defined in the WordPress space. So I'm trying to copy this is phone language there. And I'm going to do a search for is phone inside of my WordPress, my WordPress install. And you will see that it's set up in this vars.php file. So let's go there. And you will see here now, it sets up a whole bunch of global variables. So it sets up page now, is links, is get code, et cetera, et cetera, et cetera. And then as this code executes, then it changes the values of this variable to whatever the final value of that variable should be. And then you can use it anywhere else. So anyway, in a classic theme or anywhere in a block template pattern, you can use page now is get code is a lot of these are very legacy. So is Windows Internet Explorer is not really one we're going to use much anymore. I don't even know if Mac has an Internet Explorer anymore. But you can use some of these to check browser versions and all that kind of thing. There are also some web server detection Booleans. So you can check if this is running on the Apache server, is it running on IIS, IIS 7 and Nginx. This is more useful if you're doing web server-specific things like maybe to do with caching or page caching or anything like that. But WordPress does have some version variables. So there is already a WP version. We defined ours as WordPress version, but there is already a WP version. There is a WPDB version. And these are useful, for example, if you're building a plugin that requires on certain version functionality or a theme even. So if you're building a theme that only requires on the block editor, then you could say requires version 5.0 or greater. Or if you're relying on a plugin, if you're building a plugin that relies on the REST API, you could include a version check and say is version 4.4 or later. Folks generally don't do that. They generally look for functionality that relates to that code. So there's something like, I think there's a constant that is REST API or has REST API enabled or something like that. And so you can check if that constant is defined. And that constant is only defined in the REST API code. But you can use the version variables as well. Then there are some miscellaneous variables. There is my favorite, which is the WP query global object, which is the global instance of the WP query class. So if ever you need to create custom queries, you could globalize WP query and you can use it. There is also the WPDB one, which is another good one for setting up database functionality. There is the admin bar object, which is the global instance of the admin bar, which is useful for adding things to the admin bar and then various other things. And then there are also some admin globals. So there is page now, which basically allows you to get the current page. And then depending on that page, do certain functionality within the dashboard, you can get the current post type and then a few other things. Now, the main reason to know about this list is so that you don't try and recreate these variables in your code. Of the list of variables in this list, I have only ever used the post variable in an index or in a loop, sorry, in a single template or in a loop to access the post data. I think I may have used the page variable at some point to get the current page being used in a pagination environment. But the majority of these globals will have some kind of API function. So let me show you what I'm talking about. If you go to pretty much any classic theme, I'm going to close down all these searches because they're right in my way. So let me open up what's a good example. 2020 is probably a good example of a classic theme. And let's go to the other template parts. Let's go to the singular templates. So that's for a single page, post or page. So here is the loop that checks while this thing has posts, then get the post and then get the template part. Now, the post is a perfect example of an API function. So instead of going and getting the post global, you just call the post function and then that sets up the various things. And then the template part that it's using is template parts content. So let's go to that. So there's template parts content. We'll open up that. And then you'll see if we scroll down a bit here, it's going to let me just move this out the way here. There's some linking with some content. So here's a good example. So the excerpt is a function which goes and gets the post global and retrieves the excerpt object or method or variable on that object. And so that's an API function that you can use instead of globalizing. So what you could do in this code is you could do something like this, you could do global post. And then you could say post get post excerpt, for example, you could say echo at this point in time to echo the excerpt. But it's much easier and better if you will to use the excerpt function. So that's an API function that you should use instead of doing that. So whenever you're trying to get something from a global variable, you should try and see if an API function exists. So let's have a look at this function quickly and we'll just have a look at the code. You'll see that it's actually using the global post inside of the code. So I'm going to go to the excerpt. I'll share this link with you in a second. Let me pop that over there. And if we scroll down, here is the function. And it calls the get to excerpt function. So let's go to that one. And let's go there. And there's get to excerpt. I'll share that with you as well. And let's have a look and you'll see here. So it says get post and it passes in a post option. And then it uses that post. So let's see what get post does. And this is really diving deep into what functions do. And if we have a look at the source code for that, you'll see over here, what this does is it checks if the post is empty, which in this case it is, and the global's post is set. Now this is different from what we've seen before. So bear with me in a second. But then it gets the global post and then returns it to the excerpt function. And they get the excerpt function as it goes on higher up. Okay. So this is an instance where instead of globalizing the post and calling the excerpt property on that object, we can just use the excerpt function and it'll do all the work for us. While we're on the topic, let's talk about this global's post thing over here. So if we go back to the PHP manual that I shared with you earlier, you will see if you scroll down a little bit, it talks about using the global keyword, right? Number one. It also talks about using the global's array. So in this example, you'll see that A and B have been set up as one and two, similar to my WordPress version. And because of that, you can then access inside of your function scope, you can access this global's array and then pass in B, and it will return in this case two. So that's a better slash safer way of accessing global variables is to use the global's array instead of the global keyword. So it's something that WordPress has been doing. Let me go back to the documentation I shared with you in a second. It's something that WordPress has been implementing is using the global's array instead of the global keyword. You will still find some way where the global keyword is worked, is used, but generally, things are using the global's array, and that's a better way to set things up. So if you do need to define a global, and I'll give you an example of why you might want to do that, let's say you have a plugin that creates an object that you wanted to be globalized. One good example of that, I'm actually going to, this is a good way to, let me just find it here. So the plugin is WP Job Manager. I think it's still like this. I'm going to see if I can browse that code quickly for you. Let me share this in the chat. So this is a plugin that was developed by one of the original developers of WooCommerce. And if we browse the code, I hope this is still the same because it was the last time I looked, but that was a few years ago. There it is, WP Job Manager. Yeah, there we go. Exactly the same. Perfect. So in this code, what the plugin developer is doing is they are setting up all their dependencies, requiring the main files, and then they create an instance of the WP Job Manager object. And they want that instance to be made available global. So instead of using the global's keyword, they just add it to the global's array as Job Manager. So if you're building an extension for the Job Manager plugin and you want to access that instance, all you have to do is call the global's Job Manager item from that global array, and then you can work with it. So it's a better way than using the global keyword. So that's a recommendation. If you do have to use globals, that's a recommendation of how to use them. It's generally not a good idea to define global things like that. It's better to try and either pass variables around or use dependency injection if you can. You will generally see this kind of code in a space where the developer is wanting to make something available to extenders of their product. So that's why they make it as a global option. And then they will usually have API methods that you can call to access various things within that global within that global scope. Okay. So those are the lists. Let me find them again. I think we've lost them by now. Yeah, we go back to the list of global variables. This is the list of global variables that is recommended to work with in WordPress. It is a good idea to know that these exist. It is a good idea to know how to access them to find the API options if you can. One example where there isn't an API to access things is, for example, the WPDB global. That one, I just call the global instance when I need to access it because there is no function that will call it for you. So it's good to know which ones you do have functions for and which ones you don't. And that's just a case of diving into the code and diving into how things work and making sure you use them correctly. The other reason to know about this list is so that you don't define in your code any variables that might clash with this list because in doing so, you will affect other functionality within WordPress and possibly break people's sites. So it's a good idea to know about these global variables and to know that they are there. Okay. Iliad says, is global a super global variable like Post or Git? The answer to that, I'm pretty sure if we click on the globals, I am pretty sure it is a super global. Yes. There we go. It's listed under super globals. So yes, it is a super global like Git or Post. And that's another reason to apply it to that array as opposed to using the global scope. It's the same thing. If you say something is global or if you add it to the global's array, it works out to the same thing. But this is a better way of doing it. But yes, it does fall under the list of super globals which are built in variables that are always available in all scopes. So if you want to dive into that documentation there, you can go read about that there. Some of you may have seen things like Git or Post or the server variable. So these are specifically PHP things, not WordPress things, specifically PHP things. And that's another good reason to use the globals array because it is available across your PHP site. Adrian says super globals. Yes. There's lots of superhero naming conventions in programming languages. Okay. Awesome. That was my bit today. Today was very much more of an informative session as opposed to, as I say, creating something actually useful. This is one of those things where it's good to know about it. It's good to be aware of it. But I try and stay away from globals as much as I can. The one time I have used a global in production code, I think this is a useful story to share, a customer wanted something to happen under certain circumstances. And the only way that we could make that work was to create a global that was available across their WordPress site. And then when that global was set, and so there were various places where this thing could change. And then when it was set to a certain thing, one piece of functionality happened and when it was set to something else, another piece of function happened. So there are use cases for globals. But I would suggest that you should try and only use them as sort of a last resort if you can't find another way to do it. Most globals today can generally be solved by using a constant that you define. And for example, one way that I like to do it is if I want something, if I want some value like a version number available across a whole WordPress site, then I will just define it as a constant in the WP config, just like I would the database credentials or whatever the case may be. And then it's available everywhere. And because constants can't get overwritten, if they're defined, once you can't redefine them, it's a great check against the code getting changed. So if you're thinking about a global, first maybe try a constant, then think about, well, where is this going to get passed? Where is it going to get used? As I mentioned with WP Job Manager, the one good use case for a global is an object that contains or a variable that contains some information that you want extenders of your product to be able to use. So let's say you're building a word commerce type plugin or a e-commerce plugin is one good example, a learning management system is another example. And there is core functionality that you want your extenders to have access to. Adding that to a global is generally a good use case. If you do that, make sure it's documented in your documentation some way so that folks know it's there and don't try and override it. Okay. Are there any other questions before we wrap up? I didn't expect this to go the full hour today. And that's fine. Elliot says, oh, you log your error logs by date. Nice. Yes, it's actually something I started doing recently. For those of you who don't know, you can log your errors to a specific file. So here I'm logging to the site's WordPress Dev Logs Errors bar, but then I append the date to it. And this is purely me being lazy thing. I like to leave debugging on in my local environment. And then as I'm working on stuff every day, then I can just get a new log every day. I got this from, I'll show you the code if you like, you'll just need to change the path to your own environment. I got this from server logs. Generally, your Linux server logs, you can set it to create a brand new log every day and you'll end up with these massively long log files. So that's something I've always done for the past few years. And it's very handy. Okay. Awesome. Thank you all for joining me. I hope there was something useful out of that today. I hope your score went one mark higher today. If it didn't, that's perfectly fine. But do know about them. That's my main takeaway. Know about global variables. Know that they exist and make sure you're not reusing them anywhere in your code. Cool. Next week, we're going to take a brief break from common APIs and we're going to focus on all of the cool new developer-focused things that have come to WordPress in WordPress 6.3. So one of my colleagues at Automatic, Justin Tadlock, you might know him from when he used to work at WP Tavern, he writes a blog for the developer blog that he posts on the 10th of every month. That's called What's New for Developers. And he'll be posting his latest version today, the 10th of August. And it's primarily focused on the new developer tools and tricks and things that are coming to WordPress 6.3. So the plan next week is for us to go through that document together and kind of dive into some of the cool things there. There's some interesting little improvements that have come to WordPress for developers. Elliott says, Tadlock the legend. Yes. Everybody knows Justin Tadlock is. So that's all we'll be doing next week. And then the week after that, there's something different that I'm going to be trying. But I haven't fully blessed it out yet. So I don't want to share what it is. But we'll take a bit of a break from Common APIs for a bit and do those two things. And then we'll get back onto the Common APIs train after that. Awesome. Thank you all for joining. Thank you for your amazing questions. And as always, if you have any further questions about this session or any other sessions that I've done, you're welcome to find me in Slack. I'm in the Make WordPress Slack. I think just with my name. So you're welcome to ping me there with questions at any point in time. Otherwise, enjoy the rest of your Thursday. Enjoy the rest of your week and your weekend. And I'll see you next week.