gmp_init


gmp apg

CREATES a GMP number from an INTEGER or STRING.





The $num can be an INTEGER or a STRING.

If $num is a STRING, the representation can be DECIMAL, OCTAL or HEXADECIMAL.

The $base ∈ [ 2, 62 ].

If $base = 0 the leading characters determine the actual value, otherwise decimal is assumed.

GMP 4.2.0 or greater s the condition for PHP to use extended base.

It is not necessary to call this function if you want to use integer or string in place of GMP number in GMP functions.

Function arguments are automatically converted to GMP numbers, if such conversion is possible and needed, using the same rules as this function.



<?php

GMP gmp_init 
( int|string $num, int $base = 0 )


where,

$num = An INTEGER or a STRING

$base 
= The base to consider

?>
 

$num


The INTEGER or STRING as number.

The STRING representation can be decimal, hexadecimal or octal.



$base


The base to be considered.

The base may vary from 2 to 62.

If base is 0 (default value), the actual base is determined from the leading characters:

a) if the first two characters are 0x or 0X, hexadecimal is assumed.

b) if the first two characters are 0b or 0B, binary is assumed.

c) if the first character is 0, octal is assumed.

d) otherwise decimal is assumed.

For bases up to 36, case is ignored; UPPER-CASE and lower-case letters have the same value.

For bases 37 to 62, UPPER-CASE letter represent the usual 10 to 35 while lower-case letter represent 36 to 61.



  1 EXERCISE   

<table width="100%" border="1" cellspacing="5" cellpadding="5">
<tr><td colspan="3">gmp_ini</td></tr>
<tr><td>TYPE<td>VALUE</td><td>NOTE</td></tr>

<?php

$gmp01 
= [ 'DECIMAL' => 987654, 
            
'DECIMAL STRING' => '987654',
            
'OCTAL' => 0123456,
            
'OCTAL STRING' => '0123456', 
            
'HEXADECIMAL' => 0xFFAAFF,
            
'HEXADECIMAL STRING' => '0xFFAAFF' ];

foreach(
$gmp01 as $gm01 => $g01)
{
    if(
is_string($g01))
    {
    echo 
'<td>' . $gm01 . '</td><td>' . gmp_init($g01) . '</td><td>STRING</td></tr>';
    }
    else
    {
    echo 
'<td>' . $gm01 . '</td><td>' . gmp_init($g01) . '</td><td>NUMERIC</td></tr>';
    }
        
}

?> 
<td colspan="3">ed48</td></tr></table>


  2 EXERCISE   

<?php 

$bas02 
= 62;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   You can set the numeric base for random values:

   $bas02 = mt_rand(10, 62);

   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$t02a = 'INTEGER';
$g02a = 256;
// DECIMAL INTEGER

$t02b = 'STRING';
$g02b = '256';
// DECIMAL STRING

?>
<table width="100%" border="1" cellspacing="5" cellpadding="5">
<tr><td colspan="3">gmp_init</td></tr>
<tr><td>TYPE<td>EQUIVALENT VALUES</td><td>NOTE</td></tr>

<?php

    
echo '<td>' . $t02a . '</td><td>( ' . gmp_init($g02a, $bas02) . ' )<sub>' . $bas02. '</sub></td><td>NUMERIC</td></tr>';

    echo 
'<td>' . $t02b . '</td><td>( ' . gmp_init($g02b, $bas02) . ' )<sub>10</sub></td><td>STRING</td></tr>';

?> 
<td colspan="3">ed48</td></tr></table>


  3 EXERCISE   

<?php 

$bas03 
= 0;
// THE BASE MUST BE ZERO

$t03b = 'STRING';
$g03b = '0400';
// OCTAL STRING => '0400' = 256

?>
<table width="100%" border="1" cellspacing="5" cellpadding="5">
<tr><td colspan="3">gmp_init ( <?php echo $g03b . ', ' . $bas03; ?> )<br>[ OCTAL NOTATION AS STRING ]</td></tr>
<tr><td>TYPE<td>EQUIVALENT VALUES</td><td>NOTE</td></tr>

<?php

    
echo '<td>' . $t03b . '</td><td>( ' . gmp_init($g03b, $bas03) . ' )<sub>10</sub></td><td>STRING</td></tr>';

