PHP Data Types

PHP data types are used to hold different types of data or values. PHP supports 8 primitive data types that can be categorized further in 3 types:

  1. Scalar Types (predefined)
  2. Compound Types (user-defined)
  3. Special Types

PHP Data Types: Scalar Types

It holds only single value. There are 4 scalar data types in PHP.

  1. boolean
  2. integer
  3. float
  4. string

PHP Data Types: Compound Types

It can hold multiple values. There are 2 compound data types in PHP.

  1. array
  2. object

PHP Data Types: Special Types

There are 2 special data types in PHP.

  1. resource
  2. NULL

PHP Boolean

Boolean data types are used in conditional testing. Hold only two values, either TRUE(1) or FALSE(0). Successful events will return true and unsuccessful events return false. NULL type values are also treated as false in Boolean. Apart from NULL, 0 is also considered false in boolean. If a string is empty then it is also considered false in boolean data type.

Ex.1

Output:

This condition is TRUE

Note: Only show the TRUE(1) result if all conditional statement is FALSE(0) then not print any result.

PHP Integer

Integers hold only whole numbers including positive and negative numbers, i.e., numbers without fractional part or decimal point. They can be decimal (base 10), octal (base 8), or hexadecimal (base 16). The default base is decimal (base 10). The octal integers can be declared with leading 0 and the hexadecimal can be declared with leading 0x. The range of integers must lie between -2^31 to 2^31.

Ex.1

Output:

Decimal number: 34
Octal number: 163
HexaDecimal number: 69

PHP Float

A floating-point number is a number with a decimal point. Unlike integer, it can hold numbers with a fractional or decimal point, including a negative or positive sign.

Ex.1

Output:

Addition of floating numbers: 73.812

PHP String

Hold letters or any alphabets, even numbers are included. These are written within double quotes during declaration. The strings can also be written within single quotes, but they will be treated differently while printing variables. To clarify this look at the example below.

Ex.1

Output:

The name of the Geek is Dharmendra
The name of the geek is $name

PHP Array

Array is a compound data-type that can store multiple values of the same data type. Below is an example of an array of integers. It combines series of data that are related together.

Ex.1

Output:

array(3) { [0]=> string(13) “Royal Enfield” [1]=> string(6) “Yamaha” [2]=> string(3) “KTM” }
Array Element1: Royal Enfield
Array Element2: Yamaha
Array Element3: KTM

PHP object

Objects are the instances of user-defined classes that can store both values and functions. They must be explicitly declared.

Ex.1

 

Output:

Bike Model: Royal Enfield

PHP Resource

Resources in PHP are not an exact data type. These are basically used to store references to some function call or to external PHP resources. For example, consider a database call. This is an external resource. Resource variables hold special handles to files and database connections.
We will discuss resources in detail in further articles.

PHP Null

These are special types of variables that can hold only one value i.e., NULL. We follow the convention of writing it in capital form, but it’s case-sensitive. If a variable is created without a value or no value, it is automatically assigned a value of NULL. It is written in capital letters.

Note:

  • To check the type and value of an expression, use the var_dump() function which dumps information about a variable.
  • PHP allows the developer to cast the data type.
Share on:

Hello, I am Dharmendra Yadav and I am a Python Developer with experience in web development using Django, Flask, REST API, SQL, MySQL, HTML, CSS, JavaScript, WordPress, Oracle Cloud, AWS and Git. I also write technical articles where I explain web development and Software Engineering. Facebook , Linkedin