PHP Variable

What is Variable?

PHP6 introduce 64bit integer
A variable is a representation of particular value. Simplay we can call this “containers” for storing information. By assinging a value to variable, you can reference the variable in other places in your script and we can change the values of variables. Choose the good and meaningful name for variable.

Rules For Variable

  1. Add a $ dollar sign infront of the variable name
    Ex: $book_name;
  2. PHP variable name cannot begin with a numeric character and must start with a letter or the underscore character
  3. PHP variable can contain numbers (0-9), letters (a-zA-z) and underscore (-)
  4. PHP variable name are case sensitive. $VARIABLE and $variable are two different variable
  5. Variable name can be any length

Variable Types

Scalar Types

  • boolean
  • integer
  • float
  • string

Compound Types

  • object
  • array

Special Types

  • resource
  • NULL

Sample Program

1 <?php
2 $string_value="Hello world!";
3 $integer=5;
4 $float_or_double=10.5;
5 $boolean=true;
6 $array=array(1,2,3);
7 $object=new Classname();
8 $null_variable=NULL;
9 ?>


  • PHP constant are similar to variable and constant is case-sensitive by default.
  • A constant is an identifier for a value that cannot change during the execution of a script.
  • Constant dont have a dollar($) sign before their name.
  • Constant are usually uppercase to show their difference from a normal variables.
  • define() function is used to define a constant. define() function require the name and value of the name.
    1 define("USERNAME","bsourcecode");

Predefined Variables

PHP provides a large number of predefined variables to all scripts. It is also called ‘Superglobals’ or automatic global variables.

  • $GLOBALS — References all variables available in global scope
  • $_SERVER — Server and execution environment information
  • $_GET — HTTP GET variables
  • $_POST — HTTP POST variables
  • $_FILES — HTTP File Upload variables
  • $_REQUEST — HTTP Request variables
  • $_SESSION — Session variables
  • $_ENV — Environment variables
  • $_COOKIE — HTTP Cookies

Leave a Reply

Your email address will not be published. Required fields are marked *