Data Type in PHP:
The Data type in any programming language refers to
- The type of data that a variable of that language can hold.
- The concept behind the data-type is to efficiently utilize the memory space and identifying the type of data.
- So every data-type in every programming languages have their predefined characteristics i.e. how much memory will be allocated for a particular data type.
Note: In PHP you do not need the declare the type of a variable in PHP. This does not mean PHP does not have datatype.
PHP is a loosely typed language:
PHP is a loosely typed language means
- Your do not need the explicitly declare the type of the variables like in other programming languages (C, C++, JAVA).
- The compiler implicitly declare the datatype of a variable depending on the value assigned to the variables.
Following are the data-types in PHP:
- Whole numbers can be stored.
- No Decimal points are allowed.
$var=12; //only integer values
2. Float or Double:
- real numbers can be stored
- String is sequence of characters.
- Need to be enclosed in single inverted or double inverted commas.
- In single inverted or heredoc(<<<) syntax it is called literal string. No variables will be rendered within single inverted comma.
- In double inverted comma a variable value can be rendered.
echo ‘Value of var1 is $var1’;
echo “Value of var2 is $var2”;
Output: Value of var1 is $var1 Value of var2 is def.
- it can hold only two values either TRUE of FALSE
- used in conditional statements.
echo ‘I Love Tutorialstutor’;
echo ‘You know nothing about php.’;
- Represented as index and value for that index.
- Index may be integer or string.
- Three types of Array – Single dimensional, Multidimensional and Associative.
6. Objects: Instance of a class or a variable of a class data-type.
7. Resources: Special variables to hold external resources like the Database Connection.
8. NULL: A special data-type which can hold only NULL or null.