How to Convert a String to a Number in PHP

  569 views   11 months ago PHP

Use Type Casting

As we know PHP does not require or support explicit type definition in variable declaration. However, you can force a variable to be evaluated as a certain type using type casting.

Let's try out the following example to understand how this works:

$num = "2.75";
// Cast to integer
$int = (int)$num;
echo gettype($int); // Outputs: integer
echo $int; // Outputs: 2
// Cast to float
$float = (float)$num;
echo gettype($float); // Outputs: double
echo $float; // Outputs: 2.75


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on

Related Articles