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.
Index
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.
Read Also: How to convert string to int in Java
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:
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
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
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
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 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…