·  PHP – Syntax – Variables – Integer

 

- Info:

  - PHP has only one type of variable, called scalar.

  - This tutorial shows possible ways of loading scalar with integer.

 

- Create:

  $int 123;              //123  Decimal number.

  $int = -123;              //-123 Negative decimal number.

  $int = 0173;              //123  Octal number      , equivalent to 123 decimal.

  $int = 0x7B;              //123  Hexadecimal number, equivalent to 123 decimal.

  $int = bindec('1111011'); //123  Binary number, equivalent to 123 decimal.

  $int = 0b1111011;         //123  Binary number, equivalent to 123 decimal. Fro PHP 5.4

 

- Convert:

  - Following function converts number, given as variable or string, from one base to another:

    $result = base_convert('1111011'2, 10); //"123" Converts binary      string into decimal string.

    $result = base_convert('173'    ,  8, 10); //"123" Converts octal       string into decimal string.

    $result = base_convert('7B'     , 16, 10); //"123" Converts hexadecimal string into decimal string.

 

- Display:

  - Basic functions for displaying number:

    echo  ($int);

    print ($int);

    printf($int);

  - Display number in different base:

    echo(base_convert($int10, 2)."\n"); //1111011

    echo(base_convert($int10, 8)."\n"); //173

    echo(base_convert($int10,16)."\n"); //7b