# Data Types in php

PHP provides eight types of values, or data types. Four are scalar (single-value) types: integers, floating-point numbers, strings, and Booleans. Two are compound (collection) types: arrays and objects. The remaining two are special types: resource and NULL

## Integers

Integers are whole numbers, such as 1, 12, and 256. The range of acceptable values varies according to the details of your platform but typically extends from −2,147,483,648 to +2,147,483,647. Specifically, the range is equivalent to the range of the long data type of your C compiler. Unfortunately, the C standard doesn’t specify what range that long type should have, so on some systems you might see a different integer range.

Integer literals can be written in decimal, octal, or hexadecimal. Decimal values are represented by a sequence of digits, without leading zeros. The sequence may begin with a plus (+) or minus (−) sign. If there is no sign, positive is assumed. Examples of decimal integers include the following

1998
−641
+33

Octal numbers consist of a leading 0 and a sequence of digits from 0 to 7. Like decimal numbers, octal numbers can be prefixed with a plus or minus. Here are some example octal values and their equivalent decimal values

0755 // decimal 493
+010 // decimal 8

Hexadecimal values begin with 0x, followed by a sequence of digits (0–9) or letters (A–F). The letters can be upper- or lowercase but are usually written in capitals. Like decimal and octal values, you can include a sign in hexadecimal numbers:

0xFF // decimal 255
0x10 // decimal 16

Binary numbers begin with 0b, followed by a sequence of digits (0 and 1). Like other values, you can include a sign in binary numbers:

0b01100000 // decimal 1
0b00000010 // decimal 2
-0b10 // decimal −2

If you try to store a variable that is too large to be stored as an integer or is not a whole number, it will automatically be turned into a floating-point number.

Use the is_int() function (or its is_integer() alias) to test whether a value is an integer:

if (is_int(\$x)) {
// \$x is an integer
}

## Floating-Point Numbers

Floating-point numbers (often referred to as real numbers) represent numeric values with decimal digits. Like integers, their limits depend on your machine’s details. PHP floating-point numbers are equivalent to the range of the double data type of your C compiler. Usually, this allows numbers between 1.7E−308 and 1.7E+308 with 15 digits of accuracy. If you need more accuracy or a wider range of integer values, you can use the BC or GMP extensions

PHP recognizes floating-point numbers written in two different formats. There’s the one we all use every day:

3.14
0.017
-7.1

but PHP also recognizes numbers in scientific notation:

```0.314E1 // 0.314*10^1, or 3.14
17.0E-3 // 17.0*10^(-3), or 0.017

```

Floating-point values are only approximate representations of numbers. For example, on many systems 3.5 is actually represented as 3.4999999999.

This means you must take care to avoid writing code that assumes floating-point numbers are represented completely accurately, such as directly comparing two floating-point values using ==. The normal approach is to compare to several decimal places:

if (intval(\$a * 1000) == intval(\$b * 1000)) {
// numbers equal to three decimal places
}

Use the is_float() function (or its is_real() alias) to test whether a value is a floating-point number:

if (is_float(\$x)) {
// \$x is a floating-point number
}

## Strings

Because strings are so common in web applications, PHP includes core-level support for creating and manipulating strings. A string is a sequence of characters of arbitrary length. String literals are delimited by either single or double quotes:

‘big dog’
“fat hog”

Variables are expanded (interpolated) within double quotes, while within single quotes they are not:

\$name = “Guido”;
echo “Hi, \$name\n”;
echo ‘Hi, \$name’;

Hi, Guido
Hi, \$name

## Booleans

A Boolean value represents a “truth value”—it says whether something is true or not. Like most programming languages, PHP defines some values as true and others as false. Truth and falseness determine the outcome of conditional code such as:

if (\$alive) { … }

## Arrays

An array holds a group of values, which you can identify by position (a number, with zero being the first position) or some identifying name (a string), called an associative index:

\$person = “Edison”;
\$person = “Wankel”;
\$person = “Crapper”;
\$creator[‘Light bulb’] = “Edison”;
\$creator[‘Rotary Engine’] = “Wankel”;
\$creator[‘Toilet’] = “Crapper”;

## Objects

PHP also supports object-oriented programming (OOP). OOP promotes clean modular design, simplifies debugging and maintenance, and assists with code reuse

Classes are the building blocks of object-oriented design. A class is a definition of a structure that contains properties (variables) and methods (functions). Classes are defined with the class keyword:

class Person
{
public \$name = ”;
function name (\$newname = NULL)
{
if (!is_null(\$newname)) {
\$this->name = \$newname;
}
return \$this->name;
}
}

## Resources

Many modules provide several functions for dealing with the outside world. For example, every database extension has at least a function to connect to the database, a function to send a query to the database, and a function to close the connection to the database. Because you can have multiple database connections open at once, the connect function gives you something by which to identify that unique connection when you call the query and close functions: a resource (or a “handle”)

## Callbacks

Callbacks are functions or object methods used by some functions, such as call_user_func(). Callbacks can also be created by the create_function() method and through closures

\$callback = function myCallbackFunction()
{
echo “callback achieved”;
}
call_user_func(\$callback);
callback achieved

## NULL

There’s only one value of the NULL data type. That value is available through the case-insensitive keyword NULL. The NULL value represents a variable that has no value (similar to Perl’s undef or Python’s None):

\$aleph = “beta”;
\$aleph = null; // variable’s value is gone
\$aleph = Null; // same
\$aleph = NULL; // same