How To

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 🙂

Yash Panchal

Computer science student, Working as Web Developer and part-time Blogger. Highly interested in AI & ML.

Recent Posts

5 Important Things To Know About WordPress Before You Use It

There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…

2 years ago

How to Install MySQL on Your PC in 3 Easy Steps

In this tutorial, I'm going to show you how to install MySQL on your computer.…

3 years ago

Download and Install Turbo C++ for Windows 10 (Full Installation Guide)

Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…

3 years ago

PHP .HTACCESS Redirects

We can redirect any webpage to any other or redirect the whole domain or website…

3 years ago

PHP Redirect Pages

There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…

3 years ago

PHP Include & Required

Include files in PHP are used in appending various global or config files. We can…

3 years ago