PHP Data Types Explained: Scalar, Compound & Special
PHP splits its data types into scalar, compound, and special types. Walk through each one with simple examples you can try right away.

Contents

PHP data types are SCALAR, COMPOUND, and SPECIAL TYPES. Now let’s examine them one by one;
SCALAR TYPES:
We can define Boolean, Integer, Float, and String types as scalar.
- Boolean: A Boolean value takes the values TRUE or FALSE, meaning it represents whether an output or input is true or false.
- Integer: All whole numbers are defined as integers, meaning all numbers like ….-5,-4,-3,-2,-1,0,1,2,3…. are of type int.
- Float: All fractional numbers are called the float number type, such as 1.2, 3.4, 7.99 … .
- String: It is defined as a sequence of characters enclosed in single or double quotes.
COMPOUND TYPES:
Array and Object types are called compound types. We can examine them as follows.
- Array: You can think of arrays as an ordered structure made up of values and keys. You can imagine arrays as a list consisting of the names of products in your cart, or the names of products in your cart and their corresponding prices. We can explain this even better with the example below.
<?php
$sepetim= [ 'elma', 'nane', 'salatalık' ];
In the cart array above, we can list the product names like this, and there will be 3 products in the array. A question like this may come to mind: "Yes, I listed these products this way, but how can I store their prices in this array?" Let’s look at the example below for that.
$sepetim= [
'elma' => 2,
'nane' => 3,
'salatalık' => 10
];
I’ll provide more details on these in our Array lessons.
- Object: If we look around us, we actually know what objects are. We can consider concrete objects around us such as a computer, phone, TV, vase, glass, and abstract objects such as a bank account and bank account transactions.
These objects have two basic characteristics.
-
- State
- Behavior
If we continue with the bank account example above, the bank account number and your balance are called state. However, deposits and withdrawals in your account transactions are defined as behavior.
Since there is a lot to explain, we will continue with the Objects topic in our OOP lessons.
SPECIAL TYPES:
Special types are divided into 2 categories: NULL and RESOURCE.
- NULL: The null type represents a variable that does not contain a value.
- RESOURCE: Among special types, resource refers to all sources from which data is obtained. Resources can be a database or a similar file. In other words, it is where you get the data from.
If you understand PHP data types, you can also review other examples on the official website.
Share it if it helped


