25: Different Superglobals in PHP | PHP Tutorial | Learn PHP Programming | PHP for Beginners

Sharing buttons:

today we're going to talk about

something called superglobals inside PHP

and basically a superglobal is when we

have a variable that has already been

created for us inside the PHP code

itself so I do actually have a couple

examples up here I have five examples

out of many because there's actually

quite a few out there but these are the

five that I want to talk about in order

to you know actually have us be able to

do something with PHP code like creating

a login system so these are the ones who

need to know about in order to do that

sort of thing and these are the ones

that we're gonna focus on the next

couple of episodes now actually getting

pretty close to be able to actually

create something inside PHP like I said

a login system I keep using that as a

reference but we can for example create

that after the next couple of episodes

so talking about superglobals we can

actually go ahead and take the first one

up here for this episode called Global's

now the basic idea behind Global's is

that we can actually go ahead and refer

to any kind of variable inside the

global scope no matter where we are

inside our document whether we're inside

a you know like a global scope or inside

a local scope so as you guys can see you

do actually have a variable here called

X which is equal to five which is inside

the global scope and I have a function

down here that has a variable inside of

it called Y which is equal to ten and

this one is inside the local scope of

the function so let's say I want to call

on this variable up here called X inside

the function now the typical way we want

to do it is to actually go in and you

know create some kind of variable inside

the parentheses like called X which

would then refer to this variable X up

here when we actually call it out so

instead we can actually go inside the

function and say we have a what you call

a global a global x' with an S behind it

brackets quotes or single quotes doesn't

really matter and then you're gonna

write the name of that to a variable you

want to use inside this function now

doing it this way would actually you

know it's not quite the same as putting

in the variable up here into parentheses

because in that you can actually change

the variable inside here when actually

call it out later on if you

if you go back to the function episode

we have in our series here you can you

know see then we can actually change the

name of the variable in here and call

out another variable using just a


now using Global's like this you will

always refer to the same variable

outside the function so if I were to

type the name of the actual variable I

want to call on in here I can actually

say X because right now this variable up

here is called X the dollar sign is just

a symbol that means that this is a

variable so the actual variable name is

X so if I go ahead and say you know X in

here and if I actually watch will echo

it out I can actually do that as well

so I'm gonna echo out Global's X and

then I'm gonna call the function down

here so I'm just gonna go ahead and

write something because that's the

actual name of the function go inside my

browser and you guys have Steve you got

five because right now I'm calling a

superglobal called X which is out here I

haven't actually called it inside the

parentheses and we still get an answer

out of it so this is the way we can use

for example a super global like Global's

inside our code and sometimes think it'd

be quite nice to have something like

this but I would not recommend using it

you know all the time because you know

this is for a different purpose than

putting you know a variable inside the

parentheses you don't use it for the

same thing and like I said you guys can

go back and look at my functions episode

to see you know why you want to use it

inside here but this should only be used

in special cases and you you guys might

have a reason to actually use it but

don't use it as a substitute over simply

calling a variable inside the

parentheses so I hope you guys enjoyed

this episode and next one we're going to

talk about something called post and get

super Global's and this actually really

important for you to know if you want to

create like a login system or something

again I'm using a login system as a

reference here you know as an example to

tell you guys you can actually start

creating this sort of thing once you

start learning these sort of things but

you will actually be able to you know

pretty soon create a login system even

though you might not right now see how

it's possible you know with all the

stuff we've learned so far but I hope

you guys enjoyed this episode

and I'll see you guys next time