• Tutorial List

PHP Data Types

Last updated May 15, 2020
PHP Data Types

What are the PHP data types?

When you assign a value in a variable, the value of that variable can be of different types, such as number, text, etc. This is called PHP Data Types.


How many data types are supported in PHP?

PHP supports the following data types –

  • String
  • Integer
  • Float (floating-point numbers)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

PHP Data Types with a short description

String:

A string is a sequence of characters, that are inside a single or double quote.

<?php
	$x = "This is a String";
	$y = 'Another String';
?>

Integer

An integer is a non-decimal number.

<?php
 	$x = 458; 
 ?>

Float

A Float is a number with a decimal point.

<?php
 	$x = 10.5;
 ?>

Boolean

True or False (It is used in conditional testing).

<?php
 	$x = true;
	$y = false;
 ?>

Array

An array is a Collection of multiple values.

<?php
 	$x = array("value1","value2"); 
 ?>

Object

An object is an instance of a class.

<?php 
class Elephant{
    public function legs(){
      	return "4 Legs";
    }
    public function tail(){
      	return "1 Tail";
    }
}

// OBJECT
$elephant = new Elephant();

// output is - 4 Legs
echo $elephant->legs();
?>

NULL

If there is a NULL data type in a variable, then that means the variable does not have any value.

<?php
	$x = NULL;
?>

📝 If a variable is declared without any value, then the variable is automatically assigned the NULL value.


Resource

The resource type is not the actual data type. It is a storage of references to functions or to external PHP resources. Such as Database Connection.