Data Types in PHP - Variable Types in PHP
In our prevous Chapter, we’ve learned How to create and assign variable in PHP. We’ve seen that we can store some data in a variable and that data could be any type, like it could be a number, text or anything. This different types is called Data Type in the aspect of Programming language.
In this chapter, let’s learn the different data types of PHP.
¶Data types of PHP
There are total 8 data types in PHP.
Integer - Store number / numeric number to a variable. eg:
$age = 28,
$number1 = 50.
Float / Double - Store decimal or floating point number to a variable. eg:
$price = 200000.50,
$cgpa = 3.8.
Boolean - Store only
falseto a variable. If something valid then =
String - Store text like data to a variable. eg:
$name = "Jhon Doe",
$welcome_text = "Welcome to Learn PHP"
Null - Null is another special data type, if nothing found to store in a variable, then it’s used. Which stores only a single value
$avatar_url = null
Object - Object are actually a user defined data types, means - we can create a custom data type which will use several
Array - Store same types of multiple data to a variable, it could be named or indexed. eg:
$marks = [10, 20]
- Resource - Resource is another special types. It’s actually not a data type. It stores the reference value of some values.
Store number / numeric number to a variable.
¶Example of Integer
<?php $number1 = 50; $number2 = 60; $summation = $number1 + $number2; $age = 28; ?>
¶Float / Double
Store decimal or floating point number to a variable.
¶Example of Float / Double
Let’s assume a product price has some floating point value like 100.05. So, we’ve to use floating point value to this variable.
$product_price = 100.05;
Store text like data to a variable.
¶Example of String
$name = "Jhon Doe"; $welcome_text = "Welcome to Learn PHP"; $post_title = "Data Types in PHP - Variable Types in PHP"; $post_slug = "data-types-in-php-variable-types-in-php"; $post_description = "xxxxxx";
false to a variable. If something valid then =
true otherwise =
¶Example of Boolean
Suppose, in our system, we’ve an authentication system, where user could log in or not. So, there would be two case, either user is logged in or logged out. We can handle this using boolean data type
$is_user_logged_in = true; $is_user_logged_in = false;
Null is another special data type, if nothing found to store in a variable, then it’s used. Which stores only a single value
¶Example of Null
Suppose, we’ve users in our website. When they first time register to our site, they don’t upload any image or called avatar. So, at first user image could be empty. We can use null to store at first registration.
$user_image = null;
Object are actually a user defined data types, means - we can create a custom data type which will use several
key:value pair data.
¶Example of Object
Suppose, we want to make a custom user defined data types of some specific things, then we use object like system. Object is an advanced topic, since we’ll discuss it later.
Store same types of multiple data to a variable, it could be named or indexed.
¶Example of Array
Suppose, we want to store our list of marks in something, means - specific types of many data will be stored in a variable. Below example -
$marks is an array data type variable.
<?php $marks = [80, 95, 100, 75, 50]; ?>
Resource is another special types. It’s actually not a data type. It stores the reference value of some values.
¶Example of Resource
Resource is an advanced topic, since we’ll discuss it later.
¶Check Variable data type in PHP
So, we’ve learned the different data types. In our practical coding many times, we’ve to debug or test our code by checking the data type and data and PHP has a built in method or way to check that -
var_dump( $variable_name )
<?php $name = "Akash"; var_dump($name); // string(5) "Akash" $age = 28; var_dump($age); // int(28) $price = 100.50; var_dump($price); // float(100.5) ?>
That’s cool, right. We can easily check the data type for debug purpose.
Variables in PHP - How to Create, Rules and Examples
Echo, PHP Statement, Print, Concat and Comments in PHP
All Tutorials in this playlist
Design Pattern in PHP
Design Patterns - Clean Code
Laravel PHP Framework
Windows Operating system
- Bash Scripting
- C-sharp programming
- Code Editor
- Computer Engineering
- Design pattern
- Express JS
- Git Commands
- Node JS
- React Js
- Vue JS
- Windows terminal
- WordPress Plugin Development