PHP Variables

Variables in PHP
In PHP, variables need not be declared separately before using them. You can introduce a new variable anytime you wish to use them. However it is recommended to declare your variables in chunk before using them, to avoid similar variable names while introducing new variables.

The data type of a variable introduced at anytime is same as the data type of the value being passed to it for the first time. So if a value of 5 is moved to a variable while declaring it, the variable will become integer.

The variable of data type String is created when some string value is passed to it. The simplest way to create a string variable is like this, $string = ""; Once introduced, this variable can be used at any subsequent PHP statement.

Variable names in PHP are case sensitive. So $category and $Category are different. Once a variable is declared it can used any where within the program. The following rules should be kept in mind while defining a new variable name

  • All variables in PHP start with $ sign.
  • A variable name must start with a letter or an underscore "_".
  • White space is not allowed in the name of a PHP variable. Instead one should use either underscore or Capitalize the first letter of second word to make the name readable when using multiple words while defining the PHP variable. Though you can use all small or all capital letters also.
  • I have never had to think about the maximum length of the name of a PHP variable. So I do not know this thing this time. :)
  • Only alphanumeric characters and underscore are allowed in PHP variable’s name.

Some valid variable Names in PHP
$categroy
$Category
$categorymaster
$categoryMaster
$category_master
$category_1

Some invalid variable Names in PHP
Category (Variable name does not begin with $)
$Category! (Only alpha numeric characters and _ allowed)
$category master (There should not be any space in between the two words)