How to Convert Str to Int in PHP

Sometimes you require to convert the string into the number like int, float, or any other type. It is needed when you get a number as a string; then, it is necessary to convert this string into a number for performing further operations on that string type number.

Convert Str to Int in PHP

In PHP, there are different methods for converting the string into a number. PHP provides some inbuilt functions for converting the string into a number. You can also do the same by using the type conversion method.

You can use the following methods for converting the str to int.

  • The simple approach of adding zero number
  • Typecasting method
  • intval() function method
  • number_format() function method

Read Also: How to convert string to int in Java

1] Simple approach of adding zero number

In this method, you perform a mathematical operation on that string, which you want to convert into a number. You add zero to that string, and it converts the string into int number. This is an essential and most straightforward approach for converting the string into number implicitly.

Example:

<!DOCTYPE html>
<html>
<head>
    <title>Str to Int in PHP</title>
</head>
<body>

<?php

// Number into string format
$x = "7890";

// Performing mathematical operation
// to implicitly type conversion
$y = $x + 0 ;
echo $y."<br>";

// checking the type of $y after conversion
echo "Checking the type after conversion:<br>";
var_dump(is_int($y));
?>

</body>
</html>

Output:

7890
Checking the type after conversion:
bool(true)

Explanation:

  • First, we assign the number in string format to $x
  • Next, we perform addition for converting the string into number implicitly
  • Then, we check if it converted successfully or not using is_int() function.

2] Typecasting method

This method directly converts the string into number form. It is the best method for converting a string into any other type.

Syntax:

(type)variable name;

Here type is in which you want to convert the variable.

Example:

<!DOCTYPE html>
<html>
<head> 
    <title>Str to Int in PHP</title> 
</head> 
<body>

<?php

// Number into string format
$x = "7890";

// convert string into number using
// type casting method

echo (int)$x."<br>";

?>

</body>
</html>

Output:

7890

3] intval() function method

In this method, the intval() function is used for converting the string into int number. This is the inbuilt function of PHP. It returns the number after the successful conversion of string; otherwise returns an error message.

Example:

<!DOCTYPE html>
<html>
<head> 
    <title>Str to Int in PHP</title> 
</head> 
<body>

<?php

// Number into string format
$x = "7890";

// convert string into number using
// intval() function method

$y = intval($x)."<br>";
echo $y;

?>

</body>
</html>

Output:

7,890

4] number_format() function method

In this method, the number_format() function is used for converting the string into int number. This is the inbuilt function of PHP. It returns the number after the successful conversion of string; otherwise returns an error message.

Example:

<!DOCTYPE html>
<html>
<head> 
    <title>Str to Int in PHP</title> 
</head> 
<body>

<?php

// Number into string format
$x = "7890";

// convert string into number using
// number_format() function

$y = number_format($x) ;
echo $y."<br>";

?>

</body>
</html>

Output:

7,890

Conclusion

This all about converting the string into number format. It is mainly used when we get numbers as a string, and we want to perform some mathematical operations on that number.

I hope you found this post ‘ Str to Int in PHP ‘ fully informative and helpful.

Thank you for reading 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
Change Text Color on Mouseover
HTML Code to Change Text Color on Mouseover

As a regular user, you have noticed on most websites that when you click on some text or link, it...

Close