PHP Variable

1. Rules for PHP variable names:

  1. Dollar Sign is used to represented a Variable in PHP followed by the name of the variable.
  2. PHP Variable name is case-sensitive.
  3. PHP Variable name must begin with a letter or the underscore character
  4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  5. A variable name should not contain spaces
  6. Variable names are case sensitive (var1 and VAR1 are two different variables)

2. Regular Expression for PHP Variable :


3. Declaring PHP Variable :

$fname = 'Pritesh';
$lname = 'Taral';
echo "$fname, $lname";  // outputs "Pritesh, Taral"

$4site = 'not yet';     // invalid; starts with a number
$_4site = 'not yet';    // valid; starts with an underscore
$täyte = 'mansikka';    // valid; 'ä' is (Extended) ASCII 228.

Note :

[box]$this is a special variable that can’t be assigned.[/box]

How to Specify Type of the Data Variable ?

In PHP we can assign any type of data to variable i.e

$svar = "Hello World!";
$ivar = 4;
$ivar = 8;

In this example if we assign the integer value to the variable then Variable will acts as Integer Variable. Suppose we assign string value then it will acts as String Variable.