Variables History in PHP

Published July 11,2010 in PHP

What Is variable??

In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents.

A variable name in computer source code is usually associated with a data storage location and thus also its contents, and these may change during the course of program execution.

variable history

Although you've seen variables in the previous Tutorials, you may wonder what they are exactly. Variables in PHP give you access to memory storage in a part of a computer called RAM, or random access memory. RAM is a volatile medium for storing information.

That is, it all disappears when you shut off the machine. The computer sees this  memory as a long array of memory cells that reside in sequential addresses. In PHP, however, you cannot actually get to memory at this level.

You must use a variable. When you assign a value to a variable with $result = 2 + 5, or retrieve the value of a variable with print($result), PHP takes care of matching the variable name you specified with the right piece of memory in RAM.

Every use of a variable in PHP begins with $, followed by letters, numbers, or underscores. After the $, the first character must be either a letter or an underscore. Follwing Table shows examples of some valid and invalid variable names.

Using dollar signs in variable names has a long tradition in programming languages. BASIC, a popular language created in the 1960s, uses them, and so does PERL. Other languages, such as C and Java, do not. The dollar sign helps you distinguish a variable from a function, a keyword, or any other part of PHP.

You may wish to consider $ part of the variable name, or you may choose to think of it as an operator that references memory with a given name.

When speaking about variables, it's more common to say "user equals three" rather than "dollar sign-user equals three."

In written language, which lacks nuance, it's common to see $ included, but it's not necessary. In both cases, you will be understood, and it's mostly a matter of personal and community preference.

 

Name Validity Comment
i Valid Single-letter variables are good for temporary purposes, such as loop counters.
1 Invalid The first character following the dollar sign may not be a number.
_1 Valid Traditionally, variables that begin with an underscore have special meaning to the local namespace.
firstName Valid Variables that look like words help make your scripts easier to understand.
7Lucky Invalid The first character following the dollar sign may not be a number. Use Lucky7 instead.
~password Invalid ~ is not an alpha character and may not be used in variable names.
Last!Visit Invalid ! is not an alpha character and may not be used in variable names. Use LastVisit or last_visit instead.
Compute-Mean Invalid  - is not an alpha character and may not be used in variable names. Use Compute_Mean instead.



The equal sign (=) is used to set the value of a variable. This is called the assignment operator. On the left side of the assignment operator is a variable that will receive a value. On the right side is an expression, which could be a simple string constant or a complex combination of operators, variables, and constants. The simplest form of assignment is from a constant expression. This could be a number or a string surrounded by quotes.


Here is the lists of some examples.

String Constants Integer Constants Double Constants
$myString = "leon"; $myInteger = 1; $myDouble = 123.456;
$myString = "\n"; $myInteger = -256; $myDouble = -98.76e5;

 

Most compiled languages, such as C or C++, require you to declare every variable along with the type of value that it will contain, and they require every code piece to state in advance what kind of values it is designed to work with. Most interpreted languages, such as PHP, allow variables to store any type of value and allow code units to work with any type of value.

PHP doesn't even require you to explicitly declare a variable before you use it. Instead, the first time you assign a variable with some value, it is created. This simplifies development and helps you produce and maintain working programs more quickly. It also can lead to bugs when you use a variable before initializing it.

Variables in PHP don't have designated types. Instead, the type of the variable is considered to be the type of the value that it contains. The type of value that variables contain may be changed at any time. For  example, assigning an integer to a variable that previously held a string converts the variable to an integer.

This is in contrast to C, where each variable has a designated type. Assigning a value to a variable of a different type will make C attempt to convert the value so that it fits the variable.

I will show, how to use "variable" in my next tutorial.



Like our site? Be a member of Coolajax fan page to get daily posts updates!

recommended-tutorial You May Like This Posts!

» PHP Data Types

» str_ireplace in php part II

» How to stristr function work?

» How to substr_count function works?

» Origins of PHP

  tuts_view Total Read: 654
blog comments powered by Disqus

Topics

Coolajax Fans

coolajax fan page on Facebook