How to Convert a String to a Number in PHP?

Today, We want to share with you php convert string to int.In this post we will show you transforming string to integer in php, hear for Using intval() and floatval() Function. The intval() and floatval() PHP functions can also be used to convert the string into its corresponding integer as well as float values respectively. echo floatval ( $num ); we will give you demo and example for implement.In this post, we will learn about php get number from string with an example.

PHP: Convert a string into an integer.

Types of PHP Numbers

Numbers in PHP can take 4 forms:

  • Integers
  • Floats
  • Infinity
  • NaN

php string to int



$x = 54; # integer
$y = "54" # string

var_dump(is_int($x)); # Output: bool(true)
var_dump(is_int($y)); # Output: bool(false)

Example 1: cast string to int php

$num = "8.14"; 
$int = (int)$num;//string to int
$float = (float)$num;//string to float

Example 2: php to int

$num = "8.14";
$int = (int)$num;

Free Live Chat for Any Issue

Example 3: convert string to int php

$str = "899898";
echo intval($str); // 899898

$str = "welcome";
echo intval($str);	//0

I hope you get an idea about get int in php.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.