MY mENU


Monday 26 March 2012

Variables in PHP

Variable Types
Variables play an important role in PHP, as they are places for holding values. In PHP, there is no need to declare variables. Variable can hold eight different data types:
  •  bloolean,
  •  integer, 
  • float, 
  • string, 
  • array,
  •  object,
  •  resource, 
  • NULL.
PHP is a weakly typed language. This means that variable type varies depending on what is stored in the variable at the time. For example, if we have a variable $a, when $a = 0, $a is an integer type variable. If later we set $a = "New", then $a becomes a string type variable.

Variable Name:
A variable name always starts with a $, followed by a letter or an underscore. The rest of the variable name can be a letter, a number or an underscore.

For example, $dog is a valid variable name, while @dog is not (@dog does not start with a $).

Variables in PHP are case-sensitive. For example, $Employee and $employee are two different variables.

Variable Scope:
In most cases, variables are local in scope. This means that variables declared within a function cannot be accessed outside of the function, and variables declared outside of a function cannot be access within the function.

To make a variable global, it must either be declared as global specifically, or it must be accessed using the $GLOBALS array.

No comments:

Post a Comment