PHP
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
true
orfalse
to a variable. If something valid then =true
otherwise =false
. -
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
NULL
ornull
. eg:$avatar_url = null
-
Object - Object are actually a user defined data types, means - we can create a custom data type which will use several
key:value
pair data. -
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.
¶Integer
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;
¶String
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";
¶Boolean
Store only true
or false
to a variable. If something valid then = true
otherwise = false
.
¶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 true
or false
.
$is_user_logged_in = true;
$is_user_logged_in = false;
¶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 NULL
or null
.
¶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
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.
¶Array
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
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
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development