PHP Variable
1. Rules for PHP variable names:
- Dollar Sign is used to represented a Variable in PHP followed by the name of the variable.
- PHP Variable name is case-sensitive.
- PHP Variable name must begin with a letter or the underscore character
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- A variable name should not contain spaces
- 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.