An integer is a rational number without a decimal point. It can be a negative or positive number. Integers can specify in: Decimal, Hexadecimal or Octal format.
An example of an integer would be: A = {…, -2, -1, 0, 1, 2 …}.
Examples of simple integer and binary data types representation
<?php
$a = 1234; // a decimal number
$a = -123; // a negative decimal number
$a = 0123; // an octal number (equivalent to 83 decimal)
$a = 0x1A; // a hexadecimal number (equivalent to 83 decimal)
$a = 0b11111111; // a binary number (equivalent to 255 decimal)
?>
Converting to integers in PHP
To convert an another number type into an integer, use (int)
statement, fo instance:
Example of a PHP conversion to an integer
The conversion of floating point numbers to integer will only consider the number before the decimal point. If the script encounters a number beyond boundaries of its type, it will be considered as float. Integer will be called overflow if it crosses the bound of the integer type.
Example of a boundary crossing during conversion
<?php
$million = 1000000;
$large_number = 50000 * $million;
var_dump($large_number); // returns float(50000000000)
?>
Comments
No comments have been made yet.
Please login to leave a comment. Login now