Search This Blog

4/22/2011

Integer Type

Do you really know the integer type of PHP? It represents a integer number. Well, but we are not here to know what everybody already know. Let's explore something more interesting.


Integer Representation

The integer type can be expressed by three basic ways:

  • Decimal base notation
  • Octal base notation
  • Hexadecimal base notation

All these notations accept negetive values when preceded by the symbol "-" (without quotes).

In decimal base, the number can be zero or starts with a non-zero digit and be carried, optionally, by any digit between 0-9. This is the more ofen way to represent a integer number.

$i = 0;  // Zero
$i = 1;  // One
$i = 13; // Thirteen

At octal base, the number should start with the zero digit and be carried by a sequence of one or more octal digits (between 0 and 7). This way is not so usual, but there are situations where it is more appropriete. For example, to pass the second parameter to the chmod or mkdir functions. The second parameter is a tree octal number where each digit represent the permission to the owner, group and other, respectively. This is well known between Linux users. Some examples:

$o = 00; // Zero
$o = 01; // One (decimal)
$o = 07; // Seven (decimal)
$o = 010; // Eight (decimal)
$o = 011; // Nine (decimal)
$o = 012; // Ten (decimal)

// Set permission to file /tmp/example
// Owner: read, write, execute
// Group: read, execute
// Other: read
chmod('/tmp/example', 0751);

// To understand how it works, you need to see the number in binary base:
// Octal / Binary
//     7 / 111
//     6 / 110
//     5 / 101
//     4 / 100
//     3 / 011
//     2 / 010
//     1 / 001
//     0 / 000
//
// First bit (at left)    = permission to read (1 = allow / 0 = denied)
// Second bit (at middle) = permission to write
// Third bit (at right)   = permission to execute

Finally, in hexadecimal base the number should start with "0x" prefix (without quotes) and be carried by a sequence of one or more hexadecimal digits (between 0-F), in case-insensitive mode. This notation is more usual than octal. It could be used, for example, to represent values between 0 and 255, that are common to represent RGB color components.

$h = 0x0;   // Zero (decimal)
$h = 0x1;   // One (decimal)
$h = 0x9;   // Nine (decimal)
$h = 0xA;   // Ten (decimal)
$h = 0xB;   // Eleven (decimal)
$h = 0xE;   // Fifteen (decimal)
$h = 0xF;   // Sixteen (decimal)
$h = 0x10;  // Seventeen (decimal)
$h = 0xFF;  // 255 (decimal)
$h = 0x1B3; // 435 (decimal)

Cast to Integer

One interesting feature of PHP is the easy way to cast values between diferent types. But do you know what happens with a value when it is casted to integer?

Float

When a float value is casted to integer, their decimal values are ignored.

$i = intval(0.1); // $i = 0;
$i = intval(0.5); // $i = 0;
$i = intval(0.9); // $i = 0;
$i = intval(4.1); // $i = 4;
$i = intval(4.9999999); // $i = 4;
$i = intval(-1.1); // $i = -1;
$i = intval(-1.9); // $i = -1;

Note that you can not say that the value is rounded to floor, becaouse in negative values it is rounded to ceil (a less negative value).

Bool

The "true" value is casted to integer 1 (one), while the "false" value is casted to 0 (zero).

$i = intval(true); // $i = 1;
$i = intval(false); // $i = 0;

Array

Empty arrays are casted to 0 (zero), while non-empty arrays are casted to 1 (one).

$i = intval(array(3, 5, 2)); // $i = 1;
$i = intval(array()); // $i = 0;

String

Casting from string is more complex. The string is evaluated from left to right. While the digit is numeric, it is considered part of the integer number. When a non-numeric digit is found or the end of string was found, the process is finished. The number is evaluated in decimal notation. The only exception is about the simbol "-", that represents a negative value.

$i = intval('0'); // 0
$i = intval('a'); // 0
$i = intval('1'); // 1
$i = intval('01'); // 1
$i = intval('012'); // 12
$i = intval('13a14'); // 13
$i = intval('a123'); // 0
$i = intval('12 13 14'); // 12
$i = intval('-12 -13 -14'); // -12

Object

Objects can not be casted to Integer.


Trivia

The function is_int or is_integer checks wheter a variable is the integer type. A string with value "1" is not considered integer, altough it keeps a numeric value. Since the function is_numeric checks wheter a value is considered numeric. It includes integer, fload or string that keeps numeric values.

$b = is_int(1); // true
$b = is_int(-4); // true
$b = is_int(2.3); // false
$b = is_int('1'); // false

$b = is_numeric(1); // true
$b = is_numeric(-4); // true
$b = is_numeric(2.3); // true
$b = is_numeric('1'); // true
$b = is_numeric('2.3'); // true
$b = is_numeric('0x1F'); // true (hexadecimal)
$b = is_numeric('4e2'); // true (4 * 102)
$b = is_numeric('12a'); // false
$b = is_numeric('a'); // false
$b = is_numeric('-1'); // true

No comments:

Post a Comment