<?php
int intval ( mix $var , int $base = 10 );
where,
$var = variable to be converted to integer value
( MUST BE SCALAR )
$base = The specified BASE for conversion
( default = 10 )
?>
#bit | PHP_INT_MIN Since PHP 7.0.0 |
PHP_INT_MAX Since PHP 5.0.5 |
32 | −2147483648 | +2147483647 |
64 | −9223372036854775808 | +9223372036854775807 |
ed48 |
IF $var TYPE | RETURN | |
NULL | 0 | |
BOOLEAN | false ( FALSE) | 0 |
true ( TRUE) | 1 | |
STRING | PURE | 0 |
NUMERICAL OUT OF THE LIMITS | INTEGER SYSTEM LIMITS |
|
NUMERICAL WITHIN THE LIMITS | INTEGER INTO THE LIMITS |
|
FIRST LITERAL CHARACTER | 0 | |
FIRST NUMERICAL CHARACTERS | INTEGER NUMERICAL PART |
|
NUMBER | OFF-LIMITS | INF |
WITHIN THE LIMITS | INTEGER | |
ARRAY | EMPTY | 0 |
NOT EMPTY | 1 | |
RESOURCE | resource ID | |
OBJECT | EMPTY | 0 Notice or Warning |
NOT EMPTY | 1 Notice or Warning |
|
ed48 |
<?php
function integr01($tit, $var)
{
if(!is_object($var))
{
// BASE 10
echo $tit;
echo 'Value = ';
var_dump($var);
echo '<br>' . intval($var) . '<br><br>';
}
else
{
echo 'OBJECT (PHP 8.0.XX)<br>';
var_dump($var);
echo '<br>Cannot be applied!';
}
}
$v1a = 'INTEGER TIMESTAMP<br>';
$V1a = $_SERVER['REQUEST_TIME'];
// The timestamp of the start of the request.
$v2a = 'FLOAT TIMESTAMP<br>';
$V2a = $_SERVER['REQUEST_TIME_FLOAT'];
// The timestamp, with precision in microseconds,
// from the beginning of the request
$v3a = 'POSITIVE INTEGER NUMBER<br>';
$V3p = 220;
$v3b = 'NEGATIVE INTEGER NUMBER<br>';
$V3n = -220;
$v4a = 'FLOAT NUMBER as STRING<br>';
$V4p = '123.456';
$v4b = 'FLOAT NUMBER as IS<br>';
$V4n = -123.456;
$v5a = 'CIENTIFIC NOTATION as STRING<br>';
$V5a = '1E19';
$v5b = 'CIENTIFIC NOTATION as IS<br>';
$V5b = 1.6e-19;
// -> (20b)base 16 = (20B)base 16 = (523)base 10
$v6a = 'HEXADECIMAL NOTATION as STRING<br>';
$V6x = '0x20b';
$v6b = 'HEXADECIMAL NOTATION as IS<br>';
$V6X = 0X20B;
// -> (1023)base 8 = (531)base 10
$v7a = 'OCTAL NOTATION as STRING<br>';
$V7o = '01023';
$v7b = 'OCTAL NOTATION<br>';
$V7O = 01023;
// -> (0011)base 2 = (3)base 10
$v8a = 'BINARY NOTATION as STRING<br>';
$V8B = '0B0011';
$v8b = 'BINARY NOTATION<br>';
$V8b = 0b0011;
// Available since PHP 5.4.0
$v9a = 'CONSTANT VALUE = e<br>';
$V9a = M_E;
$v10a = 'CONSTANT VALUE = pi<br>';
$V10a = M_PI;
$v11a = 'CONSTANT VALUE = SQUARE ROOT of 2<br>';
$V11a = M_SQRT2;
$v12a = 'NOT EMPTY ARRAY<br>';
$V12a = [ 'a' => 1, 'b' => 2 ];
$v13a = 'EMPTY ARRAY<br>';
$V13a = [];
$v14a = 'RESOURCE (PHP 7.4.XX)<br>';
$v14b = 'OBJECT (PHP 8.0.XX)<br>';
$V14a = imagecreate(1,1);
integr01($v1a, $V1a);
integr01($v2a, $V2a);
integr01($v3a, $V3p);
integr01($v3b, $V3n);
integr01($v4a, $V4p);
integr01($v4b, $V4n);
integr01($v5a, $V5a);
integr01($v5b, $V5b);
integr01($v6a, $V6x);
integr01($v6b, $V6X);
integr01($v7a, $V7o);
integr01($v7b, $V7O);
integr01($v8a, $V8B);
integr01($v8b, $V8b);
integr01($v9a, $V9a);
integr01($v10a, $V10a);
integr01($v11a, $V11a);
integr01($v12a, $V12a);
integr01($v13a, $V13a);
integr01($v14a, $V14a);
?>
<?php
function integr02($tit, $var)
{
// BASE 16
echo $tit;
echo 'Value = ';
var_dump($var);
echo '<br>' . intval($var, 16) . '<br><br>';
}
$v14a = 'INTEGER TIMESTAMP<br>';
$V14a = $_SERVER['REQUEST_TIME'];
// The timestamp of the start of the request.
$v15a = 'FLOAT TIMESTAMP<br>';
$V15a = $_SERVER['REQUEST_TIME_FLOAT'];
// The timestamp, with precision in microseconds,
// from the beginning of the request
$v16a = 'POSITIVE INTEGER NUMBER<br>';
$V16p = 220;
$v16b = 'NEGATIVE INTEGER NUMBER<br>';
$V16n = -220;
$v17a = 'FLOAT NUMBER as STRING<br>';
$V17p = '123.456';
$v17b = 'FLOAT NUMBER as IS<br>';
$V17n = -123.456;
$v18a = 'CIENTIFIC NOTATION as STRING<br>';
$V18a = '1E19';
$v18b = 'CIENTIFIC NOTATION as IS<br>';
$V18b = 1.6e-19;
// -> (20b)base 16 = (20B)base 16 = (523)base 10
$v19a = 'HEXADECIMAL NOTATION as STRING<br>';
$V19x = '0x20b';
$v19b = 'HEXADECIMAL NOTATION as IS<br>';
$V19X = 0X20B;
// -> (1023)base 8 = (531)base 10
$v20a = 'OCTAL NOTATION as STRING<br>';
$V20o = '01023';
$v20b = 'OCTAL NOTATION<br>';
$V20O = 01023;
// -> (0011)base 2 = (3)base 10
$v21a = 'BINARY NOTATION as STRING<br>';
$V21B = '0B0011';
$v21b = 'BINARY NOTATION<br>';
$V21b = 0b0011;
// Available since PHP 5.4.0
$v22a = 'CONSTANT VALUE = e<br>';
$V22a = M_E;
$v23a = 'CONSTANT VALUE = pi<br>';
$V23a = M_PI;
$v24a = 'CONSTANT VALUE = SQUARE ROOT of 2<br>';
$V24a = M_SQRT2;
$v25a = 'NOT EMPTY ARRAY<br>';
$V25a = [ 'a' => 1, 'b' => 2 ];
$v26a = 'EMPTY ARRAY<br>';
$V26a = [];
$v27a = 'RESOURCE (PHP 7.4.XX)<br>';
$V27a = imagecreate(1,1);
$t1 = 'CLASS OBJECT<br>';
class test01
{
var $var01 = 'Alea jacta est';
var $var02 = 'Luck is on';
}
$obj01 = new test01;
integr02($v14a, $V14a);
integr02($v15a, $V15a);
integr02($v16a, $V16p);
integr02($v16b, $V16n);
integr02($v17a, $V17p);
integr02($v17b, $V17n);
integr02($v18a, $V18a);
integr02($v18b, $V18b);
integr02($v19a, $V19x);
integr02($v19b, $V19X);
integr02($v20a, $V20o);
integr02($v20b, $V20O);
integr02($v21a, $V21B);
integr02($v21b, $V21b);
integr02($v22a, $V22a);
integr02($v23a, $V23a);
integr02($v24a, $V24a);
integr02($v25a, $V25a);
integr02($v26a, $V26a);
integr02($v27a, $V27a);
integr02($t1, $obj01);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
To run this exercise at the same time
as the first exercise, without any error,
it was necessary to change the name of
the user-defined functions:
integr01 is different from integr02.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
<?php
$u1 = NULL;
var_dump($u1);
$ut1 = intval($u1);
echo '<br>' . $ut1 . '<br><br>';
$u2 = false;
var_dump($u2);
$ut2 = intval($u2);
echo '<br>' . $ut2 . '<br><br>';
$u3 = TRUE;
var_dump($u3);
$ut3 = intval($u3);
echo '<br>' . $ut3 . '<br><br>';
$u4 = 'Anything';
var_dump($u4);
$ut4 = intval($u4);
echo '<br>' . $ut4 . '<br><br>';
$u5 = 'D-5100';
var_dump($u5);
$ut5 = intval($u5);
echo '<br>' . $ut5 . '<br><br>';
$u6 = '12.3D5100';
var_dump($u6);
$ucr6 = intval($u6);
echo '<br>' . $ucr6 . '<br><br>';
$u7 = '123800.89898';
var_dump($u7);
$ut7 = intval($u7);
echo '<br>' . $ut7 . '<br><br>';
$u81 = '92233720368547758988887737737737373737373';
var_dump($u81);
$ucr81 = intval($u81);
echo '<br>' . $ucr81 . '<br><br>';
$u82 = '-92233720368547758988887737737737373737373';
var_dump($u82);
$ucr82 = intval($u82);
echo '<br>' . $ucr82 . '<br><br>';
$u9 = 92233720368547758.07;
var_dump($u9);
$ut9 = intval($u9);
echo '<br>' . $ut9 . '<br><br>';
$uA = 922.3372;
var_dump($uA);
$utA = intval($uA);
echo '<br>' . $utA . '<br><br>';
$uB = array();
var_dump($uB);
$utB = intval($uB);
echo '<br>' . $utB . '<br><br>';
$uC = array(1,3,5,7);
var_dump($uC);
$utC = intval($uC);
echo '<br>' . $utC . '<br><br>';
if(PHP_VERSION < 8)
{
$uD = imagecreate(2,8);
var_dump($uD);
$utD = intval($uD);
echo '<br> ' . $utD . '<br><br>';
$uE = imagecreate(3,5);
var_dump($uE);
$utE = intval($uE);
echo '<br> ' . $utE . '<br><br>';
}
else
{
$uD = imagecreate(2,8);
var_dump($uD);
echo '<br>Not available in PHP 8.0.XX<br><br>';
$uE = imagecreate(3,5);
var_dump($uE);
echo '<br>Not available in PHP 8.0.XX<br><br>';
}
$uF = '1E19';
var_dump($uF);
$utF = intval($uF);
echo '<br> ' . $utF . '<br><br>';
$uG = 1.6e-19;
var_dump($uG);
$utG = intval($uG);
echo '<br> ' . $utG . '<br><br>';
$uH = 1.6e2119;
var_dump($uH);
$utH = intval($uH);
echo '<br> ' . $utH . '<br><br>';
$uI = PHP_FLOAT_EPSILON;
var_dump($uI);
$utI = intval($uI);
echo '<br> ' . $utI . '<br><br>';
?>