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, 2022 in PHP by Soham
• 9,700 points
459 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, 2022 by Aditya
• 7,680 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,880 points
4,417 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,880 points
5,142 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, 2022 in PHP by Kichu
• 19,050 points
298 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, 2022 in PHP by Kichu
• 19,050 points
450 views
0 votes
0 answers

Converting an integer to a string in PHP

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

Aug 1, 2022 in PHP by Kithuzzz
• 38,010 points
745 views
0 votes
1 answer

SQL error "ORA-01722: invalid number"

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

answered Feb 15, 2022 in Database by Neha
• 9,060 points
2,750 views
0 votes
0 answers

PHP typecasting

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

Jun 14, 2022 in PHP by narikkadan
• 63,700 points
425 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, 2022 in PHP by narikkadan
• 63,700 points
354 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, 2022 in PHP by Aditya
• 7,680 points
999 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, 2022 in PHP by Aditya
• 7,680 points
1,789 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