1
$the_value = '42';

This is a string value. Now I want to convert it into int and obtain:

$the_value = 42;
Mel Sardes
  • 23
  • 1
  • 4
  • 2
    In almost all cases `'42'` would be treated the same as `42`, and converted automatically for you. So, what do you want to do that makes it important to make this distinction? – KIKO Software May 09 '22 at 20:15
  • 1
    https://www.php.net/manual/en/function.intval.php – Sammitch May 09 '22 at 20:22
  • @KIKOSoftware for example ` – Markus AO May 09 '22 at 20:36
  • @MarkusAO: It's not that I cannot think of countless examples where type is important, but the question was missing it. It could be an [XY-problem](https://xyproblem.info/) where type is not the issue, but something else. – KIKO Software May 09 '22 at 20:51
  • @KIKOSoftware right, based on available context (none), OP could just define an int to begin with if an int is desired. – Markus AO May 09 '22 at 20:58

1 Answers1

1

To convert string to int in PHP, you can use Type Casting method or PHP built-in function intval().

<?php
$string = "56";
$int = intval( $string );
echo $int;
?>