Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
[Edit] Using Variables in PHP
Variables are a fundamental part of any programming language. A variable is simply a container
that holds a certain value. Variables get their name because that certain value can change
throughout the execution of the script. It ’ s this ability to contain changing values that make
variables so useful.
For example, consider the following simple PHP script:
echo 2 + 2;
As you might imagine, this code outputs the number 4 when it ’ s run. This is all well and good; however, if you wanted to print the value of, say, 5 + 6 instead, you ’ d have to write another PHP script, as follows:
echo 5 + 6;
This is where variables come into play. By using variables instead of numbers in your script, you make the script much more useful and flexible:
echo $x + $y;
You now have a general - purpose script. You can set the variables $x and $y to any two values you want,
either at some other place in your code, or as a result of input from the user. Then, when you run the
preceding line of code, the script outputs the sum of those two values. Re - run the script with different
values for $x and $y , and you get a different result.
[Edit] Naming Variables
A variable consists of two parts: the variable ’ s name and the variable ’ s value. Because you ’ ll be using
variables in your code frequently, it ’ s best to give your variables names you can understand and
remember. Like other programming languages, PHP has certain rules you must follow when naming
your variables:
  • Variable names begin with a dollar sign ( $ )
  • The first character after the dollar sign must be a letter or an underscore
  • The remaining characters in the name may be letters, numbers, or underscores without a fixed limit

Variable names are case - sensitive

( $Variable and $variable are two distinct variables), so it ’ s worth
sticking to one variable naming method — for example, always using lowercase — to avoid mistakes.
It ’ s also worth pointing out that variable names longer than 30 characters are somewhat impractical.
Here are some examples of PHP variable names:
$my_first_variable
$anotherVariable
$x
$_123

[Edit] Creating Variables
Creating a variable in PHP is known as declaring it. Declaring a variable is as simple as using its name in
your script:
$my_first_variable;
When PHP first sees a variable ’ s name in a script, it automatically creates the variable at that point. When you declare a variable in PHP, it ’ s good practice to assign a value to it at the same time. This is known as initializing a variable. By doing this, anyone reading your code knows exactly what value the variable holds at the time it ’ s created. (If you don ’ t initialize a variable in PHP, it ’ s given the default value of null .) Here ’ s an example of declaring and initializing a variable:
$my_first_variable = 3;
This creates the variable called $my_first_variable , and uses the = operator to assign it a value of 3. (You look at = and other operators later) Looking back at the addition example earlier, the following script creates two variables, initializes them with the values 5 and 6 , then outputs their sum ( 11 ):
$x = 5;
$y = 6;
echo $x + $y;