4: How to Create PHP Variables | PHP Tutorial | Learn PHP Programming | PHP for Beginners

Sharing buttons:

so in the last episode we talked about

how to write something on the screen

inside the browser and in this episode

we're going to talk about what a

variable is

now a variable is something you're going

to use quite a lot in PHP essentially

what it is is when we take a name and

set it equal to something so to kind of

give you guys an example of a variable

let's actually go and write one out so

we're going to write dollar sign which

means that now we're creating a variable

and then we need to give it a name now

in my case I'm just going to call it

name as a name and as you guys can see

it did actually turn green because it's

recognized as a variable inside my code

editor now now we have a variable called

name and we need to set it equal to

something meaning that right now I'm

going to set it equal to a string called

Daniel and just to refresh it for you

guys remember every time we put

something inside quotation marks it's a

string inside PHP so right now we have a

string called Daniel which is set equal

to name if it were to save this refresh

my browser you guys to notice that

nothing happens and that's because right

now we just simply said that this is

equal to this with an X you tell it to

write something on the screen so if we

go down to the next line and say echo

variable name and then refresh my

browser again now you guys will notice

that it did actually write out Daniel so

what we can do here is we can actually

go ahead and just give you know a

variable any kind of you know maybe a

calculation or a you know a string of

text like we did here or even you know

functions we can create all kinds of

things instead of equal to a name and

then reference to it later on we want to

use it for something now why would we

want to use this though well let's say

and I'm just giving you guys a small

example here but let's say I have a line

of text called Daniel is a handsome

fellow now if I were to write this out

on the screen like so but I'd you know

I'm kind of thinking to myself but what

if we have a text input inside our

website where people can write something

in it

and decide for themselves who is a

handsome fellow so I want them to be

able to change the variable depending on

what they write in an input so what we

can do is we can simply delete Daniel

and because this is seen as a string we

do need to separate it from PHP code

meaning that if I were to echo out

variable name because variable name is

PHP code and this is a string we need to

put in a separator meaning that we need

to write dot or punctuation in between

the PHP code and when the string starts

I'm going to save this and now you guys

will notice that it prints out Daniel is

a handsome fellow because name is that

equal to Daniel if it were to remove the

punctuation mark you guys like to see

that we get an error message so we do

need to make sure we put this in here

when we do combine PHP code with strings

now this was basically what I wanted to

show you guys when it comes to variables

but since I mentioned that so many times

I think I should show you guys how to

write an input you know create an input

and echo out the sentence here depending

on what name you write inside the input

on the website it's just a nice thing to

know so you don't have to follow along

here you can skip the next episode if

you don't don't want to learn this I'm

just going to show you guys how to do it

so let's say I have inside my body tag

outside the PHP tags a form which we did

actually learn about in our HTML CSS

lessons so if you don't know about forms

yet I do recommend you guys go back and

learn about it so inside the form tags

I'm going to just create an input and

actually have it inside my form tags

there we go now this input is going to

have a name equal to and this name is

going to be name or I could actually say

person just so we don't have name

written everywhere and I'm going to go

out and set the type for this input as

text because it is text essentially now

what I'm going to do here is I'm

actually going to go ahead and save this

and inside perform I have to set a

method and an action well we can't we do

actually not have to set an action but

we definitely need to set a method so

actions are usually used when you do

want to pass this information to another

document but since we're going to stay

on the same page we're just going to use

method so we're going to use a method

called gets which means that now once I

do write something in the input and hit

a button called submits it's going to

take this text that I wrote and pass it

into the URL and again we're going to

learn about that in a future episode but

just so you guys can see an example now

let's go ahead and save this and refresh

the browser so right now you guys can

actually see that it says you know I can

write something and submit it if I

submit it you're going to notice that in

the URL

it says dollar sign person is equal to

blah blah blah blah

what I just wrote so what we can do here

is we can we can actually go down to the

variable and set dollar sign name equal

to whatever we write up inside the input

field so right now I can actually set it

equal to dollar sign underscore gets and

then I'm going to create the brackets

and inside the brackets I'm going to

write single quotes then I'm going to

write the name that we gave the input as

a name so right now we call the person

I'm going to save it so right now name

is that equal to dollar sign underscore

get which means that it's going to get

this thing from the URL and pass it into

this you know here which means that name

is now equal to what we wrote inside the

input and then we're going to take the

sentence down here we're just going to

take name and put it into the sentence

so if i refresh we're probably going to

get an error okay we don't because I did

actually have something up here from

before but if I want to go back to the

previous page we do actually get an

error because right now we didn't

actually write anything yet so now this

is error message because variable name

is not set yet because we didn't

actually write anything is a handsome

fellow so would you actually to write

something get rid of this Arum we can

actually write something else

won't show an error and it won't

actually show anything until we actually

submitted but for now let's just go

ahead and do it like this I'm just going

to write Jason again because Jason is in

fact a handsome fellow and submit it it

now says Jason is a handsome fellow so

this is just an example of how you can

pass variables into something like this

and how you can use it on you know just

a teeny tiny examples and really

variables are used in so many powerful

ways and this is just a really really

basic example of how he can use it so

hope you guys enjoyed this episode this

is really all I want all I wanted to

show you guys in this episode so in the

next step so we're going to talk about

this a little bit further and do

something cooler so hope you guys

enjoyed and I'll see you guys next time