Showing posts with label Data Types. Show all posts
Showing posts with label Data Types. Show all posts

Tuesday, February 5, 2008

Floating data type in php

Floating point numbers

Floating point numbers (AKA "floats", "doubles" or "real numbers") can be specified using any of the following syntaxes:

$a = 1.234;

$b = 1.2e3;

$c = 7E-10;

?>

Formally:

LNUM [0-9]+

DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)

EXPONENT_DNUM ( ({LNUM} | {DNUM}) [eE][+-]? {LNUM})

The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (that's 64 bit IEEE format).

Floating point precision

It is quite usual that simple decimal fractions like 0.1 or 0.7 cannot be converted into their internal binary counterparts without a little loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8 as the result of the internal representation really being something like 7.9999999999....

This is related to the fact that it is impossible to exactly express some fractions in decimal notation with a finite number of digits. For instance, 1/3 in decimal form becomes 0.3333333. . ..

So never trust floating number results to the last digit and never compare floating point numbers for equality. If you really need higher precision, you should use the arbitrary precision math functions or gmp functions instead.

Integers data type in php

An integer is a number of the set Z = {..., -2, -1, 0, 1, 2, ...}.

Syntax

Integers can be specified in decimal (10-based), hexadecimal (16-based) or octal (8-based) notation, optionally preceded by a sign (- or +).

If you use the octal notation, you must precede the number with a 0 (zero), to use hexadecimal notation precede the number with 0x.

Example: Integer literals

$a = 1234; // decimal number

$a = -123; // a negative number

$a = 0123; // octal number (equivalent to 83 decimal)

$a = 0x1A; // hexadecimal number (equivalent to 26 decimal)

?>

Formally the possible structure for integer literals is:

decimal : [1-9][0-9]*

| 0

hexadecimal : 0[xX][0-9a-fA-F]+

octal : 0[0-7]+

integer : [+-]?decimal

| [+-]?hexadecimal

| [+-]?octal

The size of an integer is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). PHP does not support unsigned integers. Integer size can be determined from PHP_INT_SIZE, maximum value from PHP_INT_MAX since PHP 4.4.0 and PHP 5.0.5.

Warning

If an invalid digit is passed to octal integer (i.e. 8 or 9), the rest of the number is ignored.

Example: Octal weirdness

var_dump(01090); // 010 octal = 8 decimal

?>

Integer overflow

If you specify a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, if you perform an operation that results in a number beyond the bounds of the integer type, a float will be returned instead.

$large_number = 2147483647;

var_dump($large_number);

// output: int(2147483647)

$large_number = 2147483648;

var_dump($large_number);

// output: float(2147483648)

// it's true also for hexadecimal specified integers between 2^31 and 2^32-1:

var_dump( 0xffffffff );

// output: float(4294967295)

// this doesn't go for hexadecimal specified integers above 2^32-1:

var_dump( 0x100000000 );

// output: int(2147483647)

$million = 1000000;

$large_number = 50000 * $million;

var_dump($large_number);

// output: float(50000000000)

?>

Warning

Unfortunately, there was a bug in PHP so that this does not always work correctly when there are negative numbers involved. For example: when you do -50000 * $million, the result will be -429496728. However, when both operands are positive there is no problem.

This is solved in PHP 4.1.0.

There is no integer division operator in PHP. 1/2 yields the float 0.5. You can cast the value to an integer to always round it downwards, or you can use the round() function.

var_dump(25/7); // float(3.5714285714286)

var_dump((int) (25/7)); // int(3)

var_dump(round(25/7)); // float(4)

?>

Converting to integer

To explicitly convert a value to integer, use either the (int) or the (integer) cast. However, in most cases you do not need to use the cast, since a value will be automatically converted if an operator, function or control structure requires an integer argument. You can also convert a value to integer with the function intval().

From booleans

FALSE will yield 0 (zero), and TRUE will yield 1 (one).

From floating point numbers

When converting from float to integer, the number will be rounded towards zero.

If the float is beyond the boundaries of integer (usually +/- 2.15e+9 = 2^31), the result is undefined, since the float hasn't got enough precision to give an exact integer result. No warning, not even a notice will be issued in this case!

Warning

Never cast an unknown fraction to integer, as this can sometimes lead to unexpected results.

echo (int) ( (0.1+0.7) * 10 ); // echoes 7!

?>

Booleans data type in php

This is the easiest type. A boolean expresses a truth value. It can be either TRUE or FALSE.

Syntax

To specify a boolean literal, use either the keyword TRUE or FALSE. Both are case-insensitive.

$foo = True; // assign the value TRUE to $foo

?>

Usually you use some kind of operator which returns a boolean value, and then pass it on to a control structure.

// == is an operator which test

// equality and returns a boolean

if ($action == "show_version") {

echo "The version is 1.23";

}

// this is not necessary...

if ($show_separators == TRUE) {

echo "


\n";

}

// ...because you can simply type

if ($show_separators) {

echo "


\n";

}

?>

Converting to boolean

To explicitly convert a value to boolean, use either the (bool) or the (boolean) cast. However, in most cases you do not need to use the cast, since a value will be automatically converted if an operator, function or control structure requires a boolean argument.

When converting to boolean, the following values are considered FALSE:

the boolean FALSE itself

the integer 0 (zero)

the float 0.0 (zero)

the empty string, and the string "0"

an array with zero elements

an object with zero member variables (PHP 4 only)

the special type NULL (including unset variables)

SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource).

Warning

-1 is considered TRUE, like any other non-zero (whether negative or positive) number!

var_dump((bool) ""); // bool(false)

var_dump((bool) 1); // bool(true)

var_dump((bool) -2); // bool(true)

var_dump((bool) "foo"); // bool(true)

var_dump((bool) 2.3e5); // bool(true)

var_dump((bool) array(12)); // bool(true)

var_dump((bool) array()); // bool(false)

var_dump((bool) "false"); // bool(true)

?>

Data Types in PHP

PHP supports eight primitive types.

Four scalar types:

boolean

integer

float (floating-point number, aka 'double')

string

Two compound types:

array

object

And finally two special types:

resource

NULL

This page also introduces some pseudo-types for readability reasons:

mixed

number

callback

And the pseudo-variable $....

You may also find some references to the type "double". Consider double the same as float, the two names exist only for historic reasons.

The type of a variable is usually not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used.

Note: If you want to check out the type and value of a certain expression, use var_dump().

If you simply want a human-readable representation of the type for debugging, use gettype(). To check for a certain type, do not use gettype(), but use the is_type functions. Some examples:

$a_bool = TRUE; // a boolean

$a_str = "foo"; // a string

$a_str2 = 'foo'; // a string

$an_int = 12; // an integer

echo gettype($a_bool); // prints out: boolean

echo gettype($a_str); // prints out: string

// If this is an integer, increment it by four

if (is_int($an_int)) {

$an_int += 4;

}

// If $bool is a string, print it out

// (does not print out anything)

if (is_string($a_bool)) {

echo "String: $a_bool";

}

?>

If you would like to force a variable to be converted to a certain type, you may either cast the variable or use the settype() function on it.