PHP Variable : Variable Naming Rules

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 :

[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*

3. Declaring PHP Variable :

<?php
$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

<?php
$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.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>