PHP Data Types

Variables can store different data types. In PHP, PHP chooses the appropriate data type for the variable automatically.

Normally, we need to store different types of data in variables to do different tasks.

There are several data types in PHP.

var_dump() Function

var_dump() function dumps information about a variable. The importance of this function is, it outputs the data type of the variable with its value. It is also a output function like echo()

<?php
\$x = 12;
var_dump(\$x);

PHP Booleans

A Boolean is a variable that can have one of two possible values, true or false

Boolean is the simplest data type in PHP. It holds either true or false.

To specify a boolean, you can use constants true and false (Both are case-insensitive).

PHP Booleans Example

<?php
\$a = true; // assign true to \$a
\$b = false; // assign false to \$b

var_dump(\$a);
var_dump(\$b);

PHP Integers

An integer is a number which is not a fraction; a whole number (..., -2, -1, 0, 1, 2, 3, ...)
An integer can either be a negative or a positive number

Four ways to specify an integer in following notations:

• Specifying Integers in Decimal - base 10
• Specifying Integers in Hexadecimal - base 16
• Specifying Integers in Octal - base 8
• Specifying Integers in Binary - base 2

This table shows how to specify an integer (128) in different notations.

NotationPreceded withExample
Decimal128
Octal00200
Binary0b0b10000000

PHP Integers Example

<?php
// 128 in different notations
\$a = 128; 			// decimal number
var_dump(\$a);

\$a = -128; 			// a negative number
var_dump(\$a);

\$a = 0x80; 			// hexadecimal number
var_dump(\$a);

\$a = 0200; 			// octal number
var_dump(\$a);

\$a = 0b10000000;	// binary number
var_dump(\$a);

As a beginner, you will not frequently use hexadecimal, octal and binary notations. So, it is not compulsory to remember them now.

PHP Floats

A float is a number that can contain a fractional part (2.56, 1.24, 7E-10)

Floats are also known as "Double", "Real" or "Floating-Point Numbers".

PHP Floats Example

<?php
\$a = 2.56;
var_dump(\$a);

\$a = 2.56e3; // 2.56 multiplied by 3rd power of 10
var_dump(\$a);

\$a = 2.56e-5; // 2.56 multiplied by -5th power of 10
var_dump(\$a);

PHP Strings

A string is any finite sequence of characters (letters, numerals, symbols, punctuation marks, etc.)

There are four ways to specify a string.

• Single Quoted
• Double Quoted
• Heredoc Syntax
• Nowdoc Syntax

We learned all of them in the strings chapter.

PHP Strings Example

<?php
\$str = 'This is single quoted';
\$str = "This is double quoted";

\$str = <<<EOD
This is a heredoc example
This can be multiline
EOD;

\$str = <<<'EOD'
This is a nowdoc example
This can be multiline
EOD;

var_dump(\$str);

PHP Arrays

An array is a series of values

There are two ways to declare an array.

• An array can be declared using the array() function.
• An array can be declared wrapping with [ and ].

Elements of the arrays should be separated with a comma. And, elements can be any type you learn in this chapter.

PHP Arrays Example

<?php
\$array = array('Apple', 'Banana', 'Orange', 'Mango');
var_dump(\$array);

\$array = ['Apple', 'Banana', 'Orange', 'Mango'];
var_dump(\$array);

Note: Array is a huge part in PHP. We will learn more about arrays in the Arrays Chapter.

PHP Objects

An object is a particular instance of a class where it can be a combination of variables, functions, and data structures.

Note: We will learn about objects in the Object Oriented Programming tutorial.

PHP NULL

Null represents a variable with no value
• Null means no value.
• Null is not 0, false or any other value, it is null.
• Null is case-insensitive. All Null, null and NULL are equal.

PHP Null Example

<?php
\$var = null;
var_dump(\$var);

// unsetting variables with null
\$text = 'Hello World';
\$text = null; // now \$text does not hold any value
var_dump(\$text);

Visit PHP Help group to get some help from experts. Supun Kavinda
I'm the Founder of Hyvor, Web Developer, Physics Lover, Flutist, and a Table Tennis Player.