In this video.
I'll introduce you to variables in pig HP.
First let's make a change to our code at the moment.
We just have one line of code that's printing out this message.
So above this line let's add another line that says message
equals.
Again.
We just created a variable called message.
I'll explain this in more detail in a moment.
Then let's change our echo instruction to use this variable instead of the message we had earlier.
We'll save this file.
Now if we open the file in a browser by going to a local host slash hell out up here.
P we see the message has changed.
It now says hello again.
So what did we just do.
We just added a variable variables.
I used to store information for the program to use think of variables simply as containers that hold
some information.
Once you create a variable in your code you can use the data contained in that variable throughout the
rest of your program variables also allow us to label data with a descriptive name sir.
Our code is easier to understand.
To create a variable in p p.
You just need to write its name followed by a space in equals sign another space and then its value.
So here we've created a variable called message and assigned the value of helo again to it.
And don't forget the semicolon that we have to use at the end of each instruction.
We then use that variable in the echo statement.
When this line of code is executed on the web server the echo statement takes the value of this variable.
Hello again and prints it out to the web browser.
Variables in P HP are represented by a dollar sign followed by the name of the variable.
You can use a mixture of lower case and upper case letters numbers and underscores father name.
So these are valid examples of variables.
This one however is invalid as it contains a hyphen which isn't allowed.
You can call variables whatever you want.
However it's a good idea to always call them something meaningful so it makes your code easier to understand.
So for example let's say you have a variable in your code that contains an error message instead of
a variable called X like this.
Your code is easier to understand if you call the variable error message like this.
If you use this variable later on in your code it's more obvious to whoever is reading the code which
could be you.
What the variable contains.
P HP is also case sensitive.
So this variable which begins with a lower case M is a different variable to this one which begins with
an upper case.
Although you can name your variables whatever you want based on these rules I recommend always using
all lower case and separating words with underscores like these.