Php – Variables – POFTUT

Php – Variables

[rps-include post=6522]

Variables are used to store data in application. Variables can store data like string, text, integer, float number, logic value etc. Variables may defined different ways but there are some restrictions for variables names. Here some important things about variable naming.

$ Sign

All variables start with $ sign. It is like variable selector. Following example show valid and invalid variable named.


  • $age
  • $test1
  • $NAME
  • $SiZe


  • age
  • a$age
  • $test$
  • $9name

Variable Name Must Start with Letter or Underscore

Variable names can not start other than letter or underscore. If this is not obeyed will create fatal error.


  • $_age
  • $Test
  • $My_Test


  • $.test
  • $-age

Variable Name Cannot Start with Number

Variables can not start with numbers. This is one of the pitfalls new developers or learner fall.


  • $age1
  • $T1st


  • $1
  • $1Test
  • $2age2

Variable Name Contain Alphanumeric Characters

Variables can only contain alphanumeric characters. Alphanumeric characters consist of A-z , 0-9 , _ .

Variable Name Cannot Contain Spaces

Variable names cannot contain spaces. This will break up the name convention.


  • $my_test
  • $yourtest


  • $my test
  • $test your

Variable Types

As stated before variable may have different types. These type determination is done while assigning value to the variable. In the following example we assign an integer value 9 to the variable named $age and this will set the type of the $age as integer automatically.

$age = 9

Php have following primitive variable types where we will look all of them in the next chapters.

  • String
  • Integer
  • Float
  • Double
  • Boolean
  • Null
LEARN MORE  What Is A Variable and How To Define and Use Variables In Programming Languages Like PHP, Python, Java, C#, C/C++, JavaScript, PowerShell, Bash?

Variable Scope

Variables can be defined in different part of the code or in different files. What will happen if the same variable name defined in different files where these files are important current file. This will create some conflict about scope. Should we access all variables from current file or restrict the scope. We will also look variable scopes in next chapters.

[rps-include post=6522]

Leave a Comment