PHP Data Types and Keywords

By Vijay

By Vijay

Image
I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 4, 2025
Edited by Kamila

Edited by Kamila

Image
Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

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

PHP Data Types and Keywords

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,

  1. Scalar data types (also known as predefined data types)
  2. Compound data types (also known as user-defined data types)
  3. Special data types

In a nutshell,

Data Type CategoryData TypeDescription
1ScalarStringA series of characters that stays inside a pair of single or double quotes.
2ScalarIntegerA whole number, without a decimal point.
3ScalarFloating-point numberA number with a decimal point.
4ScalarBooleanEither true or false.
5CompoundArrayA named and indexed collection of values.
6CompoundObjectAn instance of a class.
7SpecialNULLHas only one value called NULL.
8SpecialResourceIt 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:

  1. Strings
  2. Integers
  3. Floating-point numbers (floats or doubles)
  4. 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 TypeTrue ValueFalse valueDescription
1StringAll strings – except empty strings and zero stringsEmpty strings and zero strings
Empty strings: (), “” and ‘’
Zero strings: (), “0” and ‘0’
False if the string is empty (no characters) or “0”, otherwise true.
2IntegersAny non-zero value0False if exactly equals zero, otherwise true.
3Floating-point numbersAny non-zero value0False if exactly equals zero, otherwise true.
4ArraysContains at least one elementContains no elementsFalse if it does not contain any value, otherwise true.
5ObjectsAlwaysNeverValid objects are true
6NULLNeverAlwaysThe value of data type NULL is always false.
7ResourcesAlwaysNeverValid 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:

  1. Array
  2. 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.

  1. NULL
  2. 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:

  1. __halt_compiler() – It halts the compiler execution.
  2. abstract – It declares a class as abstract.
  3. and – It is a logical operator.
  4. array() – It is used to create an array.
  5. as – It is used in Foreach loops.
  6. break – It ends the execution of loops or switch statements.
  7. callable – It is used in callback functions.
  8. case – It is used in switch statements.
  9. catch – It is used in try-catch statements.
  10. class – It declares a class.
  11. clone – It creates a copy of an object.
  12. const – It defines a class constant.
  13. continue – It is used to go to the next iteration of a loop.
  14. declare – It sets execution directives for a code block.
  15. default – It is used in switch statements.
  16. die() – It prints a message and exits from the current script.
  17. do – It is used to create a do-while loop.
  18. echo – It is used to output one or more strings.
  19. else – It is used in if-else statements.
  20. elseif – It is used in if-elseif-else statements.
  21. empty() – It checks whether a variable is empty.
  22. enddeclare – It is used to end a declare block.
  23. endfor – It is used to end a for loop.
  24. endforeach – It is used to end a foreach loop.
  25. endif – It is used to end an if or elseif statement.
  26. endswitch – It is used to end a switch statement.
  27. endwhile – It is used to end a while loop.
  28. eval() – It evaluates a string as PHP code.
  29. exit() – It prints a message and exit from the current script.
  30. extends – It extends a class or interface.
  31. final – It declares a class, a property, or a method as final.
  32. finally – It is used in try-catch statements.
  33. fn – It declares an arrow function.
  34. for – It is used to create a for loop.
  35. foreach – It is used to create a foreach loop.
  36. function – It is used to create a function.
  37. global – It imports variables from the global scope.
  38. goto – It is used to go to another section of code.
  39. if – It creates a conditional statement.
  40. implements – It implements an interface.
  41. include – It is used to embed code from another file.
  42. include_once – It is also used to embed code from another file.
  43. instanceof – It checks if a PHP variable is an instantiated object of a particular class.
  44. insteadof – It resolves naming conflicts between Traits used in the same class.
  45. interface – It declares an interface.
  46. isset() – It determines if a variable exists and is not null.
  47. list() – It assigns variables as if it was an array.
  48. namespace – It declares a namespace.
  49. new – It is used to create an object.
  50. or – It is a logical operator.
  51. print – It is used to output a string.
  52. private – It declares the visibility of a property, a method, or a constant as private.
  53. protected – It declares the visibility of a property, a method, or a constant as protected.
  54. public – It declares the visibility of a property, a method, or a constant as public.
  55. require – It is used to embed code from another file.
  56. require_once – It is also used to embed code from another file.
  57. return – It ends a function and returns a value.
  58. static – It declares a property or a method as static.
  59. switch – It is used to create a switch statement.
  60. throw – It throws an exception.
  61. trait – It declares a trait (a way to reuse code).
  62. try – It is used in try-catch statements.
  63. unset() – It deletes a variable.
  64. use – It defines a namespace section.
  65. var – It declares a variable.
  66. while – It creates a while loop.
  67. xor – It is a logical operator.
  68. yield – It is used to create a generator function.
  69. 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.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



Leave a Comment