How do I convert a string to a number in PHP

0 votes

I want to convert these types of values, '3', '2.34', '0.234343', etc. to a number. In JavaScript we can use Number(), but is there any similar method available in PHP?

Input                     Output 
'2'                               2 
'2.34'                          2.34 
'0.3454545'                0.3454545

Feb 23 in PHP by Soham
• 9,670 points
112 views

1 answer to this question.

0 votes

You don't have to do this, since PHP will coerce the type for you in most circumstances. For situations where you do want to explicitly convert the type, try casting it by using:

$num = "3.14"; 
$int = (int)$num; 
$float = (float)$num

answered Feb 23 by Aditya
• 7,660 points

Related Questions In PHP

0 votes
1 answer

How do I convert a PDF document to a preview image in PHP?

Hello @kartik, You need ImageMagick and GhostScript <?php $im = new imagick('file.pdf[0]'); $im->setImageFormat('jpg'); header('Content-Type: image/jpeg'); echo ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,800 points
3,238 views
0 votes
1 answer

How to convert a string to JSON object in PHP?

Hello @kartik, it seems that your JSON is ...READ MORE

answered Oct 20, 2020 in PHP by Niroj
• 82,800 points
3,200 views
0 votes
0 answers

How do I replace part of a string in PHP?

I want to replace space in the first ...READ MORE

May 28 in PHP by Kichu
• 19,040 points
51 views
0 votes
0 answers

How to convert an array to a string in PHP?

What can I do to get the ...READ MORE

May 30 in PHP by Kichu
• 19,040 points
103 views
0 votes
0 answers

Converting an integer to a string in PHP

Is it possible to change an integer ...READ MORE

Aug 1 in PHP by Kithuzzz
• 20,660 points
244 views
0 votes
1 answer

SQL error "ORA-01722: invalid number"

When an effort is made to convert ...READ MORE

answered Feb 15 in Database by Neha
• 8,940 points
1,135 views
0 votes
0 answers

PHP typecasting

what is the best way to typecast ...READ MORE

Jun 14 in PHP by narikkadan
• 37,660 points
42 views
0 votes
0 answers

How to convert an array to object in PHP?

How can I convert an array like ...READ MORE

Jun 18 in PHP by narikkadan
• 37,660 points
56 views
0 votes
1 answer

How do I strip all spaces out of a string in PHP?

If I have understood your question right, ...READ MORE

answered Feb 23 in PHP by Aditya
• 7,660 points
162 views
0 votes
1 answer

Convert a PHP object to an associative array

Start with simply typecasting the line:- $array = ...READ MORE

answered Feb 23 in PHP by Aditya
• 7,660 points
589 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP