Explore all about the wide range of PHP Data Types and Keywords with simple examples. Understand the various kinds of PHP Data Types and learn how to use them:
In this tutorial, you will learn different data types (like PHP integers, PHP boolean, PHP null, and many more), keywords, and frequently asked questions.
Please note that we have used PHP version 7 in all examples.
Let’s begin!
=> Series of Simple PHP Tutorials
Table of Contents:
PHP Data Types and Keywords

Overview & Classification of PHP Data Types
We assign values to PHP variables. These values may have different types. Some data types may be simple data types like strings or numerical types or complex data types like arrays or objects.
Further Reading => Python Data Types: In-Depth Guide
Data types are the classification of data into different categories based on its attributes. There are three main categories of PHP data types. They are,
- Scalar data types (also known as predefined data types)
- Compound data types (also known as user-defined data types)
- Special data types
In a nutshell,
| Data Type Category | Data Type | Description | |
|---|---|---|---|
| 1 | Scalar | String | A series of characters that stays inside a pair of single or double quotes. |
| 2 | Scalar | Integer | A whole number, without a decimal point. |
| 3 | Scalar | Floating-point number | A number with a decimal point. |
| 4 | Scalar | Boolean | Either true or false. |
| 5 | Compound | Array | A named and indexed collection of values. |
| 6 | Compound | Object | An instance of a class. |
| 7 | Special | NULL | Has only one value called NULL. |
| 8 | Special | Resource | It holds a reference to an external resource. |
#1) Scalar (Pre-defined) Data Types
There are four types of scalar or pre-defined data types, as shown in the list below:
- Strings
- Integers
- Floating-point numbers (floats or doubles)
- Booleans
a) PHP String
A string is a series of characters that stay inside a pair of single or double quotes.
Example:
“hello”
“Hello World!”
‘Hello World!’
“1234”
“Year1992”
Note: We will talk about strings in detail in a separate tutorial.
b) PHP Integer
An integer represents a non-fractional numeric value, either positive or negative. It cannot contain a decimal point.
Example:
16
200
-1
-605
192400
Note: We will talk about integers in detail in a separate tutorial.
c) PHP Floating-Point Number
Floating-point numbers are also called floats, doubles, or real numbers. It is a number with a decimal point. Furthermore, a floating-point number can be in the exponential form as well.
Example:
1.0
24.578
0.33333
-200.1
2.79E+5
6.14E-23
Note: We will discuss floating-point numbers in detail in a separate tutorial.
d) PHP Boolean
PHP booleans are the simplest data type. Boolean values are either true or false. True represents any non-zero value, while false represents zero. They are commonly used in conditional testing.
Note: Both true and false are case-insensitive.
The following table shows the Boolean value equivalents.
| Data Type | True Value | False value | Description | |
|---|---|---|---|---|
| 1 | String | All strings – except empty strings and zero strings | Empty strings and zero strings Empty strings: (), “” and ‘’ Zero strings: (), “0” and ‘0’ | False if the string is empty (no characters) or “0”, otherwise true. |
| 2 | Integers | Any non-zero value | 0 | False if exactly equals zero, otherwise true. |
| 3 | Floating-point numbers | Any non-zero value | 0 | False if exactly equals zero, otherwise true. |
| 4 | Arrays | Contains at least one element | Contains no elements | False if it does not contain any value, otherwise true. |
| 5 | Objects | Always | Never | Valid objects are true |
| 6 | NULL | Never | Always | The value of data type NULL is always false. |
| 7 | Resources | Always | Never | Valid resources are true |
#2) Compound (User-Defined) Data Types
It can hold more than one value. There are two compound or user-defined data types, as shown in the list below:
- Array
- Object
a) PHP Array
An array is a named and indexed collection of values. It can store multiple values in a single variable.
Example:
$cars = array("Apple","Sony","Nokia");
Note: We will talk about arrays in detail in a separate tutorial.
b) PHP Object
An object is an instance of a class. It can have a collection of properties.
Example:
$animal = new Dog();
Note: We will talk about objects in detail in a separate tutorial.
#3) Special Data Types
There are two special data types as shown in the list below.
- NULL
- Resource
*
Note: The word null is case-insensitive. However, it is commonly written as NULL.
b) PHP Resource
The resource is not an actual data type. It is a special variable that refers to external resource data.
Example:
File resources
Database resources
What are Keywords
Keywords are reserved words. They cannot be used for function names, class names, or method names. All keywords are case-insensitive.
PHP Keywords List
There are about 70 keywords in PHP, as shown in the list below:
- __halt_compiler() – It halts the compiler execution.
- abstract – It declares a class as abstract.
- and – It is a logical operator.
- array() – It is used to create an array.
- as – It is used in Foreach loops.
- break – It ends the execution of loops or switch statements.
- callable – It is used in callback functions.
- case – It is used in switch statements.
- catch – It is used in try-catch statements.
- class – It declares a class.
- clone – It creates a copy of an object.
- const – It defines a class constant.
- continue – It is used to go to the next iteration of a loop.
- declare – It sets execution directives for a code block.
- default – It is used in switch statements.
- die() – It prints a message and exits from the current script.
- do – It is used to create a do-while loop.
- echo – It is used to output one or more strings.
- else – It is used in if-else statements.
- elseif – It is used in if-elseif-else statements.
- empty() – It checks whether a variable is empty.
- enddeclare – It is used to end a declare block.
- endfor – It is used to end a for loop.
- endforeach – It is used to end a foreach loop.
- endif – It is used to end an if or elseif statement.
- endswitch – It is used to end a switch statement.
- endwhile – It is used to end a while loop.
- eval() – It evaluates a string as PHP code.
- exit() – It prints a message and exit from the current script.
- extends – It extends a class or interface.
- final – It declares a class, a property, or a method as final.
- finally – It is used in try-catch statements.
- fn – It declares an arrow function.
- for – It is used to create a for loop.
- foreach – It is used to create a foreach loop.
- function – It is used to create a function.
- global – It imports variables from the global scope.
- goto – It is used to go to another section of code.
- if – It creates a conditional statement.
- implements – It implements an interface.
- include – It is used to embed code from another file.
- include_once – It is also used to embed code from another file.
- instanceof – It checks if a PHP variable is an instantiated object of a particular class.
- insteadof – It resolves naming conflicts between Traits used in the same class.
- interface – It declares an interface.
- isset() – It determines if a variable exists and is not null.
- list() – It assigns variables as if it was an array.
- namespace – It declares a namespace.
- new – It is used to create an object.
- or – It is a logical operator.
- print – It is used to output a string.
- private – It declares the visibility of a property, a method, or a constant as private.
- protected – It declares the visibility of a property, a method, or a constant as protected.
- public – It declares the visibility of a property, a method, or a constant as public.
- require – It is used to embed code from another file.
- require_once – It is also used to embed code from another file.
- return – It ends a function and returns a value.
- static – It declares a property or a method as static.
- switch – It is used to create a switch statement.
- throw – It throws an exception.
- trait – It declares a trait (a way to reuse code).
- try – It is used in try-catch statements.
- unset() – It deletes a variable.
- use – It defines a namespace section.
- var – It declares a variable.
- while – It creates a while loop.
- xor – It is a logical operator.
- yield – It is used to create a generator function.
- yield from – It is also used in generator functions.
Note: In PHP 8.0, a new keyword match is added.
Suggested Read => Static Keyword in JAVA
Frequently Asked Questions
In this section, we will discuss some of the FAQs related to this topic. These questions will help you prepare for your examinations and interviews confidently.
1. What do you mean by data type in PHP?
It is the classification of data into different categories based on its attributes. For example, alphanumeric characters within a pair of single or double quotes are classified as strings, whole numbers are classified as integers, and numbers with decimal points are classified as floating-point numbers.
2. How many data types are there in PHP?
There are 8 data types.
3. What are the basic data types?
They are strings, integers, floating-point numbers, booleans, arrays, objects, NULL, and resources.
4. What are PHP keywords?
They are a set of reserved words that cannot be used as function names, class names, or method names.
5. Is print a keyword in PHP?
Yes, it is a keyword.
6. What is the new keyword in PHP?
It is used to create an object from a class.
7. What are the four scalar data types in PHP?
They are strings, integers, floating-point numbers, and booleans.
8. What is float in PHP?
It is a number with a decimal point. Floats are also known as floating-point numbers.
9. What type of data is age?
Usually, age is considered an integer.
Conclusion
There are three main data types of PHP called scalar, compound, and special.
The scalar data types are strings, integers, floating-point numbers, and booleans, and compound data types are arrays and objects. PHP also has two special data types called NULL and resources.
PHP keywords are reserved words in PHP, and they are case-insensitive.






