Data types in PHP indicates the kinds of data supported in PHP. PHP has total eight kinds of data types. All the PHP data types different from each other. Data types can be used for creating functions, defining variables, printing out etc. But primarily any type of data is stored in a variable.
PHP is a loosely typed language. It means you don’t have to declare the data type when you define a variable with a specific data type. For example,
$number = 35; here
35 is an integer value. That’s mean, by using the integer data type the
$number variable is declared. But you don’t have to mention the data type. PHP understands by its own.
In this chapter, we will learn the total eight kinds of data types in details.
- String – Basically contain texts and characters covered by single or double qoutation.
- Boolean (Logic) – checks a condition for true or false.
- Integer – Indicates all the number including negative and positive.
- Float (double) – Its also indicate numbers but with a fractional part.
- Object – a compound data types that contain functions in a user-defined class.
- Array – is also a compound data type that can store multiple values in a single variable.
- NULL – is an only data type that uses one keyword and stores the empty string in a variable.
- Resources – is not actually a data type. It is a built-in external command, by using the external commands you can do the certain operation. Such as, connecting a database.
PHP Data Types Explained
1.String: We have already seen the uses of string in PHP programming. Now it is time to know where we used those strings. A string in PHP programming is sets of character that represent a meaning by word or sentence basically covered with a single quotation (
'string') or double quotation (
Strings are basically used for printing out or displaying out the string text directly to the screen. That’s why string always has a meaning that represents the user to indicate about some task has been completed or running followed by the programming functionality.
Let’s try to understand about strings with an interesting & practical example, you know Facebook is written in PHP programming language. For example, when you delete a post or picture from your facebook profile. The deleting functionality was written by the PHP function simply delete the post or picture. After deleting a notification shown to your screen “Your post has been deleted” or “Your profile picture has been deleted” here you see the notification because of uses of string in the backend. Somewhere the message is written in a string in the deleting function file of facebook backend server and finally echoing them out for displaying after deleting your post or photo.
Let’s see a coding example:
echo "Hello, world!"; echo 'Hello, world!';
Here you can see both single quotation or double quotation are behaving the same for a string. You can use either single or double quotation to represent a string. But there are some differences between the two methods. That is when you will use a variable name in a single quoted string, then the output will the name of the variable not it’s stored value. Means in a single quoted string, a variable is not executed. But in a double-quoted string, the variable is executed. It is actually the main difference between single and double-quoted string. Let’s see an Example:
$var = "Hello, world!"; echo 'Every programmer starts with $var'; echo "Every programmer starts with $var";
Every programmer starts with $var Every programmer starts with Hello, world!
2.Boolean (Logic): To check a condition either true or false every modern programming language including PHP uses boolean operation in the if-else condition. To test a condition using Boolean operation PHP uses two keywords, true for representing the condition true and false for representing the condition false.
In the numerical form, you can also use 1 and 0 for validating a condition. Where 1 is corresponding to TRUE and 0 is corresponding to FALSE. Let’s clear it by an example:
if(1 = TRUE) echo "This condition is true and it will print"; if(1 = FALSE) echo "This condition is false and won't be print";
3.Integer: Integer is the pure mathematical number that can be calculated. You can define a variable by taking an integer number. But always remember you must not use either single or double quotation mark around an integer number while defining a variable.
You can define an integer value by three different ways. They can be hexadecimal (16 based), octal (8 based) or decimal (10 based). By default, when you normally declare a variable with an integer value, the number act as the decimal. To declare an octal number you have to use a zero (0) before the number. To declare the hexadecimal number you have to use 0x before the number.
Because of PHP memory limit, you can define an integer number between
2^31 or equivalent value. Example:
<?php // decimal integer $decimal = 110; // octal integers $octal = 02; // hexadecimal base integers $hexadecimal = 0x45; ?>
4.Float (Double): In the same way float or double data type represents the number like an integer. But the only difference between integer and float number is the fraction. When a number contains some fractional part including negative and positive called float or double type data in PHP. Look at the example below:
<?php $float1 = 50.82; $float2 = 654.22; echo $float1 + $float2; ?>
5.Object: PHP objects is actually a data type that can store value and function within an user-defined class. As we didn’t learn functions and classes yet. So It is an advanced data type. We will discuss object data type in details in the further chapter.
6. Array: It is a compound data type. An array can contain multiple values in a single variable. And after declaring the variable, you can echo them separately or at once. You can also use them in the advanced function. Let’s see how can we define an array.
<?php $var = array( 10, 20 , 30); echo "First Element: $var\n"; echo "Second Element: $var\n"; echo "Third Element: $var\n"; ?>
Here you can see, while assigning data using an array in a variable here we have used the array keyword, and then we have used the first brace to define multiple values by using commas after each value. When you want to echo them out you have to use the order number after the variable name using a pair of the third brace.
Actually, it is the basic idea of the array in PHP. In the later chapters, we will see the uses and formula of the array in details.
7.NULL: It is a special data type that can contain only one value NULL. While declaring a variable with the NULL keyword it will be the Null data type variable. Remember, it is case sensitive. Means, always you have to use the uppercase letter (NULL). It is interesting if you print a NULL variable. You won’t get any output. See the example below:
<?php $var1 = NULL; echo $var1; ?>
But here the question arises If NULL data don’t give any output the why it’s necessary? In advanced and complex PHP programming sometimes the NULL data type is needed. So in the later chapter, we will learn more about it.
8.Resources: It is not actually a data type in PHP. But it is used to call a specific function or operation. For example, calling for database connection, network response etc is an external resource of PHP language.