PHP Data Types
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
- String
- Integer
- Float [floating point numbers - also called double]
- Boolean
- Array
- Object
- NULL
- Resource
PHP String
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:
Example
Try it Yourself »
PHP Integer
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.
Rules for integers:
- An integer must have at least one digit
- An integer must not have a decimal point
- An integer can be either positive or negative
- Integers can be specified in: decimal [base 10], hexadecimal [base 16], octal [base 8], or binary [base 2] notation
In the following example $x is an integer. The PHP var_dump[] function returns the data type and value:
PHP Float
A float [floating point number] is a number with a decimal point or a number in exponential form.
In the following example $x is a float. The PHP var_dump[] function returns the data type and value:
PHP Boolean
A Boolean represents two possible states: TRUE or FALSE.
Booleans are often used in conditional testing. You will learn more about conditional testing in a later chapter of this tutorial.
PHP Array
An array stores multiple values in one single variable.
In the following example $cars is an array. The PHP var_dump[] function returns the data type and value:
Example
Try it Yourself »
You will learn a lot more about arrays in later chapters of this tutorial.
PHP Object
Classes and objects are the two main aspects of object-oriented programming.
A class is a template for objects, and an object is an instance of a class.
When the individual objects are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
Let's assume we have a class named Car. A Car can have properties like model, color, etc. We can define variables like $model, $color, and so on, to hold the values of these properties.
When the individual objects [Volvo, BMW, Toyota, etc.] are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.
If you create a __construct[] function, PHP will automatically call this function when you create an object from a class.
Example
Try it Yourself »
PHP NULL Value
Null is a special data type which can have only one value: NULL.
A variable of data type NULL is a variable that has no value assigned to it.
Tip: If a variable is created without a value, it is automatically assigned a value of NULL.
Variables can also be emptied by setting the value to NULL:
PHP Resource
The special resource type is not an actual data type. It is the storing of a reference to functions and resources external to PHP.
A common example of using the resource data type is a database call.
We will not talk about the resource type here, since it is an advanced topic.
Home » Aptitude Questions and Answers » PHP Aptitude Questions and Answers
PHP Data Types Aptitude Questions and
Answers: This section contains aptitude questions and answers on PHP Data Types.
Submitted by Nidhi, on May 22, 2020
This section contains Aptitude Questions and Answers on PHP Data Types.
1] There are the following statements that are given below, which of them are correct about data types in PHP?
- In PHP, variables can store different type of data that is allowed according to PHP data-types.
- In PHP, there is no need to use special keywords to specify the data type of variable.
- In PHP, we use the 'int' keyword to declare an integer type variable.
- We use predefined classes to specify data-types in PHP.
Options:
- Only A
- Only B
- A and B
- C and D
2] There are the following data types that are given below, which of them are supported by PHP?
- String
- Array
- Object
- Resource
Options:
- A and B
- C and D
- A, B, and C
- A, B, C, and D
3] Which of the following is the correct range of integer variable in PHP?
- -32768 to 32767
- -2,147,483,648 to 2,147,483,647
- -128 to 127
- None of the above
4] Which of following function is used to return data type of a variable and its value?
- get_type[]
- var_type[]
- var_dump[]
- getType[]
5] What is correct output of given code snippets in PHP?
- int[256]
- integer[256]
- string[3] "num"
- None of the above
6] What is correct output of given code snippets in PHP?
- int[256]
- integer[256]
- string[3] "num"
- None of the above
7] What is correct output of given code snippets in PHP?
- int[256] num1 is int[256]
- int[256] num1 is
- Syntax error
- None of the above
8] What is correct output of given code snippets in PHP?
- string[hello]
- string["hello"]
- string[5] "hello"
- None of the above
9] What is correct output of given code snippets in PHP?
- int[256]
- int[256] string[5] "Hello"
- int[256] string[3]
- syntax error
10] What is correct output of given code snippets in PHP?
- bool[true]
- boolean[true]
- bool[4] "true"
- boolean[4] "true"
11] What is correct output of given code snippets in PHP?
- array[3] string
- array[3] { [0]=> string[3] "ABC" [1]=> string[3] "PQR" [2]=> string[3] "XYZ" }
- Syntax error
- None of the above
13] In PHP, NULL is a data type?
- Yes
- No
14] What is correct output of given code snippets in PHP?
- NULL
- NULL[4]
- NULL[null]
- None of the above
15] Can we create a user-defined class in the PHP script?
- Yes
- No
16] There are the following statements that are given below, which of them are correct about resource type in PHP?
- The resource type is used to contain the reference to the function.
- The resource type is used to store the reference of external resources.
- The resource type is used for the database call.
- None of the above
Options:
- A and B
- B and C
- A, B, and C
- D
17] What is the correct output of given code snippets in PHP?
- India
- Error
18] What is the correct output of given code snippets in PHP?
- India
- Error
19] What is the correct output of given code snippets in PHP?
- object[Sample]#1 [1] { [""]=> string[5] "India" }
- object[Sample]#1 [2] { [""]=> string[5] "Australia" }
- Error
- None of the above