Saturday, July 11, 2009

php3-variables

variables in php
it is very easy to declare variables in php because there is no need to declare their type like in java or c.
$ sign is used in the front of the variable .
$a is the variable

There are a few rules that you need to follow when choosing a name for your PHP variables.

•PHP variables must start with a letter or underscore "_".
•PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
•Variables with more than one word should be separated with underscores. $my_variable
•Variables with more than one word can also be distinguished with capitalization. $myVariable

Indirect References to Variables
An extremely useful feature of PHP is that you can access variables by using indirect references, or to put it simply, you can create and access variables by name at runtime.

Consider the following example:

$name = "John";
$$name = "Registered user";
print $John;



This code results in the printing of "Registered user."

The bold line uses an additional $ to access the variable with name specified by the value of $name ("John") and changing its value to "Registered user". Therefore, a variable called $John is created.

You can use as many levels of indirections as you want by adding additional $ signs in front of a variable


Superglobals
As a general rule, PHP does not support global variables (variables that can automatically be accessed from any scope). However, certain special internal variables behave like global variables similar to other languages. These variables are called superglobals and are predefined by PHP for you to use. Some examples of these superglobals are

$_GET[]. An array that includes all the GET variables that PHP received from the client browser.
$_POST[]. An array that includes all the POST variables that PHP received from the client browser.
$_COOKIE[]. An array that includes all the cookies that PHP received from the client browser.
$_SERVER[]. An array with the values of the web-server variables.

No comments:

Post a Comment