PHP Data types

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:

1. Integer:

  • Whole numbers can be stored.
  • No Decimal points are allowed.

Example:

<?php 

$var=12; //only integer values

echo $var;

?>

2. Float or Double:

  • real numbers can be stored

Example:

<?php 

$var=12;

echo $var;

?>

3. String:

  • 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.

Example:

<?php 

$var1=’abc’;

$var2=”def”;

echo ‘Value of var1 is $var1’;

echo “Value of var2 is $var2”;

?>

Output: Value of var1 is $var1 Value of var2 is def.

4. Boolean:

  • it can hold only two values either TRUE of FALSE
  • used in conditional statements.

Example:

<?php

$var=true;

if($var){

echo ‘I Love Tutorialstutor’;

}else{

echo ‘You know nothing about php.’;

}

?>

5. Array:

  • 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.

Check Page Rank