Data types define the type of data a variable can store.

PHP supports 8 basic data types, which are divided into three categories:

1)  Scalar Types (single values)

  1. Integer
  2. Float (or Double)
  3. String
  4. Boolean

2)  Compound Types (multiple values)

  1. Array
  2. Object

3)  Special Types (unique values)

  1. NULL
  2. Resource

 

Scalar Types

Scalar types hold a single value. There are 4 scalar types in PHP:

1) Integer

Stores whole numbers (positive or negative).

Example:

  $age = 25; // Integer
echo $age; 

  Output:

25

2) Float (or Double)

Stores decimal numbers.

Example:

$price = 19.99;
echo $price;

Output:

19.99

3) String

Stores text (a sequence of characters).

Example:

$price = 19.99;
echo $price;

Output:

19.99

4) Boolean

Stores true or false.

Example:

$isStudent = true; // Boolean
echo $isStudent; 

Output:

1 (true is displayed as 1)

 

Compound Types 

Compound types can hold multiple values. There are 2 compound types in PHP:

5) Array

Stores multiple values in a single variable.

Example:

$fruits = ["apple", "banana", "cherry"]; // Array
print_r($fruits); 

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)

 

 6) Object

Stores instances of classes (used in object-oriented programming).

Example:

$fruits = ["apple", "banana", "cherry"]; // Array
print_r($fruits); 

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
)

 

Special Types 

Special types are unique and don’t fall into scalar or compound categories.

There are 2 special types in PHP:

7) NULL

Represents a variable with no value.

Example:

$address = null; // NULL
echo $address;

Output:

(nothing, because null means no value)

 

8) Resource

Stores a reference to an external resource (like a database connection or file handle).

Example:

$file = fopen("example.txt", "r"); // Resource
echo gettype($file); 

Output:

Resource

 

 


15+ interview questions on PHP data types with answers and examples

 

Question 1. What are the data types in PHP?

Answer: PHP supports 8 basic data types, divided into three categories:

  • Scalar Types: Integer, Float, String, Boolean.
  • Compound Types: Array, Object.
  • Special Types: NULL, Resource.

 

Question 2. What is an integer in PHP?

Answer: An integer is a scalar data type that stores whole numbers (positive or negative).

Example:

$age = 25; // Integer
echo $age; 

Output:

25

 

Question 3. What is a float in PHP?

Answer: A float (or double) is a scalar data type that stores decimal numbers.

Example:

$price = 19.99; // Float
echo $price; 

Output:

19.99

 

Question 4. What is a string in PHP?

Answer: A string is a scalar data type that stores text (a sequence of characters).

Example:

$name = "John"; // String
echo $name; 

Output:

John

 

Question 5. What is a boolean in PHP?

Answer: A boolean is a scalar data type that stores true or false.

Example:

$isStudent = true; // Boolean
echo $isStudent; 

Output:

1 (true is displayed as 1)

 

Question 6. What is an array in PHP?

Answer: An array is a compound data type that stores multiple values in a single variable.

Example:

$fruits = ["apple", "banana", "cherry"]; // Array
print_r($fruits);

Output:

Array ( [0] => apple [1] => banana [2] => cherry )

 

Question 7. What is an object in PHP?

Answer: An object is a compound data type that stores an instance of a class (used in object-oriented programming).

Example:

class Car {
    public $color = "red";
}
$myCar = new Car(); // Object
echo $myCar->color; 

Output:

red

 

Question 8. What is NULL in PHP?

Answer: NULL is a special data type that represents a variable with no value.

Example:

$address = null; // NULL
echo $address; 

Output:

(nothing, because null means no value)

 

Question 9. What is a resource in PHP?

Answer: A resource is a special data type that stores a reference to an external resource (like a database connection or file handle).

Example:

$file = fopen("example.txt", "r"); // Resource
echo gettype($file); 

Output:

resource

 

Question 10. How do you check the data type of a variable in PHP?

Answer: Use the gettype() function to check the data type of a variable.

Example:

$age = 25;
echo gettype($age); 

Output:

integer

 

Question 11. How do you convert a string to an integer in PHP?

Answer: Use the (int) or intval() function to convert a string to an integer.

Example:

$number = "10";
$intNumber = (int)$number; // Convert to integer
echo $intNumber; 

Output:

10

 

Question 12. How do you convert an integer to a string in PHP?

Answer: Use the (string) or strval() function to convert an integer to a string.

Example:

$age = 25;
$strAge = (string)$age; // Convert to string
echo $strAge; 

Output:

25

 

Question 13. What is type juggling in PHP?

Answer: Type juggling is the automatic conversion of one data type to another by PHP, depending on the context.

Example:

$number = "10"; // String
$sum = $number + 5; // PHP converts "10" to integer
echo $sum; 

Output:

15

 

Question 14. What is type casting in PHP?

Answer: Type casting is the manual conversion of one data type to another using syntax like (int), (string), etc.

Example:

$price = "19.99"; // String
$floatPrice = (float)$price; // Convert to float
echo $floatPrice; 

Output:

19.99

 

Leave a comment

You must be logged in to post a comment.

0 Comments