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
• 8,730 points
64 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,280 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,720 points
2,813 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,720 points
2,247 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
4 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
15 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,560 points
421 views
0 votes
0 answers

PHP typecasting

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

Jun 14 in PHP by narikkadan
• 10,840 points
4 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
• 10,840 points
10 views
+1 vote
2 answers

Scp Php files into server using gradle

Tru something like this: plugins { id ...READ MORE

answered Oct 11, 2018 in DevOps & Agile by lina
• 8,220 points
526 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,280 points
90 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,280 points
125 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP