PHP Tutorials

PHP Variables & Data Types

Variables & Data Types in PHP

Variables in PHP

In this Tutorial you'll learn about Variables and different Data Types in PHP. This includes - what is a variable, naming conventions of a variable in PHP, various data types namely, String, Numeric and Array data types.

What is a Variable?

              Variables are nothing but identifiers to the memory location to store data. We can create any number of varibles. In PHP all the variables begin with a dollar sign "$" and the value can be assignes using the "=" operator as shown below:


$Name, $Name1, $First_Name, $Last_Name

$Name = "David";

$Age = 16;

              Another important thing in PHP is that all the statements must end with a semicolon ";". In PHP we needn't have to specify the variable type, as it takes the data type of the assigned value. From the above example we understand that '$Name' is of Data type String and '$Age' is of type Numeric.

Limitations in Variable Naming:

              PHP has no limit on the length of variable name as in the case of other programming languages. The variable name must start with a letter or underscore. The variable name must be a combination of letters, numbers and underscores. Other characters such as *, +, #, @ are not allowed and causes error if used.

Data Types in PHP

In PHP the data type of a variable is not set by the programmer. PHP decides the data type of variables after interpreting the web page. Data Types in PHP include:

  • Numeric data type
  • String data type
  • Array data type

Numeric data type

Numeric data type is used for number. There are two different numeric data types:

  • Integer
  • Double

Integer Data Type

       Integer data types contain whole number values.


$intmark = 100;


      Double contains floating point numbers.


$doubtotal = 99.5;

String Data Type

     String data type is used to contain textual information or letters. The value is assigned within quotes as shown below.


$strName = "David";

$strId = "David123";

String Concatenation

String Concatenation is a process of adding two strings. This is done by attaching one string to the end of another string. This is done by using '.' (period) operator as shown below:


$strFirst_Name = "David";

$strSpace = " ";

$strLast_Name = "John";

$strName = $strFirst_Name.$strSpace.$strLast_Name;

echo $strName;


David John

Array Data Type

Array data type is used to contain many values in a single variable. Each array element can be retrieved by using the array variable name and its key/index value. We can declare an array variable in different ways as shown below:


$arrMark[0] = 95;

$arrMark[1] = 88;

$arrMark[3] = 77;


$arrMark = array(95,88,77);

In the above example, the variable 'arrMark' contains three different values. In the first method the array elements are stored using the key values (0, 1, 2), but in the second example the keyword 'array' is used to assign values to the array variable. By default, array key element starts with 0.

The array elements can be accessed by using their key values. The different marks stored in the array elements can be added together to obtain the total marks as shown below:


$intTotalMark = $arrMark[0] + $arrMark[1] + $arrMark[2];

echo $intTotalMark;

Output: 260

In the above two examples we have used the key values as numbers, but it not necessary that the key value must be a number, it can also be a string as shown below:


$arrMark['maths'] = 95;

$arrMark['english'] = 88;

$arrMark['science'] = 77;

$intTotalMark = $arrMark['maths'] + $arrMark['english']+$arrMark['science'];

echo $intTotalMark;


Please like, +1, link to and share this SmartWebby resource if you found it helpful. Thanks!