Debugging Output Basics0:36
Okay, so now it makes perfect sense. It's always showing up as the About page because we've hardcoded it into the HTML. Okay, so it sounds like we need to instead apply that conditionally. So we need a way to say, well if the current page is home, then apply these active styles. So I'll show you how to do that. To begin, I'm going to visit, how about index.php, and I want to show you something. First up, within this file, I can echo gibberish, and just to show you, if I switch back and visit the home page, we'll see it at the top. However, if I instead echo something that is not a string, like how about an array, we're going to see a warning.
However, if I instead echo something that is not a string, like how about an array, we're going to see a warning. Notice Array to String Conversion. Alright so when we use echo, php is expecting us to provide a string, and that's good to know. But yeah, what about situations where I instead want to dump an array or an object? Well in those cases, we can use a function called var_dump, or variable_dump. Okay, if I come back and give it a refresh, there we go. So now I can see this variable is an array that contains one item where that item's value is this gibberish text here.
Inspecting $_SERVER Data1:42
So now I can see this $variable is an array that contains one item where that item's value is this gibberish text here. Okay, so now I want to show you something else. php also includes, and this is a new term, what's known as a super global. So these are variables that are accessible from any script or from any file. You could use it to grab information about a GET request or a POST request, and if you don't know what those are, you will soon. But we can also ask for information about the server. So let's have a look here. And notice I use $_SERVER, and then we dump that.
So let's have a look here. And notice I use $_SERVER, and then we dump that. Okay, so that's going to be an array, but it's a little difficult to parse here. So we have a couple options, but what I like to do is to wrap it within <pre> tags, and that will preserve the formatting. So check this out. Why don't we echo out an opening <pre> tag, and then we dump it, and then we echo a closing </pre> tag. All right, so if we switch back and give it a refresh, yeah, notice it's that much easier to take in, which is great.
All right, so if we switch back and give it a refresh, yeah, notice it's that much easier to take in, which is great. So we dump the contents of this array, and then we see the rest of our view. But also one thing that you often want to do is dump your variable and then kill the execution or die. And I use that word die because it's actually a programming term. It's the exact term, in fact, that we would use to kill the execution. So notice if I call die here, nothing after that function call will be executed. So if I come back and refresh, we dump our variable, and then there's nothing else at the bottom of the page.
So if I come back and refresh, we dump our variable, and then there's nothing else at the bottom of the page. And as it turns out, this is something that you're going to do all the time, basically every time that you quickly want to inspect a particular variable. So if I want to do this all the time, it's a shame that I would constantly have to echo this opening <pre> tag, then dump the variable, then close it, then call die. Well maybe we should reach for a function. And remember, functions just do things. They are the verbs of the programming world. So why don't we wrap all of this within a function called, how about dd, dump and die.
They are the verbs of the programming world. So why don't we wrap all of this within a function called, how about dd, dump and die. And then I will paste all of that in. But of course now, I don't always want to dump the $_SERVER superglobal. I want to dump whatever value you pass in. Okay, and then I will replace that here. Okay, so now have a look. If I refresh, we don't see anything because I didn't call this function. But now I will die and dump the contents of this superglobal. Come back, refresh, and now we get exactly what we had before, but that logic is now
But now I will die and dump the contents of this superglobal. Come back, refresh, and now we get exactly what we had before, but that logic is now nicely wrapped within a brand new function. So if I instead want to die and dump whatever this $heading variable is, come back, refresh, and it works. Pretty cool. Okay, so let's bring this back and actually inspect our superglobal here. And specifically, I want to point your attention to this key here, requestUri. And notice the value is forward slash, which always refers to the homepage. So basically everything after the domain name.
And notice the value is forward slash, which always refers to the homepage. So basically everything after the domain name. So I think what we can do is inspect the requestUri to figure out what the current page is. Let's give it a shot. Let's echo the server requestUri. And yeah, if we come back and refresh at the very top, sure enough, I can now detect what the current page is. If I switch over to about, let's do the same thing here. We'll do it right there. And now we should see /about.php.
Conditionally Styling Navigation5:27
We'll do it right there. And now we should see /about.php. Okay, interesting. So now let's take it a little further. I'm going to get rid of all of that and switch over to our navigation partial. And yeah, maybe we could conditionally apply this. Open up php if, and let's close out php, if we look into the $_SERVER superglobal, and specifically at the requestUri, if that equals / or the homepage, only on that condition should we echo out the following classes. And what was it?
Okay, but I think we also need to have an else condition. Yeah, so notice right now we might be applying a class of text-white as well as text-gray. So it sounds like we would need to say, well, if it's the current page, then use these classes. Otherwise, else, echo that class. And yeah, I want you to notice this will work, but already it's really, really messy. Okay, so here's what I want to show you. I want to show you a slightly different way that we could format things. And to illustrate this, I will return to index.php, and I'll paste this logic in here and reformat. All right, and this is what we just wrote. So if a condition is truthy, then echo this string.
Using the Ternary Operator7:11
All right, and this is what we just wrote. So if a condition is truthy, then echo this string. Otherwise, echo that string. So as it turns out, for situations like this, we could alternatively reach for the ternary operator, which is just a different way that we could structure things. So if I were to rewrite this, it would look like the following. echo, and then our condition, so I will copy that here. Then a question mark, and that's sort of like asking a question. Is it truthy? If so, I want to echo these classes.
Is it truthy? If so, I want to echo these classes. And if it's not truthy, I want to echo that. So yeah, this form and this form are effectively and functionally identical. It's just a different way to write the exact same thing. We ask our question. If it's truthy, then do this. If it's not truthy, then do that. Okay, so I show you this because we could rewrite this and save a few keystrokes. Let's return to our navigation area, and I'm going to change this.
Okay, so I show you this because we could rewrite this and save a few keystrokes. Let's return to our navigation area, and I'm going to change this. Let's remove all of this, and we'll say, all right, echo, and then we ask our question. Is the request your /question ? Is it? Then do these classes. If it's not, then do those classes. And yeah, this should do the exact same thing. Come back and refresh, and notice I still see those active styles. All right, and of course, I don't have to write echo here.
Come back and refresh, and notice I still see those active styles. All right, and of course, I don't have to write echo here. I could use the short php tags, like so. And even in this case, the semicolon is optional. So that cleans it up just a little bit, but I think we could take it a little further in just a moment. For now, though, let's copy all of this and repeat it. And the only thing I'm going to change is which URI I'm looking for. So for this link, I would want to check, well, are we on the about page? Only on that condition do we apply those styles.
Creating a URL Helper9:36
it's a little cumbersome to write. So I wonder if we could make it even cleaner. Let's see. Let's go into index.php, and I will add a new function. Maybe this function will tell me if the URL, or URI, we'll use URL in this case, if the URL is a particular value. So this will return a Boolean, true or false. And we know how to determine that because it's this right here. So I will copy that, switch back, and paste it in. And then we'll just replace the value with this variable and return the result.
So I will copy that, switch back, and paste it in. And then we'll just replace the value with this variable and return the result. Okay, so now we have a useful function that tells me if this URL is the home page or the about page. Let's see if we can use it now. I will go into nav, and let's replace all of this by saying, well, if the URL is the home page, only on that condition do we apply these classes. So yeah, notice it's a very small improvement. You might even call it, I don't know, a micro-improvement. But I promise you, so much of your programming career will consist of small,
But what about on the home page? Refresh, I do, but not on about, and not on contact. And if you think about it, that makes perfect sense. We only defined this function within index.php. Okay, so now we're in this weird boat where I have to copy these functions for each page, and immediately, this feels wrong. So if I do it again, it's kind of solved the problem. But yeah, we don't want to do that. I don't want to duplicate functions across every file. So once again, this is an example of extraction.
Extracting Shared Functions11:39
I don't want to duplicate functions across every file. So once again, this is an example of extraction. We're going to extract these functions into their own file, just like we extracted these view partials into their own file. So why don't we call it, create a new file. And how about functions.php, and I'll paste it in. And now, I can manage and define them in a single location. So that means, back to index.php, I can delete those. And the same for contact, and the same for about. But now, of course, I have to require that functions file.
And the same for contact, and the same for about. But now, of course, I have to require that functions.php file. So I will do that at the top, functions.php. And yeah, it's true, I would have to do this for every view, but I'll show you how to get around to that in an episode or two, I promise. And then we'll do it right here as well. And yeah, if we did everything correctly, that problem should now be solved, and it is. Great, cool. So yeah, now we have dynamic, active navigation styling for
