JavaScript Tutorial

Data Types

In any programming language, data type describes the kind of value that can be stored in a variable and also the type of operations that can be performed on it. PHP supports ten primitive data types which are described as under:

Scalar Data Types

These are the predefined data types which can hold only a single value.

1. Boolean: This can hold only two values i.e. TRUE or FALSE. It is most commonly used with conditional statements.

2. Integer: Integer is a non-decimal number. Rules for declaring integer data type are as : 

  • must have at least one digit.
  • must not contain any decimal point.
  • Integers can be specified in decimal, hexadecimal, octal or binary notation
  • It can range from -2,147,483,648 and 2,147,483,647.

3. Float: Float is a decimal number or a number in an exponential form.

4. String: String is a sequence of characters. It can be represented as a text in quotes. Quotes can be single or double.

Compund Data Types

These are the predefined data types which can hold only a single value.

Array: An array is a sequential collection of elements stored in a single variable.

2. Object: Objects have their own states and behaviours. An object is an instance of a class whereas a class is a blueprint which describes the state and behaviour that belongs to a particular object.

If you consider a human , state will be : name, age, height, weight etc.

behaviour will be : walking, laughing, sleeping etc. 

On creating a __construct() function, PHP will automatically call this function when you create an object of the related class. 

3. Callable: Callable keyword is used to pass a function argument as a reference to a function.

A callable can be any one of the following : 

  • an anonymous method.
  • a string having the name of the method.
  • an array describing an object method or a static class method.

4. Iterable: Iterable is a pseudo-type which was introduced in PHP 7.1. It accepts an array or an object which is further iterable through foreach statement. The iterable keyword can be used as an argument or as a return type of the function.

Special Data Types

Null and Resource are two special data types. 

Null: This data type has only one value i.e. NULL. If any variable is created without a value, it is automatically assigned with NULL value.

2. Resource: This data type refers to an external resource. Resource variable acts as a reference to an external source of data such as stream, file etc. The function is_resource() can be used to determine if a variable is a resource and get_resource_type() will return the type of resource it is. Various types of resources are handled in PHP and some selected ones are listed below:

RESOURCE TYPE NAME

CREATED BY 

DESTROYED BY 

DEFINITION

AddressInfo

socket_addrinfo_lookup()

None

AddressInfo (sockets extension)

bzip2

bzopen()

bzclose()

bzip2 file

curl

curl_copy_handle(), curl_init()

curl_close()

cURL handle

dbase

dbase_open()

dbase_close()

Link to Dbase database

ftp

ftp_connect(), ftp_ssl_connect()

ftp_close()

FTP stream

imap

imap_open()

imap_close()

Link to IMAP, POP3 server

mysql link

mysql_connect()

mysql_close()

Link to MySQL database

mysql result

mysql_db_query(), mysql_query()

mysql_free_result()

MySQL result

odbc link

odbc_connect()

odbc_close()

Link to ODBC database

Socket

socket_accept(),socket_create()

socket_close()

Socket (sockets extension)

stream

opendir()

closedir()

Dir handle

stream

fopen(), tmpfile()

fclose()

File handle

xml

xml_parser_create(), xml_parser_create_ns()

xml_parser_free()

XML parser

zlib

gzopen()

gzclose()

gz-compressed file

Go back to Previous Course