Variables and data type is very important part of any language .PHP also has various types of data type.A Variables can store different types od data and different types of data can do different things.
Mainly In programming languages a variables is a container which is contains a value which is volatile types.A data type is a types of Variables.
There are following some rules to define variables:
1- Letters, numbers, underscore (_) are allowed in variable names.
2- Spaces ( ), non-alphanumeric characters are NOT allowed in variable names.
2- Variable names CANNOT start with a number!
Example to create and assign variables:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?php // Defines an integer $noOfStudent = 12; // Defines an double $priceOfItem = 13.15; // Defines a boolean $isItem = true; // Defines a string $welcomeMessage = "Hello World"; ?>
How to assign Data types
Mostly languages require that you initialize the variable before using it.Specifying what type data you will store.In PHP you don’t need to initialize variable before it use.PHP interanlyy has capability to evaluate the data when you assign it to the variable and then stores it as the appropriate type.
PHP automatically converts datatype when it will need to performs operation like addition of float and integer.
$a = 1; # PHP stores it as an integer
$b = 2.1; # PHP stores it as a float
$sum = $a + $b;
In other language the addition of two number different data type is not possible,but in PHP its possible,PHP first convert integer to float then add to $b variable without any Error.
There are following types Datatype
Integer: A whole number (no fractions), such as –43, 0, 1, 27, or 5438. The range of integers that is allowed varies, depending on your operating system, but in general, you can usually use any number from –2 billion up to +2 billion.
Character string: A series of single characters, such as hello. There is no practical limit on the length of a string.
Boolean: A TRUE(1) or FALSE(0) value.