PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

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.

PHP var_dump() Example

$x = 12;

Run Example ››

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

$a = true; // assign true to $a
$b = false; // assign false to $b


Run Example ››

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:

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

Notation Preceded with Example
Decimal 128
Hexadecimal 0x 0x80
Octal 0 0200
Binary 0b 0b10000000

PHP Integers Example

// 128 in different notations
$a = 128; 			// decimal number

$a = -128; 			// a negative number

$a = 0x80; 			// hexadecimal number

$a = 0200; 			// octal number

$a = 0b10000000;	// binary number

Run Example ››

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

$a = 2.56;

$a = 2.56e3; // 2.56 multiplied by 3rd power of 10

$a = 2.56e-5; // 2.56 multiplied by -5th power of 10 

Run Example ››

PHP Strings

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

There are four ways to specify a string.

Note: It's really important to understand the difference between single and double quotes. In case you missed that part, visit Single vs. Double Quotes part in PHP Output tutorial.

PHP Strings Example

$str = 'This is single quoted';
$str = "This is double quoted";

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

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


Run Example ››

PHP Arrays

An array is a series of values

There are two ways to declare an array.

PHP Arrays Example

$array = array('Apple', 'Banana', 'Orange', 'Mango');

$array = ['Apple', 'Banana', 'Orange', 'Mango'];

Run Example ››

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

PHP Objects

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

In PHP, Objects are used in OOP (Object-Oriented Programming).

Note: We will discuss OOP deeply later in this tutorial.


Null represents a variable with no value

PHP Null Example

$var = null;

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

Run Example ››