PHP Variables

Variable is an identifier used to store value. It can be changed or removed by any time. Variable name should start with a $ sign. The syntax is,

$variable_name = value;

While declaring variable we need not to specify its data type. In PHP, the data type of its value is taken as the variable type. For example,

$message = "Welcome to PHPPOT";
$count = 5;

In this example code snippet, $message is string variable where as $count is int, based on the value.


PHP variable naming conventions

We need to follow the following rules while declaring variables in PHP script.

  • Variable name should be in alphanumerics.
  • Special characters are not allowed except underscore(_).
  • It should not be pre-defined valiable. For example, $this.

Types of variables

In PHP there are various type of variables. This categorization is based on the the scope of the variable.

  • Global variables – These variables has global scope and visibility to access from any where. For accessing them in separate file, class or function we have to use global keyword. For example global $global_variable_name.
  • Local variables – The variables that are defined and used within some specific function or any other program block are called as local variable.
  • Super globals – These are predefined global variables. For example, $_GET, $_SERVER and etc. It can be accessed from anywhere without global keyword.

This PHP code tutorial was published on May 17, 2013.

↑ Back to Top