Data Types in PHP language


Data types in PHP

Data type:
- the name itself indicating, data type means type of data.

Types of Data types in PHP:
- PHP Data types are mainly divided into 3 types:

Scalar Data Types: integer, double, string, Boolean
Composite Data Types: array, object
Special Data Types: NULL, resource

Scalar Data Types:
- allow us to create variables or constants; which can hold single value

1. Integer: means whole number, i.e. number without floating point
Ex: 122, 2000, -400, etc
$playerScore= 10; // playerScore variable is holding integer type of data

2. Double: means decimal or float number, i.e. number containing floating point
Ex: 3.142, 9.8,-48.32 etc.
$playerSpeed=3.5; // playerSpeed variable is holding double type of data

3. String: indicates sequence of characters enclosed within pair of double quotations or single quotations i.e. Textual data.
Ex: “Hello World” , ‘Hello JavaScript’ etc.
$playerName=”Manjunath”; // playerName variable is holding string type of data

4. Boolean: indicates logical or conditional result
Boolean data type has two values true and false value.
integers 0 and -0 , double 0.0 and -0.0, string with value " ", string with value "0", boolean false value, an array with zero elements, the special type NULL, and XML objects created from empty tags, are considered as false in PHP
Ex: $isGameOver=true; // isGameOver variable is holding boolean type of data

Composite Data Types:
- allow us to create variables or constants; which can hold multiple values.
- Composite types are composed of scalar, composite and / or special type data values
Ex: Arrays & Objects
$names = array("rama","ravi"); // names variable is holding array type of data
$point1 = new Point(); // point1 variable is holding object type of data

Special Data Types:
- allow us to make a variable point to some external resource OR not to point anywhere.

1. NULL: indicates the nothing.
A variable of type object points or refers to another memory location; if it should not point to any memory location then we assign a value null to it.
$point1=null; // point1 variable is holding NULL type of data

2. resource:
Indicates a variable is pointing to an external resource
$file = fopen("index.php","r"); // file variable is holding resource type of data

Interview Questions:

1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a