TUTORIALS » PHP / MySQL » PHP essentials (guided tutorials)Variables in PHPPage 1 of 2
Variables are one of the keystones of programming. Putting it simply, variables are keywords or letters that serve as "memories" to store numbers, strings (text), or even tables of data.
The basic syntax to define a variable is by writing a dollar sign ($) followed by the desired name of the variable (without spaces), the equal sign, and the desired value for the variable. Example:
Remember, in PHP all strings must be written between quotation marks. On the other hand, when you store a number in a variable, it is not necessary to put it between quotation marks (although you can do it if you want to). Operations with variablesOnce you have defined a variable and its value, you can retrieve that value, print it, change it, make calculations with it, store the result in another variable, etc.Example:
Replacing variablesYou can replace the value of a variable with a new value.Example:
Concatenation of variablesYou can also join the value of two or more variables in a single string. This textual combination of strings is called concatenation.Example:
In the next tutorial you will learn more about concatenation of strings, and the comparison of single quotes and double quotes. Now go to the next page of this tutorial for a short practice on the use of variables.
|
|