PHP Basics

Superglobal Variables & URL Parameters

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Superglobal Variables & URL Parameters" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano introduces superglobal variables that give you access to GET and POST data sent from a form. There are other superglobal variables for accessing cookies, session, and other server-related data. In this lesson, the GET superglobal variable accesses the amount and crypto variables in the URL.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Superglobal Variables & URL Parameters" Lesson

[00:00:00]
>> Maximiliano Firtman: First, I'm not sure if you have, have you ever played with the form, html form for server side, because if you have started doing web development the past five years, probably you haven't. It's kind of new, the form has an action attribute where you specify where do you wanna send the results of this form?

[00:00:25]
For example, I can send this to a convert.php file, okay? So that convert.php file will receive the data from this form, that's kind of idea, okay? So now if I refresh this, I type something and click Convert. Now you can see that the browser is going to convert.php, we are getting a 404 because we didn't create the file yet.

[00:00:55]
Okay, so the next step is to create a file convert.php. And this one, there are two options here to be honest. We can server side, render the results, or we can calculate the results and return back to the index html, or things like that. Let's do the simplest one, I'm going to create another html here.

[00:01:21]
Here, I mean it's not going to be really nice, I just wanna explain you the basics of PHP. So in this case, we're going to take Conversion Results, let me look at that. Here, I wanna add some php code, this is the moment where we add some php code.

[00:01:42]
So in the middle of the html, we'll say, here comes the dynamic part. So now we can add php in the middle of this html. That, by the way, this html has all the tags, remember we don't need them. I'm not saying delete them, okay? Just have in mind they're not mandatory but in this one, I don't care so we can just stay like this.

[00:02:02]
And the question is, how can I read the value from the form? And this is a time to talk about this idea that has a weird name, okay, that is super global variables. The what, Superglobal Variables. Well, actually PHP has a lot of global collections available typically under dollar sign because it's a variable underscore.

[00:02:33]
You can create your own variable starting with underscore. It's possible, but it's a good time practice to leave those to the system, okay? I mean you can, but also create your own underscore variable and these are known as superglobal variables. These superglobal variables will automatically be filled with data around the HTTP request, around the web session, okay?

[00:03:08]
So for example, if I try this again, and do this, and hit convert, it's going now to conversion results, so I can see that now. And I'm not sure if you can realize there that there is a question mark at the end of the URL. You say, did I add that question mark?

[00:03:31]
No, not really, why do I have a question mark there?
>> Student: URL parameters?
>> Maximiliano Firtman: URL parameters. Actually, by default, the form is trying to send all the values by URL, also known as the query string. The query string is what you have after the question mark. But maybe the question is, okay, it's for the values, but where are the values?

[00:03:59]
I'm not seeing the values and I actually type some values, why the values are not there? Well, the browser will send the values using the html semantic name attribute, an attribute that I haven't set. I have an id because Ids for the label or for CSS, but I need to also set a name.

[00:04:21]
I'm not sure if you have used this before, the name is actually the key that is going to use to send the data to the server. So name amount and for the select you also have name=crypto. Okay, so I have a name here and name here. With that, now, we can go back, refresh, and say something.

[00:04:48]
Well, actually, having 10,000 Bitcoins is like a lot, right? But anyway, let's start with 3 Bitcoins. That is still a lot, but if I convert now, if you look at the top, this is my URL. So I have amount equals 3 and crypto BTC, so I can see the variables from my form there.

[00:05:10]
So, okay, how can I get those values? Well, everything that goes there in the URL, it's actually the superglobal variable get. And you can see on the description it says it's an associative array, that's an array of where the keys are something different than an index. So it can be a string, for example, variables pass to the current script via the URL parameters.

[00:05:40]
So that means it's an array, so I can actually go and read, for example, the amount, okay? That's how you get it and I can create the local variable for that. And I can also create another one for the crypto, I should call that crypto code or something like that, good idea.

[00:06:04]
So, I'm creating local variables now that are getting the value from the superglobal variable collection GET,
>> Maximiliano Firtman: Okay?
>> Maximiliano Firtman: So now just to see if this work, I will do an echo saying, look at this, I can even write the html here because the output is the html buffer.

[00:06:32]
Let's say you want to convert amount from $crypto, I'm using double quotes, So I can use interpolation within the string.
>> Maximiliano Firtman: So now I can go back here, refresh, and I can see that you want to convert 3 from BTC. Or maybe from, I don't need from 3 BTC and I think that should be good enough.

[00:07:04]
>> Maximiliano Firtman: But now I don't need the form, because these are URL parameters, I can actually go to the URL and change this, and it works, okay? So you can see that it's not tied to the form, it's like just the form. It's just forming the URL for me, okay?

[00:07:20]
So then this is actually how you make a search and shine or things like that. You receive an argument at the top and actually, today, you can even index it at Google. You can even index different URLs with different arguments there, and then different results on the page.

[00:07:37]
And remember, if you look at the page source, 300 BTC, server side render, it's part of the html. It's a static content.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now