How to Convert a String to a Number in PHP

How to Convert a String to a Number in PHP

In this post we will give you information about How to Convert a String to a Number in PHP. Hear we will give you detail about How to Convert a String to a Number in PHPAnd how to use it also give you demo for it if it is necessary.

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:

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

 

Hope this code and post will helped you for implement How to Convert a String to a Number in PHP. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US