In previous chapter we learned about basics of PHP including php syntax, commenting in php and how to output in php using echo. In this chapter we will learn all about PHP variables.
What does Variable stands in PHP ? For a new learner this would be the most important thing to be learned. Variables are “containers” for storing information.
In Simple terms, if you want to store any information and retrieve it later, you need to store that information in a VARIABLE.
Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
[notification type=”notify” title=”To be Noted”]$this is a special variable that can’t be assigned.[/notification]
If we want to make a variable to store Names of students of class a good variable would be $Name instead of $blahblahblah. Again I would like to recall variables are CASE SENSITIVE. $footballmatch and $Footballmatch are different.
<?php $name = 'David'; $scoreNumberValue = 48; $scoreStringValue = '48'; /* $name contains David, we can use echo command to display David */ echo $name; /* Now we also have used two more variables to store data, and same data is stored in both that is 48, but there is a small difference. Case 1 $scoreNumberValue = 48; this variable stores number 48 and this can be used for mathematical calculations. Data stored is without quotes. Case 2 $scoreStringValue = '48'; this variable stores data in quotes, so php considers this as more of a string, so can not be used for mathematical calculations */ ?>
Hmm, that was easy, I hope you all learned something good out of this tutorial. Well this is beginning of something very good. The next chapter would cover Arithmetic Operators in PHP i.e how to do addition, subtraction, division, multiplication in php.
If you have any questions feel free to drop them in comments.