PHP variables

PHP Variables are fundamental parts of any programming language. A variable is simply a container that holds a certain value. Variables get their names because that certain value can change throughout the execution of the script. It is this ability to certain changing values that make variable so useful.

For example, consider the following simple PHP script:

Echo 4 +2 ;

As you might imagine, this code outputs the number 4 when it is 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 7 + 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 a variable $x and $y to any two values you want, either at some other place in your code, or as result of input from the user. Then, when you run the preceding line of code, the script outputs the some of those two values. Re-run the script with different values for $x and $y, and you get a different result.

Naming PHP Variables

A variable consist of two parts: the variable’s name and the variable’s value.  Because you will be using variables in your code frequently, it’s best to give your variable’s name you can understand and remember.  Like other programming languages, PHP has certain roles you must follow when naming your variables.

  • Variable names being 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 variable names longer than 30 characters are somewhat impractical.

Here are some examples of PHP variable names:

$my_first_variable

$another variable

$x

$_123

Creating PHP 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.

Many programming languages prevent you from using a variable without first explicitly declaring it.  But PHP lets you use variables at any point just by naming them.  This is not always the blessing you might think; if you happen to use a non existence variable name by mistake, no error message it generated, and you may end up with hard to find bug.  In most cases, though, it works just fine and is a helpful feature.

When you declare a variable in PHP, it’s a good practice to assign a value to it at the same time.  This is a 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.

Here is an example of decrements and initializing a variable:

$my_first_variable = 3;

This creates the variable called $my_first_variable, and uses the = operator to assign It the value of 3.  Looking back at the addition example earlier, the following script creates two variables, initializes then with the value 5 and 6 than output their sum(11).

$x =5;

$y =6;

Echo $x + $y;

Leave a Reply

Your email address will not be published.