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.

Read Also:  Global.asax File in ASP.NET MVC Tutorial with 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

intval($string);

Integers

<?php
$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;

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 infinityknow.com 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.

Read Also:  Vue JS Get String length Property

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel Convert object Array JSON String Example
  2. Read Also:  WooCommerce Get $product object from product ID
  3. Read Also:  How to Open URL in New Tab using Node JS
  4. Read Also:  Find all ASP.NET loop through controls on Form Clear of specific type
  5. Read Also:  What is the difference between let and var in Typescript
  6. Read Also:  Remove Yellow BGBOX WordPress plugin
  7. Read Also:  Angular Grid Alternate row color Example
  8. Read Also:  Angular Dynamic Add ng-click function
  9. Read Also:  What is the difference between let and var in Typescript
  10. Read Also:  Remove Yellow BGBOX WordPress plugin
CLOSEX