Quakenet/#php Tutorial

Note: If you opened this page from an external URL pay attention that all chapters are linked together. Be sure you also read all prior chapters of this tutorial, otherwise you will miss relevant content explained before.

Numbers

  1. The datatype numbers
  2. Integer numbers
  3. Float numbers
  4. Memory usage of integers and floats

1. The datatype numbers

If you want to calculate with variables (be more specific: with its content) you must use a data type which is used for numbers. One lazy way is to write a number in string and calculate with these strings.

<?php
echo "5"+"9";
?>

This will generated 14 as expected. However php didn't calculated with numbers but with strings. This doesn't work normally (or what do you expected for "hello" + "xyz"?), but the automatically typecasting take place here. The strings are got casted to real numbers first. In php there exists two types, the integer and float numbers.

2. Integer numbers

Integer numbers are any whole numbers, they don't have any decimal places. E.g 5, 31 and 199 are integer numbers, but also -1, -10 and -44. In php you write numbers as they are, the php interpreter reads the sequence of digits as a number.

<?php
$var 
5;
$var2 31;
$var3 = -10;
?>

Its also possible to write integer numbers in hexadecimal or octal notation, if you know how to calculate them. For hexadecimal notation you use the prefix 0x, for octal notation the prefix 0 (normal integer numbers starts with the highest digit or are zero anyway).

<?php
$var 
0xFF// hexadecimal (255), often use for protocols
$var2 0763// octal (499), often used for chmod
$var3 0// normal zero, it doesn't matter if hex-, oct- or decimal
?>

3. Float numbers

Float numbers are values with decimal places like 5.7, 12.25 or -4.07, but also 10.0, 3.1E-10 or 4.5e3.

<?php
$var 
5.7;
$var2 3.1E-10;
$var .1;
?>

Float numbers can be easily recognized by the decimal point ., so you dont use the comma (,) for the decimal places. As the dot . is also used for string concatenation you must be very carefully if you concat float numbers with strings. Sometimes you must use parentheses.

<?php
echo 'Percent value:'.77.3.'%';   // won't work
echo 'Percent value:'.(77.3).'%'// works
echo 'Percent value:'77.3 .'%'// works, these spaces "helps" php to parse this line correct
?>

4. Memory usage of integers and floats

There may be a question why to use integer and float numbers as float numbers can do more. Both types use (basically) the same memory usage but use a different usage of it. Integer values are saved very easily. Imagine you save an integer value into a 64 bit field. One bit is reserved for the sign (more or less, check Two's complement). The remaining 63 bits are for the significations. A bit with the value 1 means the integer number got this mentioned signification. The sum of all results in the value of the integer number. These significations starts from 2^0 (1) and goes up to 2^62 (4611686018427387904). As an example the number 50 got the significations 32, 16 and 2. On bit level these are the bits 1, 4 and 5. These are put into a 64 bit field.

Bits set for the integer value 50 (simplyfied) Img.:Bits set for the integer value 50 (simplyfied)

However, integer numbers are saved in the Two's complement.

Float numbers are saved in a different way. As we can save decimal places too we cannot use the same significations as for integer numbers. Float numbers are normally saved in the IEEE 754 format. For a 32 bit float number one bit is used for the sign, 8 bits for the exponent and 23 bits for the mantissa. The mantissa builds a number between around 0 and 1. On this value a base-2-exponentation is multiplied. This exponentation is build with the mentioned 8 bits. Depending on the choosen standard the mantissa is moved left (if the factor is big, like 2^50) or moved right (if the factor is small, like 2^-50). With the sign bit you get a complete number. So you cannot save any number with decimal places but only a specific number of digits of the number. But thats normally no problem as we use it all the time like giving the weight of something in 31.6 tons instead of 31.6346800235 tons.

Questions about the chapter

1. What kinds of datatypes exists for numbers?

There are integer numbers for whole numbers and float numbers for numbers with decimal places.

Back to Next to
Copyright © to the OPs of #php/QuakeNet Valid XHTML 1.0 Strict Valid CSS!