Type Casting to Integers in PHP
Let's say, for example, we have a string with digits:
<?php
$test = '1';
var_dump($test);
?>
Let's convert it to an integer. For
this, we will use the special command int
,
like this:
<?php
$test = (int) '1';
var_dump($test); // will output 1 - a number
?>
You can use the type casting command directly in the function call:
<?php
var_dump((int) '1');
?>
You can convert the value of a variable:
<?php
$test = '1';
var_dump((int) $test);
?>
Convert the following string to a number:
<?php
$test = '12345';
?>