Tuesday, 21 July 2015

PHP Variables

Like other programming languages, PHP variables are used to store some values which can be changed or removed by any time.
PHP variables can be declared by specifying the name of the variable starting with a $ sign. For example,
$message = "Welcome to PHPPOT";
$count = 5;
In PHP, we need not to specify the data type of a variable while declaration. Rather, the type of initialized value of that variable can be taken as the variable type. In above example code, $message is string variable where as $count is int, based on their initialized value.
php_variable

PHP variable naming conventions

We need to follow the following rules while declaring variables in PHP script.
  • Variable name can hold alphabets, numbers.
  • Special characters except underscore(_), are not allowed in variable name.
  • We can not reassign PHP $this variable, since it is the pre-assigned.
  • And, a variable name should not contain white spaces.

Types of variables

PHP variables can be categorized by their visibility among the program. We can seen these differences while discussing about variable scope. These are,
  • Global variables – These variables can be recognized from any where in the program. But, within some independent block structures like functions, we need to access this variable using global keyword.
  • Local variables – The variables that are defined within some specific block like functions, are called as local variable. Such kind of variables can be visible with that particular block only.
  • Super globals – These are also like PHP global variables. But, these are predefined global variable, for example, $_GET, $_SERVER and etc. This type of variables can be accessed from anywhere without global keyword or anything.

0 comments:

Post a Comment