-3

How to convert string to number in PHP?

Could any one tell me how to convert string data to number?

For example:

$Str = "212315"; // convert to 212315
yekta_7
  • 21
  • 6
mohan21
  • 65
  • 1
  • 1
  • 13

2 Answers2

7

You cast it to an integer.

(int) $Str;

http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting

To explicitly convert a value to integer, use either the (int) or (integer) casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument. A value can also be converted to integer with the intval() function.

mattslone
  • 297
  • 1
  • 2
  • 14
2

you can do several things

$str = "212315";

$int = intval($str);

$int2 = (int) $str;

$int3 = (integer) $str;
cmorrissey
  • 8,493
  • 2
  • 23
  • 27