?> 
<td colspan="3">ed48</td></tr></table>


  4 EXERCISE   

<?php 

$bas03 
= 0;
// THE BASE MUST BE ZERO

$t03b = 'STRING';
$g03b = '0x100';
// HEXADECIMAL STRING => '0X100' = 256

?>
<table width="100%" border="1" cellspacing="5" cellpadding="5">
<tr><td colspan="3">gmp_init ( <?php echo $g03b . ', ' . $bas03; ?> )<br>[ HEXADECIMAL NOTATION AS STRING ]</td></tr>
<tr><td>TYPE<td>EQUIVALENT VALUES</td><td>NOTE</td></tr>

<?php

    
echo '<td>' . $t03b . '</td><td>( ' . gmp_init($g03b, $bas03) . ' )<sub>10</sub></td><td>STRING</td></tr>';

?> 
<td colspan="3">ed48</td></tr></table>


  5 EXERCISE   

<?php

$bas05 
= mt_rand( 2, 62 );

$oc05 = gmp_init(0400, $bas05); 

$hx05 = gmp_init(0x100, $bas05);

echo 
'Base = ' . $bas05 . '<br><br>';

echo 
'gmp_init ( 0400, ' . $bas05 . ' ) = ' . $oc05 . '<br>';

echo 
'gmp_init ( 0x100, ' . $bas05 . ' ) = ' . $hx05 . '<br>';

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    In this exercise, no matter what the base value,
    the result will always be the same.
    
    Try running it several times.

     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

?>


  6 EXERCISE   

<?php

$bas06 
= mt_rand( -2, -36 );

$oc06 = gmp_init(0400, $bas06); 

$hx06 = gmp_init(0x100, $bas06);

echo 
'Base = ' . $bas06 . '<br><br>';

echo 
'gmp_init ( 0400, ' . $bas06 . ' ) = ' . $oc06 . '<br>';

echo 
'gmp_init ( 0x100, ' . $bas06 . ' ) = ' . $hx06 . '<br>';


?>


  7 EXERCISE   

<?php

$nbr07a 
= [ 123456, '123456' ];

$nbr07b = [ 0361100, '0361100' ];

$nbr07c = [ 0x1e240, '0x1e240' ];

$nbr07d = [ 0X1E240, '0X1E240' ];

$bas07a = 0;

$bas07b = 8;

$bas07c = 16;

foreach(
$nbr07a as $k07a => $v07a)
{

echo 
gettype($v07a) . ' ';
echo 
gmp_init($v07a, $bas07a) . '<br>';
}

echo 
'<br>';

foreach(
$nbr07b as $k07b => $v07b)
{

echo 
gettype($v07b) . ' ';
echo 
gmp_init($v07b, $bas07b) . '<br>';
}

echo 
'<br>';

foreach(
$nbr07c as $k07c => $v07c)
{

echo 
gettype($v07c) . ' ';
echo 
gmp_init($v07c, $bas07c) . '<br>';
}

echo 
'<br>';

foreach(
$nbr07d as $k07d => $v07d)
{

echo 
gettype($v07d) . ' ';
echo 
gmp_init($v07d, $bas07c) . '<br>';
}

?>


  8 EXERCISE   

<?php

$nbr08a 
= "0x1e240";

$nbr08b = 0x1e240;

$nbr08c = "0X1E240";

$nbr08d = 0X1E240;

$bas08 = 16;

echo 
gettype($nbr08a) . ' ';
echo 
gmp_init($nbr08a, $bas08);

echo 
'<br><br>';

echo 
gettype($nbr08b) . ' ';
echo 
gmp_init($nbr08b, $bas08);

echo 
'<br><br>';

echo 
gettype($nbr08c) . ' ';
echo 
gmp_init($nbr08c, $bas08);

echo 
'<br><br>';

echo 
gettype($nbr08d) . ' ';
echo 
gmp_init($nbr08d, $bas08);

?>


  9 EXERCISE   

<?php

// This code display an Warning in PHP 7.4.XX

$nbr09 = 123456.456;

$bas09 = 10;

echo 
gettype($nbr09) . ' ';
echo 
gmp_init($nbr09, $bas09) . '<br>';

?>